blob: 675a088f9564be9cf42b2de0826eedf65a213729 [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"
Michael Ludwigc96fc372019-01-08 15:46:15 -050015#include "SkTArray.h"
joshualittae5b2c62015-08-19 08:48:41 -070016
Michael Ludwig6bee7762018-10-19 09:50:36 -040017enum class GrAAType : unsigned;
18enum class GrQuadAAFlags;
19
Michael Ludwig1f7e4382018-10-19 09:36:57 -040020// Rectangles transformed by matrices (view or local) can be classified in three ways:
21// 1. Stays a rectangle - the matrix rectStaysRect() is true, or x(0) == x(1) && x(2) == x(3)
22// and y(0) == y(2) && y(1) == y(3). Or under mirrors, x(0) == x(2) && x(1) == x(3) and
23// y(0) == y(1) && y(2) == y(3).
Michael Ludwigf995c052018-11-26 15:24:29 -050024// 2. Is rectilinear - the matrix does not have skew or perspective, but may rotate (unlike #1)
25// 3. Is a quadrilateral - the matrix does not have perspective, but may rotate or skew, or
Michael Ludwig1f7e4382018-10-19 09:36:57 -040026// ws() == all ones.
Michael Ludwigf995c052018-11-26 15:24:29 -050027// 4. Is a perspective quad - the matrix has perspective, subsuming all previous quad types.
Michael Ludwig1f7e4382018-10-19 09:36:57 -040028enum class GrQuadType {
Michael Ludwigc182b942018-11-16 10:27:51 -050029 kRect,
Michael Ludwigf995c052018-11-26 15:24:29 -050030 kRectilinear,
Michael Ludwigc182b942018-11-16 10:27:51 -050031 kStandard,
32 kPerspective,
33 kLast = kPerspective
Michael Ludwig1f7e4382018-10-19 09:36:57 -040034};
Michael Ludwigc182b942018-11-16 10:27:51 -050035static const int kGrQuadTypeCount = static_cast<int>(GrQuadType::kLast) + 1;
Michael Ludwig1f7e4382018-10-19 09:36:57 -040036
37// If an SkRect is transformed by this matrix, what class of quad is required to represent it. Since
38// quadType() is only provided on Gr[Persp]Quad in debug builds, production code should use this
39// to efficiently determine quad types.
40GrQuadType GrQuadTypeForTransformedRect(const SkMatrix& matrix);
Michael Ludwig97b94422019-04-09 10:42:39 -040041// Perform minimal analysis of 'pts' (which are suitable for MakeFromSkQuad), and determine a
42// quad type that will be as minimally general as possible.
43GrQuadType GrQuadTypeForPoints(const SkPoint pts[4], const SkMatrix& matrix);
Michael Ludwig1f7e4382018-10-19 09:36:57 -040044
Michael Ludwig6bee7762018-10-19 09:50:36 -040045// Resolve disagreements between the overall requested AA type and the per-edge quad AA flags.
Michael Ludwigc182b942018-11-16 10:27:51 -050046// knownQuadType must have come from GrQuadTypeForTransformedRect with the matrix that created the
Michael Ludwig6bee7762018-10-19 09:50:36 -040047// provided quad. Both outAAType and outEdgeFlags will be updated.
48template <typename Q>
49void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags,
50 const Q& quad, GrQuadType knownQuadType,
51 GrAAType* outAAtype, GrQuadAAFlags* outEdgeFlags);
52
joshualittae5b2c62015-08-19 08:48:41 -070053/**
Brian Salomon57caa662017-10-18 12:21:05 +000054 * GrQuad is a collection of 4 points which can be used to represent an arbitrary quadrilateral. The
55 * points make a triangle strip with CCW triangles (top-left, bottom-left, top-right, bottom-right).
joshualittae5b2c62015-08-19 08:48:41 -070056 */
57class GrQuad {
58public:
Brian Salomona33b67c2018-05-17 10:42:14 -040059 GrQuad() = default;
joshualitt8cce8f12015-08-26 06:23:39 -070060
Brian Salomona33b67c2018-05-17 10:42:14 -040061 GrQuad(const GrQuad& that) = default;
62
63 explicit GrQuad(const SkRect& rect)
64 : fX{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight}
65 , fY{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom} {}
66
Michael Ludwige9c57d32019-02-13 13:39:39 -050067 GrQuad(const Sk4f& xs, const Sk4f& ys) {
68 xs.store(fX);
69 ys.store(fY);
70 }
Brian Salomona33b67c2018-05-17 10:42:14 -040071
72 explicit GrQuad(const SkPoint pts[4])
73 : fX{pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX}
74 , fY{pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY} {}
75
Michael Ludwige9c57d32019-02-13 13:39:39 -050076 /** Sets the quad to the rect as transformed by the matrix. */
77 static GrQuad MakeFromRect(const SkRect&, const SkMatrix&);
78
Michael Ludwig009b92e2019-02-15 16:03:53 -050079 // Creates a GrQuad from the quadrilateral 'pts', transformed by the matrix. Unlike the explicit
80 // constructor, the input points array is arranged as per SkRect::toQuad (top-left, top-right,
81 // bottom-right, bottom-left). The returned instance's point order will still be CCW tri-strip
82 // order.
83 static GrQuad MakeFromSkQuad(const SkPoint pts[4], const SkMatrix&);
84
Brian Salomona33b67c2018-05-17 10:42:14 -040085 GrQuad& operator=(const GrQuad& that) = default;
86
87 SkPoint point(int i) const { return {fX[i], fY[i]}; }
88
89 SkRect bounds() const {
90 auto x = this->x4f(), y = this->y4f();
91 return {x.min(), y.min(), x.max(), y.max()};
joshualittae5b2c62015-08-19 08:48:41 -070092 }
93
Brian Salomona33b67c2018-05-17 10:42:14 -040094 float x(int i) const { return fX[i]; }
95 float y(int i) const { return fY[i]; }
joshualittae5b2c62015-08-19 08:48:41 -070096
Brian Salomona33b67c2018-05-17 10:42:14 -040097 Sk4f x4f() const { return Sk4f::Load(fX); }
98 Sk4f y4f() const { return Sk4f::Load(fY); }
joshualitt8cce8f12015-08-26 06:23:39 -070099
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400100 // True if anti-aliasing affects this quad. Requires quadType() == kRect_QuadType
101 bool aaHasEffectOnRect() const;
102
103#ifdef SK_DEBUG
104 GrQuadType quadType() const;
105#endif
106
joshualittae5b2c62015-08-19 08:48:41 -0700107private:
Michael Ludwigc96fc372019-01-08 15:46:15 -0500108 template<typename T>
109 friend class GrQuadListBase;
110
Brian Salomona33b67c2018-05-17 10:42:14 -0400111 float fX[4];
112 float fY[4];
joshualittae5b2c62015-08-19 08:48:41 -0700113};
114
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400115class GrPerspQuad {
116public:
117 GrPerspQuad() = default;
118
Michael Ludwige9c57d32019-02-13 13:39:39 -0500119 explicit GrPerspQuad(const SkRect& rect)
120 : fX{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight}
121 , fY{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom}
122 , fW{1.f, 1.f, 1.f, 1.f} {}
123
124 GrPerspQuad(const Sk4f& xs, const Sk4f& ys) {
125 xs.store(fX);
126 ys.store(fY);
127 fW[0] = fW[1] = fW[2] = fW[3] = 1.f;
128 }
Michael Ludwig009b92e2019-02-15 16:03:53 -0500129
Michael Ludwige9c57d32019-02-13 13:39:39 -0500130 GrPerspQuad(const Sk4f& xs, const Sk4f& ys, const Sk4f& ws) {
131 xs.store(fX);
132 ys.store(fY);
133 ws.store(fW);
134 }
135
136 static GrPerspQuad MakeFromRect(const SkRect&, const SkMatrix&);
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400137
Michael Ludwig009b92e2019-02-15 16:03:53 -0500138 // Creates a GrPerspQuad from the quadrilateral 'pts', transformed by the matrix. The input
139 // points array is arranged as per SkRect::toQuad (top-left, top-right, bottom-right,
140 // bottom-left). The returned instance's point order will still be CCW tri-strip order.
141 static GrPerspQuad MakeFromSkQuad(const SkPoint pts[4], const SkMatrix&);
142
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400143 GrPerspQuad& operator=(const GrPerspQuad&) = default;
144
145 SkPoint3 point(int i) const { return {fX[i], fY[i], fW[i]}; }
146
Michael Ludwigc96fc372019-01-08 15:46:15 -0500147 SkRect bounds(GrQuadType type) const {
148 SkASSERT(this->quadType() <= type);
149
150 Sk4f x = this->x4f();
151 Sk4f y = this->y4f();
152 if (type == GrQuadType::kPerspective) {
153 Sk4f iw = this->iw4f();
154 x *= iw;
155 y *= iw;
156 }
157
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400158 return {x.min(), y.min(), x.max(), y.max()};
159 }
160
161 float x(int i) const { return fX[i]; }
162 float y(int i) const { return fY[i]; }
163 float w(int i) const { return fW[i]; }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500164 float iw(int i) const { return sk_ieee_float_divide(1.f, fW[i]); }
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400165
166 Sk4f x4f() const { return Sk4f::Load(fX); }
167 Sk4f y4f() const { return Sk4f::Load(fY); }
168 Sk4f w4f() const { return Sk4f::Load(fW); }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500169 Sk4f iw4f() const { return this->w4f().invert(); }
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400170
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400171 bool hasPerspective() const { return (w4f() != Sk4f(1.f)).anyTrue(); }
172
173 // True if anti-aliasing affects this quad. Requires quadType() == kRect_QuadType
174 bool aaHasEffectOnRect() const;
175
176#ifdef SK_DEBUG
177 GrQuadType quadType() const;
178#endif
179
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400180private:
Michael Ludwigc96fc372019-01-08 15:46:15 -0500181 template<typename T>
182 friend class GrQuadListBase;
183
184 // Copy 4 values from each of the arrays into the quad's components
185 GrPerspQuad(const float xs[4], const float ys[4], const float ws[4]);
186
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400187 float fX[4];
188 float fY[4];
189 float fW[4];
Michael Ludwigc96fc372019-01-08 15:46:15 -0500190};
191
192// Underlying data used by GrQuadListBase. It is defined outside of GrQuadListBase due to compiler
193// issues related to specializing member types.
194template<typename T>
195struct QuadData {
196 float fX[4];
197 float fY[4];
198 T fMetadata;
199};
200
201template<>
202struct QuadData<void> {
203 float fX[4];
204 float fY[4];
205};
206
207// A dynamic list of (possibly) perspective quads that tracks the most general quad type of all
208// added quads. It avoids storing the 3rd component if the quad type never becomes perspective.
209// Use GrQuadList subclass when only storing quads. Use GrTQuadList subclass when storing quads
210// and per-quad templated metadata (such as color or domain).
211template<typename T>
212class GrQuadListBase {
213public:
214
215 int count() const { return fXYs.count(); }
216
217 GrQuadType quadType() const { return fType; }
218
219 void reserve(int count, GrQuadType forType) {
220 fXYs.reserve(count);
221 if (forType == GrQuadType::kPerspective || fType == GrQuadType::kPerspective) {
222 fWs.reserve(4 * count);
223 }
224 }
225
226 GrPerspQuad operator[] (int i) const {
227 SkASSERT(i < this->count());
228 SkASSERT(i >= 0);
229
230 const QuadData<T>& item = fXYs[i];
231 if (fType == GrQuadType::kPerspective) {
232 // Read the explicit ws
233 return GrPerspQuad(item.fX, item.fY, fWs.begin() + 4 * i);
234 } else {
235 // Ws are implicitly 1s.
236 static constexpr float kNoPerspectiveWs[4] = {1.f, 1.f, 1.f, 1.f};
237 return GrPerspQuad(item.fX, item.fY, kNoPerspectiveWs);
238 }
239 }
240
241 // Subclasses expose push_back(const GrQuad|GrPerspQuad&, GrQuadType, [const T&]), where
242 // the metadata argument is only present in GrTQuadList's push_back definition.
243
244protected:
245 GrQuadListBase() : fType(GrQuadType::kRect) {}
246
247 void concatImpl(const GrQuadListBase<T>& that) {
248 this->upgradeType(that.fType);
249 fXYs.push_back_n(that.fXYs.count(), that.fXYs.begin());
250 if (fType == GrQuadType::kPerspective) {
251 if (that.fType == GrQuadType::kPerspective) {
252 // Copy the other's ws into the end of this list's data
253 fWs.push_back_n(that.fWs.count(), that.fWs.begin());
254 } else {
255 // This list stores ws but the appended list had implicit 1s, so add explicit 1s to
256 // fill out the total list
257 fWs.push_back_n(4 * that.count(), 1.f);
258 }
259 }
260 }
261
262 // Returns the added item data so that its metadata can be initialized if T is not void
263 QuadData<T>& pushBackImpl(const GrQuad& quad, GrQuadType type) {
264 SkASSERT(quad.quadType() <= type);
265
266 this->upgradeType(type);
267 QuadData<T>& item = fXYs.push_back();
268 memcpy(item.fX, quad.fX, 4 * sizeof(float));
269 memcpy(item.fY, quad.fY, 4 * sizeof(float));
270 if (fType == GrQuadType::kPerspective) {
271 fWs.push_back_n(4, 1.f);
272 }
273 return item;
274 }
275
276 QuadData<T>& pushBackImpl(const GrPerspQuad& quad, GrQuadType type) {
277 SkASSERT(quad.quadType() <= type);
278
279 this->upgradeType(type);
280 QuadData<T>& item = fXYs.push_back();
281 memcpy(item.fX, quad.fX, 4 * sizeof(float));
282 memcpy(item.fY, quad.fY, 4 * sizeof(float));
283 if (fType == GrQuadType::kPerspective) {
284 fWs.push_back_n(4, quad.fW);
285 }
286 return item;
287 }
288
289 const QuadData<T>& item(int i) const {
290 return fXYs[i];
291 }
292
293 QuadData<T>& item(int i) {
294 return fXYs[i];
295 }
296
297private:
298 void upgradeType(GrQuadType type) {
299 // Possibly upgrade the overall type tracked by the list
300 if (type > fType) {
301 fType = type;
302 if (type == GrQuadType::kPerspective) {
303 // All existing quads were 2D, so the ws array just needs to be filled with 1s
304 fWs.push_back_n(4 * this->count(), 1.f);
305 }
306 }
307 }
308
309 // Interleaves xs, ys, and per-quad metadata so that all data for a single quad is together
310 // (barring ws, which can be dropped entirely if the quad type allows it).
311 SkSTArray<1, QuadData<T>, true> fXYs;
312 // The w channel is kept separate so that it can remain empty when only dealing with 2D quads.
313 SkTArray<float, true> fWs;
314
315 GrQuadType fType;
316};
317
318// This list only stores the quad data itself.
319class GrQuadList : public GrQuadListBase<void> {
320public:
321 GrQuadList() : INHERITED() {}
322
323 void concat(const GrQuadList& that) {
324 this->concatImpl(that);
325 }
326
327 void push_back(const GrQuad& quad, GrQuadType type) {
328 this->pushBackImpl(quad, type);
329 }
330
331 void push_back(const GrPerspQuad& quad, GrQuadType type) {
332 this->pushBackImpl(quad, type);
333 }
334
335private:
336 typedef GrQuadListBase<void> INHERITED;
337};
338
339// This variant of the list allows simple metadata to be stored per quad as well, such as color
340// or texture domain.
341template<typename T>
342class GrTQuadList : public GrQuadListBase<T> {
343public:
344 GrTQuadList() : INHERITED() {}
345
346 void concat(const GrTQuadList<T>& that) {
347 this->concatImpl(that);
348 }
349
350 // Adding to the list requires metadata
351 void push_back(const GrQuad& quad, GrQuadType type, T&& metadata) {
352 QuadData<T>& item = this->pushBackImpl(quad, type);
353 item.fMetadata = std::move(metadata);
354 }
355
356 void push_back(const GrPerspQuad& quad, GrQuadType type, T&& metadata) {
357 QuadData<T>& item = this->pushBackImpl(quad, type);
358 item.fMetadata = std::move(metadata);
359 }
360
361 // And provide access to the metadata per quad
362 const T& metadata(int i) const {
363 return this->item(i).fMetadata;
364 }
365
366 T& metadata(int i) {
367 return this->item(i).fMetadata;
368 }
369
370private:
371 typedef GrQuadListBase<T> INHERITED;
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400372};
373
joshualittae5b2c62015-08-19 08:48:41 -0700374#endif