Allow 9patches to shrink
Bug #7307304

Change-Id: I1fabf6df99c18c86ab1ec0e1e398a3d6d4098496
diff --git a/libs/hwui/Patch.cpp b/libs/hwui/Patch.cpp
index abc88fa..b3631df 100644
--- a/libs/hwui/Patch.cpp
+++ b/libs/hwui/Patch.cpp
@@ -118,7 +118,10 @@
     const uint32_t yStretchCount = (mYCount + 1) >> 1;
 
     float stretchX = 0.0f;
-    float stretchY = 0.0;
+    float stretchY = 0.0f;
+
+    float rescaleX = 1.0f;
+    float rescaleY = 1.0f;
 
     const float meshWidth = right - left;
 
@@ -129,8 +132,9 @@
         }
         const float xStretchTex = stretchSize;
         const float fixed = bitmapWidth - stretchSize;
-        const float xStretch = right - left - fixed;
+        const float xStretch = fmaxf(right - left - fixed, 0.0f);
         stretchX = xStretch / xStretchTex;
+        rescaleX = fminf(fmaxf(right - left, 0.0f) / fixed, 1.0f);
     }
 
     if (yStretchCount > 0) {
@@ -140,8 +144,9 @@
         }
         const float yStretchTex = stretchSize;
         const float fixed = bitmapHeight - stretchSize;
-        const float yStretch = bottom - top - fixed;
+        const float yStretch = fmaxf(bottom - top - fixed, 0.0f);
         stretchY = yStretch / yStretchTex;
+        rescaleY = fminf(fmaxf(bottom - top, 0.0f) / fixed, 1.0f);
     }
 
     TextureVertex* vertex = mVertices;
@@ -160,7 +165,7 @@
         if (i & 1) {
             y2 = y1 + floorf(segment * stretchY + 0.5f);
         } else {
-            y2 = y1 + segment;
+            y2 = y1 + segment * rescaleY;
         }
 
         float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
@@ -172,7 +177,7 @@
             y1 += i * EXPLODE_GAP;
             y2 += i * EXPLODE_GAP;
 #endif
-            generateRow(vertex, y1, y2, v1, v2, stretchX, right - left,
+            generateRow(vertex, y1, y2, v1, v2, stretchX, rescaleX, right - left,
                     bitmapWidth, quadCount);
 #if DEBUG_EXPLODE_PATCHES
             y2 -= i * EXPLODE_GAP;
@@ -191,7 +196,8 @@
         y1 += mYCount * EXPLODE_GAP;
         y2 += mYCount * EXPLODE_GAP;
 #endif
-        generateRow(vertex, y1, y2, v1, 1.0f, stretchX, right - left, bitmapWidth, quadCount);
+        generateRow(vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, right - left,
+                bitmapWidth, quadCount);
     }
 
     if (verticesCount > 0) {
@@ -212,7 +218,7 @@
 }
 
 void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
-        float stretchX, float width, float bitmapWidth, uint32_t& quadCount) {
+        float stretchX, float rescaleX, float width, float bitmapWidth, uint32_t& quadCount) {
     float previousStepX = 0.0f;
 
     float x1 = 0.0f;
@@ -227,7 +233,7 @@
         if (i & 1) {
             x2 = x1 + floorf(segment * stretchX + 0.5f);
         } else {
-            x2 = x1 + segment;
+            x2 = x1 + segment * rescaleX;
         }
 
         float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
@@ -272,7 +278,7 @@
     if (y2 < 0.0f) y2 = 0.0f;
 
     // Skip degenerate and transparent (empty) quads
-    if ((mColorKey >> oldQuadCount) & 0x1) {
+    if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) {
 #if DEBUG_PATCHES_EMPTY_VERTICES
         PATCH_LOGD("    quad %d (empty)", oldQuadCount);
         PATCH_LOGD("        left,  top    = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
diff --git a/libs/hwui/Patch.h b/libs/hwui/Patch.h
index 28c9048..0518d91 100644
--- a/libs/hwui/Patch.h
+++ b/libs/hwui/Patch.h
@@ -75,8 +75,8 @@
     void copy(const int32_t* yDivs);
 
     void generateRow(TextureVertex*& vertex, float y1, float y2,
-            float v1, float v2, float stretchX, float width, float bitmapWidth,
-            uint32_t& quadCount);
+            float v1, float v2, float stretchX, float rescaleX,
+            float width, float bitmapWidth, uint32_t& quadCount);
     void generateQuad(TextureVertex*& vertex,
             float x1, float y1, float x2, float y2,
             float u1, float v1, float u2, float v2,