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 | |
| 16 | /** |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 17 | * GrQuad is a collection of 4 points which can be used to represent an arbitrary quadrilateral. The |
| 18 | * 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] | 19 | */ |
| 20 | class GrQuad { |
| 21 | public: |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 22 | GrQuad() = default; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 23 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 24 | GrQuad(const GrQuad& that) = default; |
| 25 | |
| 26 | explicit GrQuad(const SkRect& rect) |
| 27 | : fX{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight} |
| 28 | , fY{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom} {} |
| 29 | |
| 30 | /** Sets the quad to the rect as transformed by the matrix. */ |
| 31 | GrQuad(const SkRect&, const SkMatrix&); |
| 32 | |
| 33 | explicit GrQuad(const SkPoint pts[4]) |
| 34 | : fX{pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX} |
| 35 | , fY{pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY} {} |
| 36 | |
| 37 | GrQuad& operator=(const GrQuad& that) = default; |
| 38 | |
| 39 | SkPoint point(int i) const { return {fX[i], fY[i]}; } |
| 40 | |
| 41 | SkRect bounds() const { |
| 42 | auto x = this->x4f(), y = this->y4f(); |
| 43 | return {x.min(), y.min(), x.max(), y.max()}; |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 46 | float x(int i) const { return fX[i]; } |
| 47 | float y(int i) const { return fY[i]; } |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 48 | |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 49 | Sk4f x4f() const { return Sk4f::Load(fX); } |
| 50 | Sk4f y4f() const { return Sk4f::Load(fY); } |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 51 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 52 | private: |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 53 | float fX[4]; |
| 54 | float fY[4]; |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 57 | class GrPerspQuad { |
| 58 | public: |
| 59 | GrPerspQuad() = default; |
| 60 | |
| 61 | GrPerspQuad(const SkRect&, const SkMatrix&); |
| 62 | |
| 63 | GrPerspQuad& operator=(const GrPerspQuad&) = default; |
| 64 | |
| 65 | SkPoint3 point(int i) const { return {fX[i], fY[i], fW[i]}; } |
| 66 | |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame] | 67 | SkRect bounds() const { |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 68 | auto x = this->x4f() * this->iw4f(); |
| 69 | auto y = this->y4f() * this->iw4f(); |
| 70 | return {x.min(), y.min(), x.max(), y.max()}; |
| 71 | } |
| 72 | |
| 73 | float x(int i) const { return fX[i]; } |
| 74 | float y(int i) const { return fY[i]; } |
| 75 | float w(int i) const { return fW[i]; } |
| 76 | float iw(int i) const { return fIW[i]; } |
| 77 | |
| 78 | Sk4f x4f() const { return Sk4f::Load(fX); } |
| 79 | Sk4f y4f() const { return Sk4f::Load(fY); } |
| 80 | Sk4f w4f() const { return Sk4f::Load(fW); } |
| 81 | Sk4f iw4f() const { return Sk4f::Load(fIW); } |
| 82 | |
| 83 | private: |
| 84 | float fX[4]; |
| 85 | float fY[4]; |
| 86 | float fW[4]; |
| 87 | float fIW[4]; // 1/w |
| 88 | }; |
| 89 | |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 90 | #endif |