Add nine patch type to SkRRect.

BUG=skia:2181

Committed: http://code.google.com/p/skia/source/detail?r=13833

R=robertphillips@google.com, reed@google.com

Author: bsalomon@google.com

Review URL: https://codereview.chromium.org/196343015

git-svn-id: http://skia.googlecode.com/svn/trunk@13839 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkRRect.h b/include/core/SkRRect.h
index c09d2d4..5223ae7 100644
--- a/include/core/SkRRect.h
+++ b/include/core/SkRRect.h
@@ -71,6 +71,14 @@
         //!< the curves) nor a rect (i.e., both radii are non-zero)
         kSimple_Type,
 
+        //!< The RR is non-empty and the two left x radii are equal, the two top
+        //!< y radii are equal, and the same for the right and bottom but it is
+        //!< neither an rect, oval, nor a simple RR. It is called "nine patch"
+        //!< because the centers of the corner ellipses form an axis aligned
+        //!< rect with edges that divide the RR into an 9 rectangular patches:
+        //!< an interior patch, four edge patches, and four corner patches.
+        kNinePatch_Type,
+
         //!< A fully general (non-empty) RR. Some of the x and/or y radii are
         //!< different from the others and there must be one corner where
         //!< both radii are non-zero.
@@ -99,21 +107,11 @@
     inline bool isSimpleCircular() const {
         return this->isSimple() && fRadii[0].fX == fRadii[0].fY;
     }
+    inline bool isNinePatch() const { return kNinePatch_Type == this->getType(); }
     inline bool isComplex() const { return kComplex_Type == this->getType(); }
 
     bool allCornersCircular() const;
 
-    /**
-     * Are both x-radii the same on the two left corners, and similar for the top, right, and
-     * bottom. When this is the case the four ellipse centers form a rectangle.
-     */
-    bool isNinePatch() const {
-        return fRadii[kUpperLeft_Corner].fX == fRadii[kLowerLeft_Corner].fX &&
-               fRadii[kUpperRight_Corner].fX == fRadii[kLowerRight_Corner].fX &&
-               fRadii[kUpperLeft_Corner].fY == fRadii[kUpperRight_Corner].fY &&
-               fRadii[kLowerLeft_Corner].fY == fRadii[kLowerRight_Corner].fY;
-    }
-
     SkScalar width() const { return fRect.width(); }
     SkScalar height() const { return fRect.height(); }
 
@@ -172,6 +170,12 @@
     void setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad);
 
     /**
+     * Initialize the rr with one radius per-side.
+     */
+    void setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
+                      SkScalar rightRad, SkScalar bottomRad);
+
+    /**
      * Initialize the RR with potentially different radii for all four corners.
      */
     void setRectRadii(const SkRect& rect, const SkVector radii[4]);
diff --git a/src/core/SkRRect.cpp b/src/core/SkRRect.cpp
index 915ed75..082646c 100644
--- a/src/core/SkRRect.cpp
+++ b/src/core/SkRRect.cpp
@@ -43,6 +43,61 @@
     SkDEBUGCODE(this->validate();)
 }
 
+void SkRRect::setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad,
+                           SkScalar rightRad, SkScalar bottomRad) {
+    if (rect.isEmpty()) {
+        this->setEmpty();
+        return;
+    }
+
+    leftRad = SkMaxScalar(leftRad, 0);
+    topRad = SkMaxScalar(topRad, 0);
+    rightRad = SkMaxScalar(rightRad, 0);
+    bottomRad = SkMaxScalar(bottomRad, 0);
+
+    SkScalar scale = SK_Scalar1;
+    if (leftRad + rightRad > rect.width()) {
+        scale = SkScalarDiv(rect.width(), leftRad + rightRad);
+    }
+    if (topRad + bottomRad > rect.height()) {
+        scale = SkMinScalar(scale, SkScalarDiv(rect.width(), leftRad + rightRad));
+    }
+
+    if (scale < SK_Scalar1) {
+        leftRad = SkScalarMul(leftRad, scale);
+        topRad = SkScalarMul(topRad, scale);
+        rightRad = SkScalarMul(rightRad, scale);
+        bottomRad = SkScalarMul(bottomRad, scale);
+    }
+
+    if (leftRad == rightRad && topRad == bottomRad) {
+        if (leftRad >= SkScalarHalf(rect.width()) && topRad >= SkScalarHalf(rect.height())) {
+            fType = kOval_Type;
+        } else if (0 == leftRad || 0 == topRad) {
+            // If the left and (by equality check above) right radii are zero then it is a rect.
+            // Same goes for top/bottom.
+            fType = kRect_Type;
+            leftRad = 0;
+            topRad = 0;
+            rightRad = 0;
+            bottomRad = 0;
+        } else {
+            fType = kSimple_Type;
+        }
+    } else {
+        fType = kNinePatch_Type;
+    }
+
+    fRect = rect;
+    fRadii[kUpperLeft_Corner].set(leftRad, topRad);
+    fRadii[kUpperRight_Corner].set(rightRad, topRad);
+    fRadii[kLowerRight_Corner].set(rightRad, bottomRad);
+    fRadii[kLowerLeft_Corner].set(leftRad, bottomRad);
+
+    SkDEBUGCODE(this->validate();)
+}
+
+
 void SkRRect::setRectRadii(const SkRect& rect, const SkVector radii[4]) {
     if (rect.isEmpty()) {
         this->setEmpty();
@@ -200,6 +255,13 @@
            this->checkCornerContainment(rect.fLeft, rect.fBottom);
 }
 
+static bool radii_are_nine_patch(const SkVector radii[4]) {
+    return radii[SkRRect::kUpperLeft_Corner].fX == radii[SkRRect::kLowerLeft_Corner].fX &&
+           radii[SkRRect::kUpperLeft_Corner].fY == radii[SkRRect::kUpperRight_Corner].fY &&
+           radii[SkRRect::kUpperRight_Corner].fX == radii[SkRRect::kLowerRight_Corner].fX &&
+           radii[SkRRect::kLowerLeft_Corner].fY == radii[SkRRect::kLowerRight_Corner].fY;
+}
+
 // There is a simplified version of this method in setRectXY
 void SkRRect::computeType() const {
     SkDEBUGCODE(this->validate();)
@@ -238,7 +300,11 @@
         return;
     }
 
-    fType = kComplex_Type;
+    if (radii_are_nine_patch(fRadii)) {
+        fType = kNinePatch_Type;
+    } else {
+        fType = kComplex_Type;
+    }
 }
 
 static bool matrix_only_scale_and_translate(const SkMatrix& matrix) {
@@ -391,6 +457,7 @@
             allCornersSquare = false;
         }
     }
+    bool patchesOfNine = radii_are_nine_patch(fRadii);
 
     switch (fType) {
         case kEmpty_Type:
@@ -417,9 +484,15 @@
             SkASSERT(!fRect.isEmpty());
             SkASSERT(!allRadiiZero && allRadiiSame && !allCornersSquare);
             break;
+        case kNinePatch_Type:
+            SkASSERT(!fRect.isEmpty());
+            SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare);
+            SkASSERT(patchesOfNine);
+            break;
         case kComplex_Type:
             SkASSERT(!fRect.isEmpty());
             SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare);
+            SkASSERT(!patchesOfNine);
             break;
         case kUnknown_Type:
             // no limits on this
diff --git a/src/effects/SkBlurMaskFilter.cpp b/src/effects/SkBlurMaskFilter.cpp
index a5cdc4f..ff98daf 100644
--- a/src/effects/SkBlurMaskFilter.cpp
+++ b/src/effects/SkBlurMaskFilter.cpp
@@ -288,10 +288,10 @@
             // already have code for rectangles.
             return kUnimplemented_FilterReturn;
 
+        // These three can take advantage of this fast path.
         case SkRRect::kSimple_Type:
-            // Fall through.
+        case SkRRect::kNinePatch_Type:
         case SkRRect::kComplex_Type:
-            // These can take advantage of this fast path.
             break;
     }
 
diff --git a/src/gpu/effects/GrRRectEffect.cpp b/src/gpu/effects/GrRRectEffect.cpp
index 9d83785..61652c1 100644
--- a/src/gpu/effects/GrRRectEffect.cpp
+++ b/src/gpu/effects/GrRRectEffect.cpp
@@ -658,7 +658,7 @@
             return EllipticalRRectEffect::Create(edgeType,
                                                  EllipticalRRectEffect::kSimple_RRectType, rrect);
         }
-    } else if (rrect.isComplex()) {
+    } else if (rrect.isComplex() || rrect.isNinePatch()) {
         // Check for the "tab" cases - two adjacent circular corners and two square corners.
         SkScalar radius = 0;
         cornerFlags = 0;
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
index 4b87458..44f2211 100644
--- a/src/utils/SkLua.cpp
+++ b/src/utils/SkLua.cpp
@@ -1208,6 +1208,7 @@
         case SkRRect::kRect_Type: return "rect";
         case SkRRect::kOval_Type: return "oval";
         case SkRRect::kSimple_Type: return "simple";
+        case SkRRect::kNinePatch_Type: return "nine-patch";
         case SkRRect::kComplex_Type: return "complex";
     }
     SkDEBUGFAIL("never get here");
diff --git a/src/utils/debugger/SkObjectParser.cpp b/src/utils/debugger/SkObjectParser.cpp
index 028300c..a3b2085 100644
--- a/src/utils/debugger/SkObjectParser.cpp
+++ b/src/utils/debugger/SkObjectParser.cpp
@@ -241,6 +241,8 @@
             mRRect->append("oval");
         } else if (rrect.isSimple()) {
             mRRect->append("simple");
+        } else if (rrect.isNinePatch()) {
+            mRRect->append("nine-patch");
         } else {
             SkASSERT(rrect.isComplex());
             mRRect->append("complex");
diff --git a/tests/RoundRectTest.cpp b/tests/RoundRectTest.cpp
index 9b2eecd..0a528a6 100644
--- a/tests/RoundRectTest.cpp
+++ b/tests/RoundRectTest.cpp
@@ -60,6 +60,13 @@
     for (int i = 0; i < 4; ++i) {
         REPORTER_ASSERT(reporter, zeroPt == rr1.radii((SkRRect::Corner) i));
     }
+    SkRRect rr1_2; // construct the same RR using the most general set function
+    SkVector rr1_2_radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
+    rr1_2.setRectRadii(rect, rr1_2_radii);
+    REPORTER_ASSERT(reporter, rr1_2 == rr1 && rr1_2.getType() == rr1.getType());
+    SkRRect rr1_3;  // construct the same RR using the nine patch set function
+    rr1_3.setNinePatch(rect, 0, 0, 0, 0);
+    REPORTER_ASSERT(reporter, rr1_3 == rr1 && rr1_3.getType() == rr1.getType());
 
     //----
     SkPoint halfPoint = { SkScalarHalf(kWidth), SkScalarHalf(kHeight) };
@@ -73,6 +80,14 @@
         REPORTER_ASSERT(reporter,
                         rr2.radii((SkRRect::Corner) i).equalsWithinTolerance(halfPoint));
     }
+    SkRRect rr2_2;  // construct the same RR using the most general set function
+    SkVector rr2_2_radii[4] = { { halfPoint.fX, halfPoint.fY }, { halfPoint.fX, halfPoint.fY },
+                                { halfPoint.fX, halfPoint.fY }, { halfPoint.fX, halfPoint.fY } };
+    rr2_2.setRectRadii(rect, rr2_2_radii);
+    REPORTER_ASSERT(reporter, rr2_2 == rr2 && rr2_2.getType() == rr2.getType());
+    SkRRect rr2_3;  // construct the same RR using the nine patch set function
+    rr2_3.setNinePatch(rect, halfPoint.fX, halfPoint.fY, halfPoint.fX, halfPoint.fY);
+    REPORTER_ASSERT(reporter, rr2_3 == rr2 && rr2_3.getType() == rr2.getType());
 
     //----
     SkPoint p = { 5, 5 };
@@ -85,19 +100,33 @@
     for (int i = 0; i < 4; ++i) {
         REPORTER_ASSERT(reporter, p == rr3.radii((SkRRect::Corner) i));
     }
+    SkRRect rr3_2; // construct the same RR using the most general set function
+    SkVector rr3_2_radii[4] = { { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 } };
+    rr3_2.setRectRadii(rect, rr3_2_radii);
+    REPORTER_ASSERT(reporter, rr3_2 == rr3 && rr3_2.getType() == rr3.getType());
+    SkRRect rr3_3;  // construct the same RR using the nine patch set function
+    rr3_3.setNinePatch(rect, 5, 5, 5, 5);
+    REPORTER_ASSERT(reporter, rr3_3 == rr3 && rr3_3.getType() == rr3.getType());
 
     //----
-    SkPoint radii[4] = { { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 } };
+    SkRect ninePatchRadii = { 10, 9, 8, 7 };
 
     SkRRect rr4;
-    rr4.setRectRadii(rect, radii);
+    rr4.setNinePatch(rect, ninePatchRadii.fLeft, ninePatchRadii.fTop, ninePatchRadii.fRight,
+                     ninePatchRadii.fBottom);
 
-    REPORTER_ASSERT(reporter, SkRRect::kSimple_Type == rr4.type());
+    REPORTER_ASSERT(reporter, SkRRect::kNinePatch_Type == rr4.type());
     REPORTER_ASSERT(reporter, rr4.rect() == rect);
 
+    SkPoint rquad[4];
+    ninePatchRadii.toQuad(rquad);
     for (int i = 0; i < 4; ++i) {
-        REPORTER_ASSERT(reporter, radii[i] == rr4.radii((SkRRect::Corner) i));
+        REPORTER_ASSERT(reporter, rquad[i] == rr4.radii((SkRRect::Corner) i));
     }
+    SkRRect rr4_2; // construct the same RR using the most general set function
+    SkVector rr4_2_radii[4] = { { 10, 9 }, { 8, 9 }, {8, 7 }, { 10, 7 } };
+    rr4_2.setRectRadii(rect, rr4_2_radii);
+    REPORTER_ASSERT(reporter, rr4_2 == rr4 && rr4_2.getType() == rr4.getType());
 
     //----
     SkPoint radii2[4] = { { 0, 0 }, { 0, 0 }, { 50, 50 }, { 20, 50 } };
@@ -114,7 +143,7 @@
 
     // Test out == & !=
     REPORTER_ASSERT(reporter, empty != rr3);
-    REPORTER_ASSERT(reporter, rr3 == rr4);
+    REPORTER_ASSERT(reporter, rr3 != rr4);
     REPORTER_ASSERT(reporter, rr4 != rr5);
 }