Share code with SkRect

http://codereview.appspot.com/4523046/



git-svn-id: http://skia.googlecode.com/svn/trunk@1277 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/GrClip.cpp b/gpu/src/GrClip.cpp
index 425ad27..2d1680c 100644
--- a/gpu/src/GrClip.cpp
+++ b/gpu/src/GrClip.cpp
@@ -87,6 +87,12 @@
     }
 }
 
+static void intersectWith(SkRect* dst, const SkRect& src) {
+    if (!dst->intersect(src)) {
+        dst->setEmpty();
+    }
+}
+
 void GrClip::setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty,
                              const GrRect* conservativeBounds) {
     fList.reset();
@@ -118,7 +124,7 @@
                                 rectCount = 1;
                                 fList.pop_back();
                                 GrAssert(kRect_ClipType == fList.back().fType);
-                                fList.back().fRect.intersectWith(e.fRect);
+                                intersectWith(&fList.back().fRect, e.fRect);
                             }
                         } else {
                             isectRectValid = false;
diff --git a/gpu/src/GrContext.cpp b/gpu/src/GrContext.cpp
index ff3119a..8461187 100644
--- a/gpu/src/GrContext.cpp
+++ b/gpu/src/GrContext.cpp
@@ -550,7 +550,7 @@
     // clip gets applied in second pass
     target->disableState(GrDrawTarget::kClip_StateBit);
 
-    GrIRect clear(0, 0, scale * boundW, scale * boundH);
+    GrIRect clear = SkIRect::MakeWH(scale * boundW, scale * boundH);
     target->clear(&clear, 0x0);
 
     return true;
@@ -590,15 +590,14 @@
                          scale * GR_Scalar1 / src->height());
         sampler.setMatrix(sampleM);
         target->setSamplerState(kOffscreenStage, sampler);
-        GrRect rect(0, 0,
-                    scale * boundRect.width(),
-                    scale * boundRect.height());
+        GrRect rect = SkRect::MakeWH(scale * boundRect.width(),
+                                     scale * boundRect.height());
         target->drawSimpleRect(rect, NULL, 1 << kOffscreenStage);
         
         src = record->fEntry1->texture();
     } else if (OffscreenRecord::kFSAA_Downsample == record->fDownsample) {
         scale = 1;
-        GrIRect rect(0, 0, boundRect.width(), boundRect.height());
+        GrIRect rect = SkIRect::MakeWH(boundRect.width(), boundRect.height());
         src->asRenderTarget()->overrideResolveRect(rect);
     } else {
         GrAssert(OffscreenRecord::k4x4SinglePass_Downsample == 
@@ -624,8 +623,9 @@
     sampler.preConcatMatrix(sampleM);
     target->setSamplerState(kOffscreenStage, sampler);
 
-    GrRect dstRect(boundRect);
+    GrRect dstRect;
     int stages = (1 << kOffscreenStage) | (NULL == paint.getTexture() ? 0 : 1);
+    dstRect.set(boundRect);
     target->drawSimpleRect(dstRect, NULL, stages);
 
     this->unlockTexture(record->fEntry0);
@@ -840,6 +840,14 @@
                         0, 0, 16, aaStrokeRectIndexCount());
 }
 
+/**
+ * Returns true if the rects edges are integer-aligned.
+ */
+static bool isIRect(const GrRect& r) {
+    return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) && 
+           GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
+}
+
 static bool apply_aa_to_rect(GrDrawTarget* target,
                              GrGpu* gpu,
                              const GrPaint& paint,
@@ -884,7 +892,7 @@
     devRect->sort();
 
     if (width < 0) {
-        return !devRect->isIRect();
+        return !isIRect(*devRect);
     } else {
         return true;
     }
@@ -1163,13 +1171,12 @@
         bool needsStencil = pr->requiresStencilPass(target, path, fill);
 
         // compute bounds as intersection of rt size, clip, and path
-        GrIRect bound(0, 0, 
-                      target->getRenderTarget()->width(), 
-                      target->getRenderTarget()->height());
+        GrIRect bound = SkIRect::MakeWH(target->getRenderTarget()->width(), 
+                                        target->getRenderTarget()->height());
         if (target->getClip().hasConservativeBounds()) {
             GrIRect clipIBounds;
             target->getClip().getConservativeBounds().roundOut(&clipIBounds);
-            if (!bound.intersectWith(clipIBounds)) {
+            if (!bound.intersect(clipIBounds)) {
                 return;
             }
         }
@@ -1178,7 +1185,7 @@
             GrIRect pathIBounds;
             target->getViewMatrix().mapRect(&pathBounds, pathBounds);
             pathBounds.roundOut(&pathIBounds);
-            if (!bound.intersectWith(pathIBounds)) {
+            if (!bound.intersect(pathIBounds)) {
                 return;
             }
         }
diff --git a/gpu/src/GrGpu.cpp b/gpu/src/GrGpu.cpp
index 66105db..9950afd 100644
--- a/gpu/src/GrGpu.cpp
+++ b/gpu/src/GrGpu.cpp
@@ -399,7 +399,9 @@
                        GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
         if (fClip.hasConservativeBounds()) {
             bounds = fClip.getConservativeBounds();
-            bounds.intersectWith(rtRect);
+            if (!bounds.intersect(rtRect)) {
+                bounds.setEmpty();
+            }
         } else {
             bounds = rtRect;
         }
diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp
index c63c766..e8c7afb 100644
--- a/gpu/src/GrGpuGL.cpp
+++ b/gpu/src/GrGpuGL.cpp
@@ -1175,10 +1175,9 @@
     if (NULL != rect) {
         // flushScissor expects rect to be clipped to the target.
         r = *rect;
-        GrIRect rtRect(0, 0, 
-                       fCurrDrawState.fRenderTarget->width(),
-                       fCurrDrawState.fRenderTarget->height());
-        if (r.intersectWith(rtRect)) {
+        GrIRect rtRect = SkIRect::MakeWH(fCurrDrawState.fRenderTarget->width(),
+                                         fCurrDrawState.fRenderTarget->height());
+        if (r.intersect(rtRect)) {
             rect = &r;
         } else {
             return;
diff --git a/gpu/src/GrMatrix.cpp b/gpu/src/GrMatrix.cpp
index b169d19..c88c6e1 100644
--- a/gpu/src/GrMatrix.cpp
+++ b/gpu/src/GrMatrix.cpp
@@ -717,33 +717,3 @@
     return count;
 }
 
-///////////////////////////////////////////////////////////////////////////////
-#include "GrRect.h"
-
-void GrRect::setBounds(const GrPoint pts[], int count) {
-    if (count <= 0) {
-        this->setEmpty();
-    } else {
-        GrScalar L, R, T, B;
-        L = R = pts[0].fX;
-        T = B = pts[0].fY;
-        for (int i = 1; i < count; i++) {
-            GrScalar x = pts[i].fX;
-            GrScalar y = pts[i].fY;
-            if (x < L) {
-                L = x;
-            } else if (x > R) {
-                R = x;
-            }
-            if (y < T) {
-                T = y;
-            } else if (y > B) {
-                B = y;
-            }
-        }
-        this->setLTRB(L, T, R, B);
-    }
-}
-
-
-
diff --git a/gpu/src/GrPath.cpp b/gpu/src/GrPath.cpp
index 3687b80..aa89d37 100644
--- a/gpu/src/GrPath.cpp
+++ b/gpu/src/GrPath.cpp
@@ -104,7 +104,7 @@
         iter->offset(tx, ty);
         ++iter;
     }
-    fConservativeBounds.translate(tx, ty);
+    fConservativeBounds.offset(tx, ty);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -221,7 +221,7 @@
         }
         int n = NumPathCmdPoints(cmd);
         for (int i = 0; i < n; ++i) {
-            fConservativeBounds.growToInclude(pts[i]);
+            fConservativeBounds.growToInclude(pts[i].fX, pts[i].fY);
         }
         if (0 == subPathPts && n > 0) {
             previousPt = pts[0];
@@ -423,6 +423,13 @@
     this->rewind();
 }
 
+#ifdef SK_DEBUG
+static bool containsInclusive(const GrRect& rect, const GrPoint& point) {
+    return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
+            point.fY >= rect.fTop && point.fY <= rect.fBottom;
+}
+#endif
+
 GrPathCmd GrPath::Iter::next(GrPoint points[]) {
     if (fCmdIndex == fPath->fCmds.count()) {
         GrAssert(fPtIndex == fPath->fPts.count());
@@ -441,7 +448,7 @@
             }
             fLastPt = srcPts[0];
             GrAssert(fPtIndex <= fPath->fPts.count() + 1);
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[0]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[0]));
             fPtIndex += 1;
             break;
         case kLine_PathCmd:
@@ -451,7 +458,7 @@
             }
             fLastPt = srcPts[0];
             GrAssert(fPtIndex <= fPath->fPts.count() + 1);
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[0]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[0]));
             fPtIndex += 1;
             break;
         case kQuadratic_PathCmd:
@@ -462,8 +469,8 @@
             }
             fLastPt = srcPts[1];
             GrAssert(fPtIndex <= fPath->fPts.count() + 2);
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[0]));
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[1]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[0]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[1]));
             fPtIndex += 2;
             break;
         case kCubic_PathCmd:
@@ -475,9 +482,9 @@
             }
             fLastPt = srcPts[2];
             GrAssert(fPtIndex <= fPath->fPts.count() + 3);
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[0]));
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[1]));
-            GrAssert(fPath->getConservativeBounds().containsInclusive(srcPts[2]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[0]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[1]));
+            GrAssert(containsInclusive(fPath->getConservativeBounds(), srcPts[2]));
             fPtIndex += 3;
             break;
         case kClose_PathCmd:
diff --git a/gpu/src/GrTexture.cpp b/gpu/src/GrTexture.cpp
index 8ee531c..8df9c9e 100644
--- a/gpu/src/GrTexture.cpp
+++ b/gpu/src/GrTexture.cpp
@@ -32,8 +32,10 @@
 void GrRenderTarget::flagAsNeedingResolve(const GrIRect* rect) {
     if (kCanResolve_ResolveType == getResolveType()) {
         if (NULL != rect) {
-            fResolveRect.growToInclude(*rect);
-            fResolveRect.intersectWith(0, 0, this->width(), this->height());
+            fResolveRect.join(*rect);
+            if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
+                fResolveRect.setEmpty();
+            }
         } else {
             fResolveRect.setLTRB(0, 0, this->width(), this->height());
         }
@@ -45,7 +47,7 @@
     if (fResolveRect.isEmpty()) {
         fResolveRect.setLargestInverted();
     } else {
-        if (!fResolveRect.intersectWith(0, 0, this->width(), this->height())) {
+        if (!fResolveRect.intersect(0, 0, this->width(), this->height())) {
             fResolveRect.setLargestInverted();
         }
     }