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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkMatrix.h" |
| 12 | #include "include/core/SkPoint.h" |
| 13 | #include "include/core/SkPoint3.h" |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 14 | #include "include/private/SkVx.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 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 19 | /** |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 20 | * GrQuad is a collection of 4 points which can be used to represent an arbitrary quadrilateral. The |
| 21 | * 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] | 22 | */ |
| 23 | class GrQuad { |
| 24 | public: |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 25 | // Quadrilaterals can be classified in several useful ways that assist AA tessellation and other |
| 26 | // analysis when drawing, in particular, knowing if it was originally a rectangle transformed by |
| 27 | // certain types of matrices: |
| 28 | enum class Type { |
| 29 | // The 4 points remain an axis-aligned rectangle; their logical indices may not respect |
| 30 | // TL, BL, TR, BR ordering if the transform was a 90 degre rotation or mirror. |
| 31 | kAxisAligned, |
| 32 | // The 4 points represent a rectangle subjected to a rotation, its corners are right angles. |
| 33 | kRectilinear, |
| 34 | // Arbitrary 2D quadrilateral; may have been a rectangle transformed with skew or some |
| 35 | // clipped polygon. Its w coordinates will all be 1. |
| 36 | kGeneral, |
| 37 | // Even more general-purpose than kGeneral, this allows the w coordinates to be non-unity. |
| 38 | kPerspective, |
| 39 | kLast = kPerspective |
| 40 | }; |
| 41 | static const int kTypeCount = static_cast<int>(Type::kLast) + 1; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 42 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 43 | GrQuad() = default; |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 44 | |
| 45 | explicit GrQuad(const SkRect& rect) |
| 46 | : fX{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight} |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 47 | , fY{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom} |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 48 | , fW{1.f, 1.f, 1.f, 1.f} |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 49 | , fType(Type::kAxisAligned) {} |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 50 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 51 | GrQuad(const skvx::Vec<4, float>& xs, const skvx::Vec<4, float>& ys, Type type) |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 52 | : fType(type) { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 53 | SkASSERT(type != Type::kPerspective); |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 54 | xs.store(fX); |
| 55 | ys.store(fY); |
| 56 | fW[0] = fW[1] = fW[2] = fW[3] = 1.f; |
| 57 | } |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 58 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 59 | GrQuad(const skvx::Vec<4, float>& xs, const skvx::Vec<4, float>& ys, |
| 60 | const skvx::Vec<4, float>& ws, Type type) |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 61 | : fType(type) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 62 | xs.store(fX); |
| 63 | ys.store(fY); |
| 64 | ws.store(fW); |
| 65 | } |
| 66 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 67 | // Copy 4 values from each of the arrays into the quad's components |
| 68 | GrQuad(const float xs[4], const float ys[4], const float ws[4], Type type) |
| 69 | : fType(type) { |
| 70 | memcpy(fX, xs, 4 * sizeof(float)); |
| 71 | memcpy(fY, ys, 4 * sizeof(float)); |
| 72 | memcpy(fW, ws, 4 * sizeof(float)); |
| 73 | } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 74 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 75 | static GrQuad MakeFromRect(const SkRect&, const SkMatrix&); |
| 76 | |
| 77 | // Creates a GrQuad from the quadrilateral 'pts', transformed by the matrix. The input |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 78 | // points array is arranged as per SkRect::toQuad (top-left, top-right, bottom-right, |
| 79 | // bottom-left). The returned instance's point order will still be CCW tri-strip order. |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 80 | static GrQuad MakeFromSkQuad(const SkPoint pts[4], const SkMatrix&); |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 81 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 82 | GrQuad& operator=(const GrQuad&) = default; |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 83 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 84 | SkPoint3 point3(int i) const { return {fX[i], fY[i], fW[i]}; } |
| 85 | |
| 86 | SkPoint point(int i) const { |
| 87 | if (fType == Type::kPerspective) { |
| 88 | return {fX[i] / fW[i], fY[i] / fW[i]}; |
| 89 | } else { |
| 90 | return {fX[i], fY[i]}; |
| 91 | } |
| 92 | } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 93 | |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 94 | SkRect bounds() const { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 95 | auto x = this->x4f(); |
| 96 | auto y = this->y4f(); |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 97 | if (fType == Type::kPerspective) { |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 98 | auto iw = this->iw4f(); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 99 | x *= iw; |
| 100 | y *= iw; |
| 101 | } |
| 102 | |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 103 | return {min(x), min(y), max(x), max(y)}; |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | float x(int i) const { return fX[i]; } |
| 107 | float y(int i) const { return fY[i]; } |
| 108 | float w(int i) const { return fW[i]; } |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 109 | float iw(int i) const { return sk_ieee_float_divide(1.f, fW[i]); } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 110 | |
Michael Ludwig | b3461fa | 2019-04-30 11:50:55 -0400 | [diff] [blame] | 111 | skvx::Vec<4, float> x4f() const { return skvx::Vec<4, float>::Load(fX); } |
| 112 | skvx::Vec<4, float> y4f() const { return skvx::Vec<4, float>::Load(fY); } |
| 113 | skvx::Vec<4, float> w4f() const { return skvx::Vec<4, float>::Load(fW); } |
| 114 | skvx::Vec<4, float> iw4f() const { return 1.f / this->w4f(); } |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 115 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 116 | Type quadType() const { return fType; } |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 117 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 118 | bool hasPerspective() const { return fType == Type::kPerspective; } |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 119 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 120 | // True if anti-aliasing affects this quad. Only valid when quadType == kAxisAligned |
Michael Ludwig | 1f7e438 | 2018-10-19 09:36:57 -0400 | [diff] [blame] | 121 | bool aaHasEffectOnRect() const; |
| 122 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 123 | private: |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 124 | template<typename T> |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 125 | friend class GrQuadListBase; // for access to fX, fY, fW |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 126 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 127 | float fX[4]; |
| 128 | float fY[4]; |
| 129 | float fW[4]; |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 130 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 131 | Type fType; |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 132 | }; |
| 133 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame^] | 134 | // Resolve disagreements between the overall requested AA type and the per-edge quad AA flags. |
| 135 | // Both outAAType and outEdgeFlags will be updated. |
| 136 | void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, |
| 137 | const GrQuad& quad, GrAAType* outAAtype, GrQuadAAFlags* outEdgeFlags); |
| 138 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 139 | #endif |