Skip to content

Commit 4e525a6

Browse files
author
Romain Guy
committed
Tentative fix for mysteriously recycled bitmap
This code should not be triggered with scale == 1.0f because of the density comparisons above though. Change-Id: I9e39e3769a3b6550c97df3b213457947ec1f554b
1 parent e1a409b commit 4e525a6

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

graphics/java/android/graphics/BitmapFactory.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,16 @@ private static Bitmap finishDecode(Bitmap bm, Rect outPadding, Options opts) {
518518
byte[] np = bm.getNinePatchChunk();
519519
final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np);
520520
if (opts.inScaled || isNinePatch) {
521-
float scale = targetDensity / (float)density;
522-
// TODO: This is very inefficient and should be done in native by Skia
523-
final Bitmap oldBitmap = bm;
524-
bm = Bitmap.createScaledBitmap(oldBitmap, (int) (bm.getWidth() * scale + 0.5f),
525-
(int) (bm.getHeight() * scale + 0.5f), true);
526-
oldBitmap.recycle();
521+
float scale = targetDensity / (float) density;
522+
if (scale != 1.0f) {
523+
final Bitmap oldBitmap = bm;
524+
bm = Bitmap.createScaledBitmap(oldBitmap, (int) (bm.getWidth() * scale + 0.5f),
525+
(int) (bm.getHeight() * scale + 0.5f), true);
526+
if (bm != oldBitmap) oldBitmap.recycle();
527+
}
527528

528529
if (isNinePatch) {
529-
np = nativeScaleNinePatch(np, scale, outPadding);
530+
if (scale != 1.0f) np = nativeScaleNinePatch(np, scale, outPadding);
530531
bm.setNinePatchChunk(np);
531532
}
532533
bm.setDensity(targetDensity);

0 commit comments

Comments
 (0)