Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 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 | |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 8 | #include "src/gpu/geometry/GrQuad.h" |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 9 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 10 | #include "include/core/SkMatrix.h" |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 11 | |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 12 | using V4f = skvx::Vec<4, float>; |
| 13 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 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 | |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 18 | static void map_rect_translate_scale(const SkRect& rect, const SkMatrix& m, |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 19 | V4f* xs, V4f* ys) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 20 | SkMatrix::TypeMask tm = m.getType(); |
| 21 | SkASSERT(tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)); |
| 22 | |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 23 | V4f r = V4f::Load(&rect); |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 24 | if (tm > SkMatrix::kIdentity_Mask) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 25 | const V4f t{m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()}; |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 26 | if (tm <= SkMatrix::kTranslate_Mask) { |
| 27 | r += t; |
| 28 | } else { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 29 | const V4f s{m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()}; |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 30 | r = r * s + t; |
| 31 | } |
| 32 | } |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 33 | *xs = skvx::shuffle<0, 0, 2, 2>(r); |
| 34 | *ys = skvx::shuffle<1, 3, 1, 3>(r); |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 37 | static 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())); |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 41 | if (m.hasPerspective()) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 42 | V4f w = mad(m.getPerspX(), qx, |
| 43 | mad(m.getPerspY(), qy, m.get(SkMatrix::kMPersp2))); |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 44 | if (ws) { |
| 45 | // Output the calculated w coordinates |
| 46 | *ws = w; |
| 47 | } else { |
| 48 | // Apply perspective division immediately |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 49 | V4f iw = 1.f / w; |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 50 | *xs *= iw; |
| 51 | *ys *= iw; |
| 52 | } |
| 53 | } else if (ws) { |
| 54 | *ws = 1.f; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void map_rect_general(const SkRect& rect, const SkMatrix& matrix, |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 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}; |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 62 | map_quad_general(rx, ry, matrix, xs, ys, ws); |
| 63 | } |
| 64 | |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 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) |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 67 | static 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}; |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 72 | // If an SkRect is transformed by this matrix, what class of quad is required to represent it. |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 73 | static GrQuad::Type quad_type_for_transformed_rect(const SkMatrix& matrix) { |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 74 | if (matrix.rectStaysRect()) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 75 | return GrQuad::Type::kAxisAligned; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 76 | } else if (matrix.preservesRightAngles()) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 77 | return GrQuad::Type::kRectilinear; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 78 | } else if (matrix.hasPerspective()) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 79 | return GrQuad::Type::kPerspective; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 80 | } else { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 81 | return GrQuad::Type::kGeneral; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 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. |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 87 | static GrQuad::Type quad_type_for_points(const SkPoint pts[4], const SkMatrix& matrix) { |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 88 | if (matrix.hasPerspective()) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 89 | return GrQuad::Type::kPerspective; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 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 { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 98 | return GrQuad::Type::kGeneral; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 102 | GrQuad GrQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 103 | V4f x, y, w; |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 104 | SkMatrix::TypeMask tm = m.getType(); |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 105 | Type type; |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 106 | if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 107 | map_rect_translate_scale(rect, m, &x, &y); |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 108 | w = 1.f; |
| 109 | type = Type::kAxisAligned; |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 110 | } else { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 111 | map_rect_general(rect, m, &x, &y, &w); |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 112 | type = quad_type_for_transformed_rect(m); |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 113 | } |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 114 | return GrQuad(x, y, w, type); |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 115 | } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 116 | |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 117 | GrQuad GrQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 118 | V4f xs, ys; |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 119 | rearrange_sk_to_gr_points(pts, &xs, &ys); |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 120 | Type type = quad_type_for_points(pts, matrix); |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 121 | if (matrix.isIdentity()) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 122 | return GrQuad(xs, ys, 1.f, type); |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 123 | } else { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 124 | V4f mx, my, mw; |
| 125 | map_quad_general(xs, ys, matrix, &mx, &my, &mw); |
| 126 | return GrQuad(mx, my, mw, type); |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 130 | bool GrQuad::aaHasEffectOnRect() const { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 131 | SkASSERT(this->quadType() == Type::kAxisAligned); |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 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 | } |
Michael Ludwig | 22429f9 | 2019-06-27 10:44:48 -0400 | [diff] [blame] | 135 | |
| 136 | bool 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 | } |