aboutsummaryrefslogtreecommitdiffhomepage
path: root/gpu/include/GrRect.h
diff options
context:
space:
mode:
authorGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-03-03 13:54:13 +0000
committerGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2011-03-03 13:54:13 +0000
commitd302f1401b3c9aea094804bad4e76de98782cfe8 (patch)
treeb46ec6c4de175842aef051d7b812785dacbd1d73 /gpu/include/GrRect.h
parent1d12b1fd66e5be27fb4769ee09ce4fcd6bcc5979 (diff)
Add support for clipstack to Gr. GrClip is now a list of rects and paths with set operations to combine them. The stencil buffer is used to perform the set operations to put the clip into the stencil buffer. Building Gr's clip from Skia's clipStack is currently disabled due to the fact that Skia's clipStack is relative to the root layer not the current layer. This will be fixed in a subsequent CL.
git-svn-id: http://skia.googlecode.com/svn/trunk@878 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'gpu/include/GrRect.h')
-rw-r--r--gpu/include/GrRect.h107
1 files changed, 81 insertions, 26 deletions
diff --git a/gpu/include/GrRect.h b/gpu/include/GrRect.h
index e98913a57c..96d302f53d 100644
--- a/gpu/include/GrRect.h
+++ b/gpu/include/GrRect.h
@@ -22,7 +22,7 @@
struct GrIRect {
int32_t fLeft, fTop, fRight, fBottom;
-
+
GrIRect() {}
GrIRect(int32_t left, int32_t top, int32_t right, int32_t bottom) {
fLeft = left;
@@ -47,23 +47,22 @@ struct GrIRect {
fRight = x + w;
fBottom = y + h;
}
-
+
void setLTRB(int32_t l, int32_t t, int32_t r, int32_t b) {
fLeft = l;
fTop = t;
fRight = r;
fBottom = b;
}
-
+
/**
* Make the largest representable rectangle
-
*/
void setLargest() {
fLeft = fTop = GR_Int32Min;
fRight = fBottom = GR_Int32Max;
}
-
+
bool quickReject(int l, int t, int r, int b) const {
return l >= fRight || fLeft >= r || t >= fBottom || fTop >= b;
}
@@ -75,10 +74,28 @@ struct GrIRect {
if (fBottom < r.fBottom) fBottom = r.fBottom;
}
+ /**
+ * Sets this rect to the intersection with a clip rect. If there is no
+ * intersection then this rect will be made empty.
+ */
+ void intersectWith(const GrIRect& clipRect) {
+ if (fRight < clipRect.fLeft ||
+ fLeft > clipRect.fRight ||
+ fBottom < clipRect.fTop ||
+ fTop > clipRect.fBottom) {
+ this->setEmpty();
+ } else {
+ fLeft = GrMax(fLeft, clipRect.fLeft);
+ fRight = GrMin(fRight, clipRect.fRight);
+ fTop = GrMax(fTop, clipRect.fTop);
+ fBottom = GrMin(fBottom, clipRect.fBottom);
+ }
+ }
+
friend bool operator==(const GrIRect& a, const GrIRect& b) {
return 0 == memcmp(&a, &b, sizeof(a));
}
-
+
friend bool operator!=(const GrIRect& a, const GrIRect& b) {
return 0 != memcmp(&a, &b, sizeof(a));
}
@@ -91,7 +108,7 @@ struct GrIRect {
return fLeft == x && fTop == y &&
this->width() == w && this->height() == h;
}
-
+
bool contains(const GrIRect& r) const {
return fLeft <= r.fLeft &&
fRight >= r.fRight &&
@@ -102,12 +119,12 @@ struct GrIRect {
struct GrIRect16 {
int16_t fLeft, fTop, fRight, fBottom;
-
+
int width() const { return fRight - fLeft; }
int height() const { return fBottom - fTop; }
int area() const { return this->width() * this->height(); }
bool isEmpty() const { return fLeft >= fRight || fTop >= fBottom; }
-
+
void set(const GrIRect& r) {
fLeft = GrToS16(r.fLeft);
fTop = GrToS16(r.fTop);
@@ -116,12 +133,12 @@ struct GrIRect16 {
}
};
-/**
+/**
* 2D Rect struct
*/
struct GrRect {
GrScalar fLeft, fTop, fRight, fBottom;
-
+
/**
* Uninitialized rectangle.
*/
@@ -158,7 +175,7 @@ struct GrRect {
GrScalar top() const { return fTop; }
GrScalar right() const { return fRight; }
GrScalar bottom() const { return fBottom; }
-
+
GrScalar diagonalLengthSqd() const {
GrScalar w = width();
GrScalar h = height();
@@ -169,18 +186,18 @@ struct GrRect {
// TODO: fixed point sqrt
return GrFloatToScalar(sqrtf(GrScalarToFloat(diagonalLengthSqd())));
}
-
+
/**
* Returns true if the width or height is <= 0
*/
bool isEmpty() const {
return fLeft >= fRight || fTop >= fBottom;
}
-
+
void setEmpty() {
fLeft = fTop = fRight = fBottom = 0;
}
-
+
/**
* returns true if the rectangle is inverted either in x or y
*/
@@ -192,7 +209,7 @@ struct GrRect {
return point.fX >= fLeft && point.fX < fRight &&
point.fY >= fTop && point.fY < fBottom;
}
-
+
/**
* Initialize a rectangle to a point.
* @param pt the point used to initialize the rectangle.
@@ -226,16 +243,16 @@ struct GrRect {
/**
* Make the largest representable rectangle
- * Set the rect to fLeft = fTop = GR_ScalarMin and
+ * Set the rect to fLeft = fTop = GR_ScalarMin and
* fRight = fBottom = GR_ScalarMax.
*/
void setLargest() {
fLeft = fTop = GR_ScalarMin;
fRight = fBottom = GR_ScalarMax;
}
-
+
/**
- Set the rect to fLeft = fTop = GR_ScalarMax and
+ Set the rect to fLeft = fTop = GR_ScalarMax and
fRight = fBottom = GR_ScalarMin.
Useful for initializing a bounding rectangle.
*/
@@ -243,24 +260,24 @@ struct GrRect {
fLeft = fTop = GR_ScalarMax;
fRight = fBottom = GR_ScalarMin;
}
-
- void setLTRB(GrScalar left,
- GrScalar top,
- GrScalar right,
+
+ void setLTRB(GrScalar left,
+ GrScalar top,
+ GrScalar right,
GrScalar bottom) {
fLeft = left;
fTop = top;
fRight = right;
fBottom = bottom;
}
-
+
void setXYWH(GrScalar x, GrScalar y, GrScalar width, GrScalar height) {
fLeft = x;
fTop = y;
fRight = x + width;
fBottom = y + height;
}
-
+
/**
Expand the edges of the rectangle to include a point.
Useful for constructing a bounding rectangle.
@@ -269,12 +286,43 @@ struct GrRect {
void growToInclude(const GrPoint& pt) {
fLeft = GrMin(pt.fX, fLeft);
fRight = GrMax(pt.fX, fRight);
-
+
fTop = GrMin(pt.fY, fTop);
fBottom = GrMax(pt.fY, fBottom);
}
/**
+ * Grows a rect to include another rect.
+ * @param rect the rect to include
+ */
+ void growToInclude(const GrRect& rect) {
+ GrAssert(!rect.isEmpty());
+ fLeft = GrMin(rect.fLeft, fLeft);
+ fRight = GrMax(rect.fRight, fRight);
+
+ fTop = GrMin(rect.fTop, fTop);
+ fBottom = GrMax(rect.fBottom, fBottom);
+ }
+
+ /**
+ * Sets this rect to the intersection with a clip rect. If there is no
+ * intersection then this rect will be made empty.
+ */
+ void intersectWith(const GrRect& clipRect) {
+ if (fRight < clipRect.fLeft ||
+ fLeft > clipRect.fRight ||
+ fBottom < clipRect.fTop ||
+ fTop > clipRect.fBottom) {
+ this->setEmpty();
+ } else {
+ fLeft = GrMax(fLeft, clipRect.fLeft);
+ fRight = GrMin(fRight, clipRect.fRight);
+ fTop = GrMax(fTop, clipRect.fTop);
+ fBottom = GrMin(fBottom, clipRect.fBottom);
+ }
+ }
+
+ /**
* Assigns 4 sequential points in order to construct a counter-clockwise
* triangle fan, given the corners of this rect. Returns the address of
* the next point, treating pts as an array.
@@ -283,6 +331,13 @@ struct GrRect {
pts->setRectFan(fLeft, fTop, fRight, fBottom);
return pts + 4;
}
+
+ bool operator ==(const GrRect& r) const {
+ return fLeft == r.fLeft &&
+ fTop == r.fTop &&
+ fRight == r.fRight &&
+ fBottom == r.fBottom;
+ }
};
#endif