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