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 | |
| 8 | #include "GrQuad.h" |
| 9 | |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 10 | #include "GrTypesPriv.h" |
| 11 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 12 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 13 | // Functions for identifying the quad type from its coordinates, which are kept debug-only since |
| 14 | // production code should rely on the matrix to derive the quad type more efficiently. These are |
| 15 | // useful in asserts that the quad type is as expected. |
| 16 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | |
| 18 | #ifdef SK_DEBUG |
| 19 | // Allow some tolerance from floating point matrix transformations, but SkScalarNearlyEqual doesn't |
| 20 | // support comparing infinity, and coords_form_rect should return true for infinite edges |
| 21 | #define NEARLY_EQUAL(f1, f2) (f1 == f2 || SkScalarNearlyEqual(f1, f2, 1e-5f)) |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame^] | 22 | // Similarly, support infinite rectangles by looking at the sign of infinities |
| 23 | static bool dot_nearly_zero(const SkVector& e1, const SkVector& e2) { |
| 24 | static constexpr auto dot = SkPoint::DotProduct; |
| 25 | static constexpr auto sign = SkScalarSignAsScalar; |
| 26 | |
| 27 | SkScalar dotValue = dot(e1, e2); |
| 28 | if (SkScalarIsNaN(dotValue)) { |
| 29 | // Form vectors from the signs of infinities, and check their dot product |
| 30 | dotValue = dot({sign(e1.fX), sign(e1.fY)}, {sign(e2.fX), sign(e2.fY)}); |
| 31 | } |
| 32 | |
| 33 | // Unfortunately must have a pretty healthy tolerance here or transformed rects that are |
| 34 | // effectively rectilinear will have edge dot products of around .005 |
| 35 | return SkScalarNearlyZero(dotValue, 1e-2f); |
| 36 | } |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 37 | |
| 38 | // This is not the most performance critical function; code using GrQuad should rely on the faster |
| 39 | // quad type from matrix path, so this will only be called as part of SkASSERT. |
| 40 | static bool coords_form_rect(const float xs[4], const float ys[4]) { |
| 41 | return (NEARLY_EQUAL(xs[0], xs[1]) && NEARLY_EQUAL(xs[2], xs[3]) && |
| 42 | NEARLY_EQUAL(ys[0], ys[2]) && NEARLY_EQUAL(ys[1], ys[3])) || |
| 43 | (NEARLY_EQUAL(xs[0], xs[2]) && NEARLY_EQUAL(xs[1], xs[3]) && |
| 44 | NEARLY_EQUAL(ys[0], ys[1]) && NEARLY_EQUAL(ys[2], ys[3])); |
| 45 | } |
| 46 | |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 47 | static bool coords_rectilinear(const float xs[4], const float ys[4]) { |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 48 | SkVector e0{xs[1] - xs[0], ys[1] - ys[0]}; // Connects to e1 and e2(repeat) |
| 49 | SkVector e1{xs[3] - xs[1], ys[3] - ys[1]}; // connects to e0(repeat) and e3 |
| 50 | SkVector e2{xs[0] - xs[2], ys[0] - ys[2]}; // connects to e0 and e3(repeat) |
| 51 | SkVector e3{xs[2] - xs[3], ys[2] - ys[3]}; // connects to e1(repeat) and e2 |
| 52 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame^] | 53 | return dot_nearly_zero(e0, e1) && dot_nearly_zero(e1, e3) && |
| 54 | dot_nearly_zero(e2, e0) && dot_nearly_zero(e3, e2); |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 57 | GrQuadType GrQuad::quadType() const { |
| 58 | // Since GrQuad applies any perspective information at construction time, there's only two |
| 59 | // types to choose from. |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 60 | if (coords_form_rect(fX, fY)) { |
| 61 | return GrQuadType::kRect; |
| 62 | } else if (coords_rectilinear(fX, fY)) { |
| 63 | return GrQuadType::kRectilinear; |
| 64 | } else { |
| 65 | return GrQuadType::kStandard; |
| 66 | } |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | GrQuadType GrPerspQuad::quadType() const { |
| 70 | if (this->hasPerspective()) { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 71 | return GrQuadType::kPerspective; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 72 | } else { |
| 73 | // Rect or standard quad, can ignore w since they are all ones |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 74 | if (coords_form_rect(fX, fY)) { |
| 75 | return GrQuadType::kRect; |
| 76 | } else if (coords_rectilinear(fX, fY)) { |
| 77 | return GrQuadType::kRectilinear; |
| 78 | } else { |
| 79 | return GrQuadType::kStandard; |
| 80 | } |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 86 | |
| 87 | static bool aa_affects_rect(float ql, float qt, float qr, float qb) { |
| 88 | return !SkScalarIsInt(ql) || !SkScalarIsInt(qr) || !SkScalarIsInt(qt) || !SkScalarIsInt(qb); |
| 89 | } |
| 90 | |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 91 | template <typename Q> |
| 92 | void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, |
| 93 | const Q& quad, GrQuadType knownType, |
| 94 | GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) { |
| 95 | // Most cases will keep the requested types unchanged |
| 96 | *outAAType = requestedAAType; |
| 97 | *outEdgeFlags = requestedEdgeFlags; |
| 98 | |
| 99 | switch (requestedAAType) { |
| 100 | // When aa type is coverage, disable AA if the edge configuration doesn't actually need it |
| 101 | case GrAAType::kCoverage: |
| 102 | if (requestedEdgeFlags == GrQuadAAFlags::kNone) { |
| 103 | // Turn off anti-aliasing |
| 104 | *outAAType = GrAAType::kNone; |
| 105 | } else { |
| 106 | // For coverage AA, if the quad is a rect and it lines up with pixel boundaries |
| 107 | // then overall aa and per-edge aa can be completely disabled |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 108 | if (knownType == GrQuadType::kRect && !quad.aaHasEffectOnRect()) { |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 109 | *outAAType = GrAAType::kNone; |
| 110 | *outEdgeFlags = GrQuadAAFlags::kNone; |
| 111 | } |
| 112 | } |
| 113 | break; |
| 114 | // For no or msaa anti aliasing, override the edge flags since edge flags only make sense |
| 115 | // when coverage aa is being used. |
| 116 | case GrAAType::kNone: |
| 117 | *outEdgeFlags = GrQuadAAFlags::kNone; |
| 118 | break; |
| 119 | case GrAAType::kMSAA: |
| 120 | *outEdgeFlags = GrQuadAAFlags::kAll; |
| 121 | break; |
| 122 | case GrAAType::kMixedSamples: |
| 123 | SK_ABORT("Should not use mixed sample AA with edge AA flags"); |
| 124 | break; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | // Instantiate GrResolve... for GrQuad and GrPerspQuad |
| 129 | template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrQuad&, GrQuadType, |
| 130 | GrAAType*, GrQuadAAFlags*); |
| 131 | template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrPerspQuad&, GrQuadType, |
| 132 | GrAAType*, GrQuadAAFlags*); |
| 133 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 134 | GrQuadType GrQuadTypeForTransformedRect(const SkMatrix& matrix) { |
| 135 | if (matrix.rectStaysRect()) { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 136 | return GrQuadType::kRect; |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 137 | } else if (matrix.preservesRightAngles()) { |
| 138 | return GrQuadType::kRectilinear; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 139 | } else if (matrix.hasPerspective()) { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 140 | return GrQuadType::kPerspective; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 141 | } else { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 142 | return GrQuadType::kStandard; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 146 | GrQuad::GrQuad(const SkRect& rect, const SkMatrix& m) { |
| 147 | SkMatrix::TypeMask tm = m.getType(); |
| 148 | if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
| 149 | auto r = Sk4f::Load(&rect); |
| 150 | const Sk4f t(m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()); |
| 151 | if (tm <= SkMatrix::kTranslate_Mask) { |
| 152 | r += t; |
| 153 | } else { |
| 154 | const Sk4f s(m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()); |
| 155 | r = r * s + t; |
| 156 | } |
| 157 | SkNx_shuffle<0, 0, 2, 2>(r).store(fX); |
| 158 | SkNx_shuffle<1, 3, 1, 3>(r).store(fY); |
| 159 | } else { |
| 160 | Sk4f rx(rect.fLeft, rect.fLeft, rect.fRight, rect.fRight); |
| 161 | Sk4f ry(rect.fTop, rect.fBottom, rect.fTop, rect.fBottom); |
| 162 | Sk4f sx(m.getScaleX()); |
| 163 | Sk4f kx(m.getSkewX()); |
| 164 | Sk4f tx(m.getTranslateX()); |
| 165 | Sk4f ky(m.getSkewY()); |
| 166 | Sk4f sy(m.getScaleY()); |
| 167 | Sk4f ty(m.getTranslateY()); |
| 168 | auto x = SkNx_fma(sx, rx, SkNx_fma(kx, ry, tx)); |
| 169 | auto y = SkNx_fma(ky, rx, SkNx_fma(sy, ry, ty)); |
| 170 | if (m.hasPerspective()) { |
| 171 | Sk4f w0(m.getPerspX()); |
| 172 | Sk4f w1(m.getPerspY()); |
| 173 | Sk4f w2(m.get(SkMatrix::kMPersp2)); |
| 174 | auto iw = SkNx_fma(w0, rx, SkNx_fma(w1, ry, w2)).invert(); |
| 175 | x *= iw; |
| 176 | y *= iw; |
| 177 | } |
| 178 | x.store(fX); |
| 179 | y.store(fY); |
| 180 | } |
| 181 | } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 182 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 183 | bool GrQuad::aaHasEffectOnRect() const { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 184 | SkASSERT(this->quadType() == GrQuadType::kRect); |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 185 | return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]); |
| 186 | } |
| 187 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 188 | GrPerspQuad::GrPerspQuad(const SkRect& rect, const SkMatrix& m) { |
| 189 | SkMatrix::TypeMask tm = m.getType(); |
| 190 | if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { |
| 191 | auto r = Sk4f::Load(&rect); |
| 192 | const Sk4f t(m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()); |
| 193 | if (tm <= SkMatrix::kTranslate_Mask) { |
| 194 | r += t; |
| 195 | } else { |
| 196 | const Sk4f s(m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()); |
| 197 | r = r * s + t; |
| 198 | } |
| 199 | SkNx_shuffle<0, 0, 2, 2>(r).store(fX); |
| 200 | SkNx_shuffle<1, 3, 1, 3>(r).store(fY); |
| 201 | fW[0] = fW[1] = fW[2] = fW[3] = 1.f; |
| 202 | fIW[0] = fIW[1] = fIW[2] = fIW[3] = 1.f; |
| 203 | } else { |
| 204 | Sk4f rx(rect.fLeft, rect.fLeft, rect.fRight, rect.fRight); |
| 205 | Sk4f ry(rect.fTop, rect.fBottom, rect.fTop, rect.fBottom); |
| 206 | Sk4f sx(m.getScaleX()); |
| 207 | Sk4f kx(m.getSkewX()); |
| 208 | Sk4f tx(m.getTranslateX()); |
| 209 | Sk4f ky(m.getSkewY()); |
| 210 | Sk4f sy(m.getScaleY()); |
| 211 | Sk4f ty(m.getTranslateY()); |
| 212 | SkNx_fma(sx, rx, SkNx_fma(kx, ry, tx)).store(fX); |
| 213 | SkNx_fma(ky, rx, SkNx_fma(sy, ry, ty)).store(fY); |
| 214 | if (m.hasPerspective()) { |
| 215 | Sk4f w0(m.getPerspX()); |
| 216 | Sk4f w1(m.getPerspY()); |
| 217 | Sk4f w2(m.get(SkMatrix::kMPersp2)); |
| 218 | auto w = SkNx_fma(w0, rx, SkNx_fma(w1, ry, w2)); |
| 219 | w.store(fW); |
| 220 | w.invert().store(fIW); |
| 221 | } else { |
| 222 | fW[0] = fW[1] = fW[2] = fW[3] = 1.f; |
| 223 | fIW[0] = fIW[1] = fIW[2] = fIW[3] = 1.f; |
| 224 | } |
| 225 | } |
| 226 | } |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 227 | |
| 228 | bool GrPerspQuad::aaHasEffectOnRect() const { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 229 | SkASSERT(this->quadType() == GrQuadType::kRect); |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 230 | // If rect, ws must all be 1s so no need to divide |
| 231 | return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]); |
| 232 | } |