Flutter Engine
The Flutter Engine
GrQuad.cpp
Go to the documentation of this file.
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
9
12
14
15static 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
24static void map_rect_translate_scale(const SkRect& rect, const SkMatrix& m,
25 V4f* xs, V4f* ys) {
26 SkMatrix::TypeMask tm = m.getType();
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
43static 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
63static 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)
72static 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.
79 if (matrix.rectStaysRect()) {
81 } else if (matrix.preservesRightAngles()) {
83 } else if (matrix.hasPerspective()) {
85 } else {
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.
93 if (matrix.hasPerspective()) {
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)) {
102 } else {
104 }
105}
106
108 V4f x, y, w;
109 SkMatrix::TypeMask tm = m.getType();
110 Type type;
113 w = 1.f;
115 } else {
116 map_rect_general(rect, m, &x, &y, &w);
118 }
119 return GrQuad(x, y, w, type);
120}
121
123 V4f xs, ys;
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
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
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}
static void map_rect_general(const SkRect &rect, const SkMatrix &matrix, V4f *xs, V4f *ys, V4f *ws)
Definition: GrQuad.cpp:63
static void map_quad_general(const V4f &qx, const V4f &qy, const SkMatrix &m, V4f *xs, V4f *ys, V4f *ws)
Definition: GrQuad.cpp:43
static GrQuad::Type quad_type_for_transformed_rect(const SkMatrix &matrix)
Definition: GrQuad.cpp:78
static void map_rect_translate_scale(const SkRect &rect, const SkMatrix &m, V4f *xs, V4f *ys)
Definition: GrQuad.cpp:24
static void rearrange_sk_to_gr_points(const SkPoint skQuadPts[4], V4f *xs, V4f *ys)
Definition: GrQuad.cpp:72
static bool aa_affects_rect(GrQuadAAFlags edgeFlags, float ql, float qt, float qr, float qb)
Definition: GrQuad.cpp:15
static GrQuad::Type quad_type_for_points(const SkPoint pts[4], const SkMatrix &matrix)
Definition: GrQuad.cpp:92
GrQuadAAFlags
Definition: GrTypesPriv.h:247
#define SkASSERT(cond)
Definition: SkAssert.h:116
static bool SkScalarIsInt(SkScalar x)
Definition: SkScalar.h:80
GLenum type
Definition: GrQuad.h:30
const float * xs() const
Definition: GrQuad.h:132
const float * ys() const
Definition: GrQuad.h:134
float y(int i) const
Definition: GrQuad.h:109
Type quadType() const
Definition: GrQuad.h:118
static GrQuad MakeFromRect(const SkRect &, const SkMatrix &)
Definition: GrQuad.cpp:107
float w(int i) const
Definition: GrQuad.h:110
bool aaHasEffectOnRect(GrQuadAAFlags edgeFlags) const
Definition: GrQuad.cpp:135
float x(int i) const
Definition: GrQuad.h:108
bool asRect(SkRect *rect) const
Definition: GrQuad.cpp:141
static GrQuad MakeFromSkQuad(const SkPoint pts[4], const SkMatrix &)
Definition: GrQuad.cpp:122
SkRect bounds() const
Definition: GrQuad.h:81
Type
Definition: GrQuad.h:35
GrQuad()=default
static constexpr int kMPersp2
perspective bias
Definition: SkMatrix.h:361
@ kTranslate_Mask
translation SkMatrix
Definition: SkMatrix.h:193
@ kScale_Mask
scale SkMatrix
Definition: SkMatrix.h:194
@ kIdentity_Mask
identity SkMatrix; all bits clear
Definition: SkMatrix.h:192
struct MyStruct s
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkScalar w
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
Definition: SkVx.h:83
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition: SkVx.h:109