blob: 4a104aa94ce178310c63d930fa7093abfa59d58a [file] [log] [blame]
joshualittae5b2c62015-08-19 08:48:41 -07001/*
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
joshualittae5b2c62015-08-19 08:48:41 -070011#include "SkMatrix.h"
Brian Salomona33b67c2018-05-17 10:42:14 -040012#include "SkNx.h"
13#include "SkPoint.h"
Brian Salomonbe3c1d22018-05-21 12:54:39 -040014#include "SkPoint3.h"
joshualittae5b2c62015-08-19 08:48:41 -070015
16/**
Brian Salomon57caa662017-10-18 12:21:05 +000017 * 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).
joshualittae5b2c62015-08-19 08:48:41 -070019 */
20class GrQuad {
21public:
Brian Salomona33b67c2018-05-17 10:42:14 -040022 GrQuad() = default;
joshualitt8cce8f12015-08-26 06:23:39 -070023
Brian Salomona33b67c2018-05-17 10:42:14 -040024 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()};
joshualittae5b2c62015-08-19 08:48:41 -070044 }
45
Brian Salomona33b67c2018-05-17 10:42:14 -040046 float x(int i) const { return fX[i]; }
47 float y(int i) const { return fY[i]; }
joshualittae5b2c62015-08-19 08:48:41 -070048
Brian Salomona33b67c2018-05-17 10:42:14 -040049 Sk4f x4f() const { return Sk4f::Load(fX); }
50 Sk4f y4f() const { return Sk4f::Load(fY); }
joshualitt8cce8f12015-08-26 06:23:39 -070051
joshualittae5b2c62015-08-19 08:48:41 -070052private:
Brian Salomona33b67c2018-05-17 10:42:14 -040053 float fX[4];
54 float fY[4];
joshualittae5b2c62015-08-19 08:48:41 -070055};
56
Brian Salomonbe3c1d22018-05-21 12:54:39 -040057class GrPerspQuad {
58public:
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 Salomonb80ffee2018-05-23 16:39:39 -040067 SkRect bounds() const {
Brian Salomonbe3c1d22018-05-21 12:54:39 -040068 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
83private:
84 float fX[4];
85 float fY[4];
86 float fW[4];
87 float fIW[4]; // 1/w
88};
89
joshualittae5b2c62015-08-19 08:48:41 -070090#endif