joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | #ifndef GrQuad_DEFINED |
| 9 | #define GrQuad_DEFINED |
| 10 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 11 | #include "SkMatrix.h" |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 12 | #include "SkNx.h" |
| 13 | #include "SkPoint.h" |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 14 | #include "SkPoint3.h" |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 15 | |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 16 | enum class GrAAType : unsigned; |
| 17 | enum class GrQuadAAFlags; |
| 18 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 19 | // Rectangles transformed by matrices (view or local) can be classified in three ways: |
| 20 | // 1. Stays a rectangle - the matrix rectStaysRect() is true, or x(0) == x(1) && x(2) == x(3) |
| 21 | // and y(0) == y(2) && y(1) == y(3). Or under mirrors, x(0) == x(2) && x(1) == x(3) and |
| 22 | // y(0) == y(1) && y(2) == y(3). |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame^] | 23 | // 2. Is rectilinear - the matrix does not have skew or perspective, but may rotate (unlike #1) |
| 24 | // 3. Is a quadrilateral - the matrix does not have perspective, but may rotate or skew, or |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 25 | // ws() == all ones. |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame^] | 26 | // 4. Is a perspective quad - the matrix has perspective, subsuming all previous quad types. |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 27 | enum class GrQuadType { |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 28 | kRect, |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame^] | 29 | kRectilinear, |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 30 | kStandard, |
| 31 | kPerspective, |
| 32 | kLast = kPerspective |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 33 | }; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 34 | static const int kGrQuadTypeCount = static_cast<int>(GrQuadType::kLast) + 1; |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 35 | |
| 36 | // If an SkRect is transformed by this matrix, what class of quad is required to represent it. Since |
| 37 | // quadType() is only provided on Gr[Persp]Quad in debug builds, production code should use this |
| 38 | // to efficiently determine quad types. |
| 39 | GrQuadType GrQuadTypeForTransformedRect(const SkMatrix& matrix); |
| 40 | |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 41 | // Resolve disagreements between the overall requested AA type and the per-edge quad AA flags. |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 42 | // knownQuadType must have come from GrQuadTypeForTransformedRect with the matrix that created the |
Michael Ludwig | 6bee776 | 2018-10-19 09:50:36 -0400 | [diff] [blame] | 43 | // provided quad. Both outAAType and outEdgeFlags will be updated. |
| 44 | template <typename Q> |
| 45 | void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, |
| 46 | const Q& quad, GrQuadType knownQuadType, |
| 47 | GrAAType* outAAtype, GrQuadAAFlags* outEdgeFlags); |
| 48 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 49 | /** |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 50 | * GrQuad is a collection of 4 points which can be used to represent an arbitrary quadrilateral. The |
| 51 | * points make a triangle strip with CCW triangles (top-left, bottom-left, top-right, bottom-right). |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 52 | */ |
| 53 | class GrQuad { |
| 54 | public: |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 55 | GrQuad() = default; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 56 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 57 | GrQuad(const GrQuad& that) = default; |
| 58 | |
| 59 | explicit GrQuad(const SkRect& rect) |
| 60 | : fX{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight} |
| 61 | , fY{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom} {} |
| 62 | |
| 63 | /** Sets the quad to the rect as transformed by the matrix. */ |
| 64 | GrQuad(const SkRect&, const SkMatrix&); |
| 65 | |
| 66 | explicit GrQuad(const SkPoint pts[4]) |
| 67 | : fX{pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX} |
| 68 | , fY{pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY} {} |
| 69 | |
| 70 | GrQuad& operator=(const GrQuad& that) = default; |
| 71 | |
| 72 | SkPoint point(int i) const { return {fX[i], fY[i]}; } |
| 73 | |
| 74 | SkRect bounds() const { |
| 75 | auto x = this->x4f(), y = this->y4f(); |
| 76 | return {x.min(), y.min(), x.max(), y.max()}; |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 79 | float x(int i) const { return fX[i]; } |
| 80 | float y(int i) const { return fY[i]; } |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 81 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 82 | Sk4f x4f() const { return Sk4f::Load(fX); } |
| 83 | Sk4f y4f() const { return Sk4f::Load(fY); } |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 84 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 85 | // True if anti-aliasing affects this quad. Requires quadType() == kRect_QuadType |
| 86 | bool aaHasEffectOnRect() const; |
| 87 | |
| 88 | #ifdef SK_DEBUG |
| 89 | GrQuadType quadType() const; |
| 90 | #endif |
| 91 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 92 | private: |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 93 | float fX[4]; |
| 94 | float fY[4]; |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 97 | class GrPerspQuad { |
| 98 | public: |
| 99 | GrPerspQuad() = default; |
| 100 | |
| 101 | GrPerspQuad(const SkRect&, const SkMatrix&); |
| 102 | |
| 103 | GrPerspQuad& operator=(const GrPerspQuad&) = default; |
| 104 | |
| 105 | SkPoint3 point(int i) const { return {fX[i], fY[i], fW[i]}; } |
| 106 | |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 107 | SkRect bounds() const { |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 108 | auto x = this->x4f() * this->iw4f(); |
| 109 | auto y = this->y4f() * this->iw4f(); |
| 110 | return {x.min(), y.min(), x.max(), y.max()}; |
| 111 | } |
| 112 | |
| 113 | float x(int i) const { return fX[i]; } |
| 114 | float y(int i) const { return fY[i]; } |
| 115 | float w(int i) const { return fW[i]; } |
| 116 | float iw(int i) const { return fIW[i]; } |
| 117 | |
| 118 | Sk4f x4f() const { return Sk4f::Load(fX); } |
| 119 | Sk4f y4f() const { return Sk4f::Load(fY); } |
| 120 | Sk4f w4f() const { return Sk4f::Load(fW); } |
| 121 | Sk4f iw4f() const { return Sk4f::Load(fIW); } |
| 122 | |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 123 | bool hasPerspective() const { return (w4f() != Sk4f(1.f)).anyTrue(); } |
| 124 | |
| 125 | // True if anti-aliasing affects this quad. Requires quadType() == kRect_QuadType |
| 126 | bool aaHasEffectOnRect() const; |
| 127 | |
| 128 | #ifdef SK_DEBUG |
| 129 | GrQuadType quadType() const; |
| 130 | #endif |
| 131 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 132 | private: |
| 133 | float fX[4]; |
| 134 | float fY[4]; |
| 135 | float fW[4]; |
| 136 | float fIW[4]; // 1/w |
| 137 | }; |
| 138 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 139 | #endif |