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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrQuad.h" |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/GrTypesPriv.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 | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 72 | template <typename Q> |
| 73 | void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, |
| 74 | const Q& quad, GrQuadType knownType, |
| 75 | GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) { |
| 76 | // Most cases will keep the requested types unchanged |
| 77 | *outAAType = requestedAAType; |
| 78 | *outEdgeFlags = requestedEdgeFlags; |
| 79 | |
| 80 | switch (requestedAAType) { |
| 81 | // When aa type is coverage, disable AA if the edge configuration doesn't actually need it |
| 82 | case GrAAType::kCoverage: |
| 83 | if (requestedEdgeFlags == GrQuadAAFlags::kNone) { |
| 84 | // Turn off anti-aliasing |
| 85 | *outAAType = GrAAType::kNone; |
| 86 | } else { |
| 87 | // For coverage AA, if the quad is a rect and it lines up with pixel boundaries |
| 88 | // then overall aa and per-edge aa can be completely disabled |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 89 | if (knownType == GrQuadType::kRect && !quad.aaHasEffectOnRect()) { |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 90 | *outAAType = GrAAType::kNone; |
| 91 | *outEdgeFlags = GrQuadAAFlags::kNone; |
| 92 | } |
| 93 | } |
| 94 | break; |
| 95 | // For no or msaa anti aliasing, override the edge flags since edge flags only make sense |
| 96 | // when coverage aa is being used. |
| 97 | case GrAAType::kNone: |
| 98 | *outEdgeFlags = GrQuadAAFlags::kNone; |
| 99 | break; |
| 100 | case GrAAType::kMSAA: |
| 101 | *outEdgeFlags = GrQuadAAFlags::kAll; |
| 102 | break; |
| 103 | case GrAAType::kMixedSamples: |
| 104 | SK_ABORT("Should not use mixed sample AA with edge AA flags"); |
| 105 | break; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | // Instantiate GrResolve... for GrQuad and GrPerspQuad |
| 110 | template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrQuad&, GrQuadType, |
| 111 | GrAAType*, GrQuadAAFlags*); |
| 112 | template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrPerspQuad&, GrQuadType, |
| 113 | GrAAType*, GrQuadAAFlags*); |
| 114 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 115 | GrQuadType GrQuadTypeForTransformedRect(const SkMatrix& matrix) { |
| 116 | if (matrix.rectStaysRect()) { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 117 | return GrQuadType::kRect; |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 118 | } else if (matrix.preservesRightAngles()) { |
| 119 | return GrQuadType::kRectilinear; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 120 | } else if (matrix.hasPerspective()) { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 121 | return GrQuadType::kPerspective; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 122 | } else { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 123 | return GrQuadType::kStandard; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Michael Ludwig | 97b9442 | 2019-04-09 10:42:39 -0400 | [diff] [blame] | 127 | GrQuadType GrQuadTypeForPoints(const SkPoint pts[4], const SkMatrix& matrix) { |
| 128 | if (matrix.hasPerspective()) { |
| 129 | return GrQuadType::kPerspective; |
| 130 | } |
| 131 | // If 'pts' was formed by SkRect::toQuad() and not transformed further, it is safe to use the |
| 132 | // quad type derived from 'matrix'. Otherwise don't waste any more time and assume kStandard |
| 133 | // (most general 2D quad). |
Michael Ludwig | d8d859a | 2019-04-18 13:43:58 -0400 | [diff] [blame] | 134 | if ((pts[0].fX == pts[3].fX && pts[1].fX == pts[2].fX) && |
| 135 | (pts[0].fY == pts[1].fY && pts[2].fY == pts[3].fY)) { |
Michael Ludwig | 97b9442 | 2019-04-09 10:42:39 -0400 | [diff] [blame] | 136 | return GrQuadTypeForTransformedRect(matrix); |
| 137 | } else { |
| 138 | return GrQuadType::kStandard; |
| 139 | } |
| 140 | } |
| 141 | |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 142 | GrQuad GrQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame^] | 143 | V4f x, y; |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 144 | SkMatrix::TypeMask tm = m.getType(); |
| 145 | if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 146 | map_rect_translate_scale(rect, m, &x, &y); |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 147 | } else { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 148 | map_rect_general(rect, m, &x, &y, nullptr); |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 149 | } |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 150 | return GrQuad(x, y); |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 151 | } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 152 | |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 153 | GrQuad GrQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame^] | 154 | V4f xs, ys; |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 155 | rearrange_sk_to_gr_points(pts, &xs, &ys); |
| 156 | if (matrix.isIdentity()) { |
| 157 | return GrQuad(xs, ys); |
| 158 | } else { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame^] | 159 | V4f mx, my; |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 160 | map_quad_general(xs, ys, matrix, &mx, &my, nullptr); |
| 161 | return GrQuad(mx, my); |
| 162 | } |
| 163 | } |
| 164 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 165 | bool GrQuad::aaHasEffectOnRect() const { |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 166 | return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]); |
| 167 | } |
| 168 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 169 | // Private constructor used by GrQuadList to quickly fill in a quad's values from the channel arrays |
| 170 | GrPerspQuad::GrPerspQuad(const float* xs, const float* ys, const float* ws) { |
| 171 | memcpy(fX, xs, 4 * sizeof(float)); |
| 172 | memcpy(fY, ys, 4 * sizeof(float)); |
| 173 | memcpy(fW, ws, 4 * sizeof(float)); |
| 174 | } |
| 175 | |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 176 | GrPerspQuad GrPerspQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame^] | 177 | V4f x, y, w; |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 178 | SkMatrix::TypeMask tm = m.getType(); |
| 179 | if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
| 180 | map_rect_translate_scale(rect, m, &x, &y); |
| 181 | w = 1.f; |
| 182 | } else { |
| 183 | map_rect_general(rect, m, &x, &y, &w); |
| 184 | } |
| 185 | return GrPerspQuad(x, y, w); |
| 186 | } |
| 187 | |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 188 | GrPerspQuad GrPerspQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame^] | 189 | V4f xs, ys; |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 190 | rearrange_sk_to_gr_points(pts, &xs, &ys); |
| 191 | if (matrix.isIdentity()) { |
| 192 | return GrPerspQuad(xs, ys, 1.f); |
| 193 | } else { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame^] | 194 | V4f mx, my, mw; |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 195 | map_quad_general(xs, ys, matrix, &mx, &my, &mw); |
| 196 | return GrPerspQuad(mx, my, mw); |
| 197 | } |
| 198 | } |
| 199 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 200 | bool GrPerspQuad::aaHasEffectOnRect() const { |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 201 | // If rect, ws must all be 1s so no need to divide |
| 202 | return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]); |
| 203 | } |