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 
11 #include "SkPoint.h"
12 #include "SkMatrix.h"
13 #include "SkMatrixPriv.h"
14 
15 /**
16  * GrQuad is a collection of 4 points which can be used to represent an arbitrary quadrilateral. The
17  * points make a triangle strip with CCW triangles (top-left, bottom-left, top-right, bottom-right).
18  */
19 class GrQuad {
20 public:
21     GrQuad() {}
22 
23     GrQuad(const GrQuad& that) {
24         *this = that;
25     }
26 
27     explicit GrQuad(const SkRect& rect) {
28         this->set(rect);
29     }
30 
31     void set(const SkRect& rect) {
32         SkPointPriv::SetRectTriStrip(fPoints, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
33                 sizeof(SkPoint));
34     }
35 
36     void map(const SkMatrix& matrix) {
37         matrix.mapPoints(fPoints, kNumPoints);
38     }
39 
40     void setFromMappedRect(const SkRect& rect, const SkMatrix& matrix) {
41         SkMatrixPriv::SetMappedRectTriStrip(matrix, rect, fPoints);
42     }
43 
44     const GrQuad& operator=(const GrQuad& that) {
45         memcpy(fPoints, that.fPoints, sizeof(SkPoint) * kNumPoints);
46         return *this;
47     }
48 
49     SkPoint* points() {
50         return fPoints;
51     }
52 
53     const SkPoint* points() const {
54         return fPoints;
55     }
56 
57     const SkPoint& point(int i) const {
58         SkASSERT(i < kNumPoints);
59         return fPoints[i];
60     }
61 
62 private:
63     static const int kNumPoints = 4;
64     SkPoint fPoints[kNumPoints];
65 };
66 
67 #endif
68