Skip to content

Commit 38a6424

Browse files
Romain GuyAndroid (Google) Code Review
authored andcommitted
Merge "Allow 9patches to shrink Bug #7307304" into jb-mr1-dev
2 parents bcfc1ca + 41d35ae commit 38a6424

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

libs/hwui/Patch.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
118118
const uint32_t yStretchCount = (mYCount + 1) >> 1;
119119

120120
float stretchX = 0.0f;
121-
float stretchY = 0.0;
121+
float stretchY = 0.0f;
122+
123+
float rescaleX = 1.0f;
124+
float rescaleY = 1.0f;
122125

123126
const float meshWidth = right - left;
124127

@@ -129,8 +132,9 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
129132
}
130133
const float xStretchTex = stretchSize;
131134
const float fixed = bitmapWidth - stretchSize;
132-
const float xStretch = right - left - fixed;
135+
const float xStretch = fmaxf(right - left - fixed, 0.0f);
133136
stretchX = xStretch / xStretchTex;
137+
rescaleX = fminf(fmaxf(right - left, 0.0f) / fixed, 1.0f);
134138
}
135139

136140
if (yStretchCount > 0) {
@@ -140,8 +144,9 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
140144
}
141145
const float yStretchTex = stretchSize;
142146
const float fixed = bitmapHeight - stretchSize;
143-
const float yStretch = bottom - top - fixed;
147+
const float yStretch = fmaxf(bottom - top - fixed, 0.0f);
144148
stretchY = yStretch / yStretchTex;
149+
rescaleY = fminf(fmaxf(bottom - top, 0.0f) / fixed, 1.0f);
145150
}
146151

147152
TextureVertex* vertex = mVertices;
@@ -160,7 +165,7 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
160165
if (i & 1) {
161166
y2 = y1 + floorf(segment * stretchY + 0.5f);
162167
} else {
163-
y2 = y1 + segment;
168+
y2 = y1 + segment * rescaleY;
164169
}
165170

166171
float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
@@ -172,7 +177,7 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
172177
y1 += i * EXPLODE_GAP;
173178
y2 += i * EXPLODE_GAP;
174179
#endif
175-
generateRow(vertex, y1, y2, v1, v2, stretchX, right - left,
180+
generateRow(vertex, y1, y2, v1, v2, stretchX, rescaleX, right - left,
176181
bitmapWidth, quadCount);
177182
#if DEBUG_EXPLODE_PATCHES
178183
y2 -= i * EXPLODE_GAP;
@@ -191,7 +196,8 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
191196
y1 += mYCount * EXPLODE_GAP;
192197
y2 += mYCount * EXPLODE_GAP;
193198
#endif
194-
generateRow(vertex, y1, y2, v1, 1.0f, stretchX, right - left, bitmapWidth, quadCount);
199+
generateRow(vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, right - left,
200+
bitmapWidth, quadCount);
195201
}
196202

197203
if (verticesCount > 0) {
@@ -212,7 +218,7 @@ void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
212218
}
213219

214220
void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
215-
float stretchX, float width, float bitmapWidth, uint32_t& quadCount) {
221+
float stretchX, float rescaleX, float width, float bitmapWidth, uint32_t& quadCount) {
216222
float previousStepX = 0.0f;
217223

218224
float x1 = 0.0f;
@@ -227,7 +233,7 @@ void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, fl
227233
if (i & 1) {
228234
x2 = x1 + floorf(segment * stretchX + 0.5f);
229235
} else {
230-
x2 = x1 + segment;
236+
x2 = x1 + segment * rescaleX;
231237
}
232238

233239
float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
@@ -272,7 +278,7 @@ void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, f
272278
if (y2 < 0.0f) y2 = 0.0f;
273279

274280
// Skip degenerate and transparent (empty) quads
275-
if ((mColorKey >> oldQuadCount) & 0x1) {
281+
if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) {
276282
#if DEBUG_PATCHES_EMPTY_VERTICES
277283
PATCH_LOGD(" quad %d (empty)", oldQuadCount);
278284
PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);

libs/hwui/Patch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ struct Patch {
7575
void copy(const int32_t* yDivs);
7676

7777
void generateRow(TextureVertex*& vertex, float y1, float y2,
78-
float v1, float v2, float stretchX, float width, float bitmapWidth,
79-
uint32_t& quadCount);
78+
float v1, float v2, float stretchX, float rescaleX,
79+
float width, float bitmapWidth, uint32_t& quadCount);
8080
void generateQuad(TextureVertex*& vertex,
8181
float x1, float y1, float x2, float y2,
8282
float u1, float v1, float u2, float v2,

0 commit comments

Comments
 (0)