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 "src/gpu/ganesh/geometry/GrQuad.h"
9
10 #include "include/core/SkMatrix.h"
11 #include "include/private/gpu/ganesh/GrTypesPriv.h"
12
13 using V4f = skvx::Vec<4, float>;
14
aa_affects_rect(GrQuadAAFlags edgeFlags,float ql,float qt,float qr,float qb)15 static bool aa_affects_rect(GrQuadAAFlags edgeFlags, float ql, float qt, float qr, float qb) {
16 // Edge coordinates for non-AA edges do not need to be integers; any AA-enabled edge that is
17 // at an integer coordinate could be drawn non-AA and be visually identical to non-AA.
18 return ((edgeFlags & GrQuadAAFlags::kLeft) && !SkScalarIsInt(ql)) ||
19 ((edgeFlags & GrQuadAAFlags::kRight) && !SkScalarIsInt(qr)) ||
20 ((edgeFlags & GrQuadAAFlags::kTop) && !SkScalarIsInt(qt)) ||
21 ((edgeFlags & GrQuadAAFlags::kBottom) && !SkScalarIsInt(qb));
22 }
23
map_rect_translate_scale(const SkRect & rect,const SkMatrix & m,V4f * xs,V4f * ys)24 static void map_rect_translate_scale(const SkRect& rect, const SkMatrix& m,
25 V4f* xs, V4f* ys) {
26 SkMatrix::TypeMask tm = m.getType();
27 SkASSERT(tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
28
29 V4f r = V4f::Load(&rect);
30 if (tm > SkMatrix::kIdentity_Mask) {
31 const V4f t{m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()};
32 if (tm <= SkMatrix::kTranslate_Mask) {
33 r += t;
34 } else {
35 const V4f s{m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()};
36 r = r * s + t;
37 }
38 }
39 *xs = skvx::shuffle<0, 0, 2, 2>(r);
40 *ys = skvx::shuffle<1, 3, 1, 3>(r);
41 }
42
map_quad_general(const V4f & qx,const V4f & qy,const SkMatrix & m,V4f * xs,V4f * ys,V4f * ws)43 static void map_quad_general(const V4f& qx, const V4f& qy, const SkMatrix& m,
44 V4f* xs, V4f* ys, V4f* ws) {
45 *xs = m.getScaleX() * qx + (m.getSkewX() * qy + m.getTranslateX());
46 *ys = m.getSkewY() * qx + (m.getScaleY() * qy + m.getTranslateY());
47 if (m.hasPerspective()) {
48 V4f w = m.getPerspX() * qx + (m.getPerspY() * qy + m.get(SkMatrix::kMPersp2));
49 if (ws) {
50 // Output the calculated w coordinates
51 *ws = w;
52 } else {
53 // Apply perspective division immediately
54 V4f iw = 1.f / w;
55 *xs *= iw;
56 *ys *= iw;
57 }
58 } else if (ws) {
59 *ws = 1.f;
60 }
61 }
62
map_rect_general(const SkRect & rect,const SkMatrix & matrix,V4f * xs,V4f * ys,V4f * ws)63 static void map_rect_general(const SkRect& rect, const SkMatrix& matrix,
64 V4f* xs, V4f* ys, V4f* ws) {
65 V4f rx{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight};
66 V4f ry{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom};
67 map_quad_general(rx, ry, matrix, xs, ys, ws);
68 }
69
70 // Rearranges (top-left, top-right, bottom-right, bottom-left) ordered skQuadPts into xs and ys
71 // ordered (top-left, bottom-left, top-right, bottom-right)
rearrange_sk_to_gr_points(const SkPoint skQuadPts[4],V4f * xs,V4f * ys)72 static void rearrange_sk_to_gr_points(const SkPoint skQuadPts[4], V4f* xs, V4f* ys) {
73 *xs = V4f{skQuadPts[0].fX, skQuadPts[3].fX, skQuadPts[1].fX, skQuadPts[2].fX};
74 *ys = V4f{skQuadPts[0].fY, skQuadPts[3].fY, skQuadPts[1].fY, skQuadPts[2].fY};
75 }
76
77 // If an SkRect is transformed by this matrix, what class of quad is required to represent it.
quad_type_for_transformed_rect(const SkMatrix & matrix)78 static GrQuad::Type quad_type_for_transformed_rect(const SkMatrix& matrix) {
79 if (matrix.rectStaysRect()) {
80 return GrQuad::Type::kAxisAligned;
81 } else if (matrix.preservesRightAngles()) {
82 return GrQuad::Type::kRectilinear;
83 } else if (matrix.hasPerspective()) {
84 return GrQuad::Type::kPerspective;
85 } else {
86 return GrQuad::Type::kGeneral;
87 }
88 }
89
90 // Perform minimal analysis of 'pts' (which are suitable for MakeFromSkQuad), and determine a
91 // quad type that will be as minimally general as possible.
quad_type_for_points(const SkPoint pts[4],const SkMatrix & matrix)92 static GrQuad::Type quad_type_for_points(const SkPoint pts[4], const SkMatrix& matrix) {
93 if (matrix.hasPerspective()) {
94 return GrQuad::Type::kPerspective;
95 }
96 // If 'pts' was formed by SkRect::toQuad() and not transformed further, it is safe to use the
97 // quad type derived from 'matrix'. Otherwise don't waste any more time and assume kStandard
98 // (most general 2D quad).
99 if ((pts[0].fX == pts[3].fX && pts[1].fX == pts[2].fX) &&
100 (pts[0].fY == pts[1].fY && pts[2].fY == pts[3].fY)) {
101 return quad_type_for_transformed_rect(matrix);
102 } else {
103 return GrQuad::Type::kGeneral;
104 }
105 }
106
MakeFromRect(const SkRect & rect,const SkMatrix & m)107 GrQuad GrQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) {
108 V4f x, y, w;
109 SkMatrix::TypeMask tm = m.getType();
110 Type type;
111 if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
112 map_rect_translate_scale(rect, m, &x, &y);
113 w = 1.f;
114 type = Type::kAxisAligned;
115 } else {
116 map_rect_general(rect, m, &x, &y, &w);
117 type = quad_type_for_transformed_rect(m);
118 }
119 return GrQuad(x, y, w, type);
120 }
121
MakeFromSkQuad(const SkPoint pts[4],const SkMatrix & matrix)122 GrQuad GrQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) {
123 V4f xs, ys;
124 rearrange_sk_to_gr_points(pts, &xs, &ys);
125 Type type = quad_type_for_points(pts, matrix);
126 if (matrix.isIdentity()) {
127 return GrQuad(xs, ys, 1.f, type);
128 } else {
129 V4f mx, my, mw;
130 map_quad_general(xs, ys, matrix, &mx, &my, &mw);
131 return GrQuad(mx, my, mw, type);
132 }
133 }
134
aaHasEffectOnRect(GrQuadAAFlags edgeFlags) const135 bool GrQuad::aaHasEffectOnRect(GrQuadAAFlags edgeFlags) const {
136 SkASSERT(this->quadType() == Type::kAxisAligned);
137 // If rect, ws must all be 1s so no need to divide
138 return aa_affects_rect(edgeFlags, fX[0], fY[0], fX[3], fY[3]);
139 }
140
asRect(SkRect * rect) const141 bool GrQuad::asRect(SkRect* rect) const {
142 if (this->quadType() != Type::kAxisAligned) {
143 return false;
144 }
145
146 *rect = this->bounds();
147 // v0 at the geometric top-left is unique amongst axis-aligned vertex orders
148 // (90, 180, 270 rotations or axis flips all move v0).
149 return fX[0] == rect->fLeft && fY[0] == rect->fTop;
150 }
151