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/geometry/GrQuad.h"
9
10#include "include/core/SkMatrix.h"
11
12using V4f = skvx::Vec<4, float>;
13
14static bool aa_affects_rect(float ql, float qt, float qr, float qb) {
15 return !SkScalarIsInt(ql) || !SkScalarIsInt(qr) || !SkScalarIsInt(qt) || !SkScalarIsInt(qb);
16}
17
18static void map_rect_translate_scale(const SkRect& rect, const SkMatrix& m,
19 V4f* xs, V4f* ys) {
20 SkMatrix::TypeMask tm = m.getType();
21 SkASSERT(tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
22
23 V4f r = V4f::Load(&rect);
24 if (tm > SkMatrix::kIdentity_Mask) {
25 const V4f t{m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()};
26 if (tm <= SkMatrix::kTranslate_Mask) {
27 r += t;
28 } else {
29 const V4f s{m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()};
30 r = r * s + t;
31 }
32 }
33 *xs = skvx::shuffle<0, 0, 2, 2>(r);
34 *ys = skvx::shuffle<1, 3, 1, 3>(r);
35}
36
37static 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()));
41 if (m.hasPerspective()) {
42 V4f w = mad(m.getPerspX(), qx,
43 mad(m.getPerspY(), qy, m.get(SkMatrix::kMPersp2)));
44 if (ws) {
45 // Output the calculated w coordinates
46 *ws = w;
47 } else {
48 // Apply perspective division immediately
49 V4f iw = 1.f / w;
50 *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,
59 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};
62 map_quad_general(rx, ry, matrix, xs, ys, ws);
63}
64
65// 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)
67static 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};
70}
71
72// If an SkRect is transformed by this matrix, what class of quad is required to represent it.
73static GrQuad::Type quad_type_for_transformed_rect(const SkMatrix& matrix) {
74 if (matrix.rectStaysRect()) {
75 return GrQuad::Type::kAxisAligned;
76 } else if (matrix.preservesRightAngles()) {
77 return GrQuad::Type::kRectilinear;
78 } else if (matrix.hasPerspective()) {
79 return GrQuad::Type::kPerspective;
80 } else {
81 return GrQuad::Type::kGeneral;
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 GrQuad::Type quad_type_for_points(const SkPoint pts[4], const SkMatrix& matrix) {
88 if (matrix.hasPerspective()) {
89 return GrQuad::Type::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 GrQuad::Type::kGeneral;
99 }
100}
101
102GrQuad GrQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) {
103 V4f x, y, w;
104 SkMatrix::TypeMask tm = m.getType();
105 Type type;
106 if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
107 map_rect_translate_scale(rect, m, &x, &y);
108 w = 1.f;
109 type = Type::kAxisAligned;
110 } else {
111 map_rect_general(rect, m, &x, &y, &w);
112 type = quad_type_for_transformed_rect(m);
113 }
114 return GrQuad(x, y, w, type);
115}
116
117GrQuad GrQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) {
118 V4f xs, ys;
119 rearrange_sk_to_gr_points(pts, &xs, &ys);
120 Type type = quad_type_for_points(pts, matrix);
121 if (matrix.isIdentity()) {
122 return GrQuad(xs, ys, 1.f, type);
123 } else {
124 V4f mx, my, mw;
125 map_quad_general(xs, ys, matrix, &mx, &my, &mw);
126 return GrQuad(mx, my, mw, type);
127 }
128}
129
130bool GrQuad::aaHasEffectOnRect() const {
131 SkASSERT(this->quadType() == Type::kAxisAligned);
132 // If rect, ws must all be 1s so no need to divide
133 return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]);
134}
135
136bool GrQuad::asRect(SkRect* rect) const {
137 if (this->quadType() != Type::kAxisAligned) {
138 return false;
139 }
140
141 *rect = this->bounds();
142 // v0 at the geometric top-left is unique amongst axis-aligned vertex orders
143 // (90, 180, 270 rotations or axis flips all move v0).
144 return fX[0] == rect->fLeft && fY[0] == rect->fTop;
145}
146