diff options
| author | 2018-01-24 15:57:44 +0000 | |
|---|---|---|
| committer | 2018-01-24 15:57:44 +0000 | |
| commit | 0c60f4357feebd83b7553640c125d0f8ef9c5eda (patch) | |
| tree | d25aa21679a46e81d664e9afe0f089c4d69f6e59 | |
| parent | 08a45549af12d725b8df34f504bcc876b1a3cd28 (diff) | |
| parent | e7addfc99909b8e304f5e3ec3bda4cd938197fc2 (diff) | |
Merge "Revert "Use ImageDecoder for BitmapDrawable""
| -rw-r--r-- | graphics/java/android/graphics/drawable/BitmapDrawable.java | 85 | ||||
| -rw-r--r-- | graphics/java/android/graphics/drawable/Drawable.java | 43 |
2 files changed, 31 insertions, 97 deletions
diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java index 3a5f7b73e196..e3740e3cf284 100644 --- a/graphics/java/android/graphics/drawable/BitmapDrawable.java +++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java @@ -27,7 +27,6 @@ import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; -import android.graphics.ImageDecoder; import android.graphics.Insets; import android.graphics.Matrix; import android.graphics.Outline; @@ -50,7 +49,6 @@ import com.android.internal.R; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -113,7 +111,7 @@ public class BitmapDrawable extends Drawable { */ @Deprecated public BitmapDrawable() { - init(new BitmapState((Bitmap) null), null); + mBitmapState = new BitmapState((Bitmap) null); } /** @@ -126,7 +124,8 @@ public class BitmapDrawable extends Drawable { @SuppressWarnings("unused") @Deprecated public BitmapDrawable(Resources res) { - init(new BitmapState((Bitmap) null), res); + mBitmapState = new BitmapState((Bitmap) null); + mBitmapState.mTargetDensity = mTargetDensity; } /** @@ -136,7 +135,7 @@ public class BitmapDrawable extends Drawable { */ @Deprecated public BitmapDrawable(Bitmap bitmap) { - init(new BitmapState(bitmap), null); + this(new BitmapState(bitmap), null); } /** @@ -144,7 +143,8 @@ public class BitmapDrawable extends Drawable { * the display metrics of the resources. */ public BitmapDrawable(Resources res, Bitmap bitmap) { - init(new BitmapState(bitmap), res); + this(new BitmapState(bitmap), res); + mBitmapState.mTargetDensity = mTargetDensity; } /** @@ -154,7 +154,10 @@ public class BitmapDrawable extends Drawable { */ @Deprecated public BitmapDrawable(String filepath) { - this(null, filepath); + this(new BitmapState(BitmapFactory.decodeFile(filepath)), null); + if (mBitmapState.mBitmap == null) { + android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + filepath); + } } /** @@ -162,21 +165,10 @@ public class BitmapDrawable extends Drawable { */ @SuppressWarnings("unused") public BitmapDrawable(Resources res, String filepath) { - Bitmap bitmap = null; - try (FileInputStream stream = new FileInputStream(filepath)) { - bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(res, stream), - (decoder, info, src) -> { - decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); - }); - } catch (Exception e) { - /* do nothing. This matches the behavior of BitmapFactory.decodeFile() - If the exception happened on decode, mBitmapState.mBitmap will be null. - */ - } finally { - init(new BitmapState(bitmap), res); - if (mBitmapState.mBitmap == null) { - android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + filepath); - } + this(new BitmapState(BitmapFactory.decodeFile(filepath)), null); + mBitmapState.mTargetDensity = mTargetDensity; + if (mBitmapState.mBitmap == null) { + android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + filepath); } } @@ -187,7 +179,10 @@ public class BitmapDrawable extends Drawable { */ @Deprecated public BitmapDrawable(java.io.InputStream is) { - this(null, is); + this(new BitmapState(BitmapFactory.decodeStream(is)), null); + if (mBitmapState.mBitmap == null) { + android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + is); + } } /** @@ -195,21 +190,10 @@ public class BitmapDrawable extends Drawable { */ @SuppressWarnings("unused") public BitmapDrawable(Resources res, java.io.InputStream is) { - Bitmap bitmap = null; - try { - bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(res, is), - (decoder, info, src) -> { - decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); - }); - } catch (Exception e) { - /* do nothing. This matches the behavior of BitmapFactory.decodeStream() - If the exception happened on decode, mBitmapState.mBitmap will be null. - */ - } finally { - init(new BitmapState(bitmap), res); - if (mBitmapState.mBitmap == null) { - android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + is); - } + this(new BitmapState(BitmapFactory.decodeStream(is)), null); + mBitmapState.mTargetDensity = mTargetDensity; + if (mBitmapState.mBitmap == null) { + android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + is); } } @@ -828,19 +812,9 @@ public class BitmapDrawable extends Drawable { } } - int density = Bitmap.DENSITY_NONE; - if (value.density == TypedValue.DENSITY_DEFAULT) { - density = DisplayMetrics.DENSITY_DEFAULT; - } else if (value.density != TypedValue.DENSITY_NONE) { - density = value.density; - } - Bitmap bitmap = null; try (InputStream is = r.openRawResource(srcResId, value)) { - ImageDecoder.Source source = ImageDecoder.createSource(r, is, density); - bitmap = ImageDecoder.decodeBitmap(source, (decoder, info, src) -> { - decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); - }); + bitmap = BitmapFactory.decodeResourceStream(r, value, is, null, null); } catch (Exception e) { // Do nothing and pick up the error below. } @@ -1039,21 +1013,14 @@ public class BitmapDrawable extends Drawable { } } - private BitmapDrawable(BitmapState state, Resources res) { - init(state, res); - } - /** - * The one helper to rule them all. This is called by all public & private + * The one constructor to rule them all. This is called by all public * constructors to set the state and initialize local properties. */ - private void init(BitmapState state, Resources res) { + private BitmapDrawable(BitmapState state, Resources res) { mBitmapState = state; - updateLocalState(res); - if (mBitmapState != null && res != null) { - mBitmapState.mTargetDensity = mTargetDensity; - } + updateLocalState(res); } /** diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 36a4d26d62bb..f17cd768c386 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -37,7 +37,6 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; -import android.graphics.ImageDecoder; import android.graphics.Insets; import android.graphics.NinePatch; import android.graphics.Outline; @@ -51,13 +50,11 @@ import android.graphics.Xfermode; import android.os.Trace; import android.util.AttributeSet; import android.util.DisplayMetrics; -import android.util.Log; import android.util.StateSet; import android.util.TypedValue; import android.util.Xml; import android.view.View; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; @@ -1178,10 +1175,6 @@ public abstract class Drawable { return null; } - if (opts == null) { - return getBitmapDrawable(res, value, is); - } - /* ugh. The decodeStream contract is that we have already allocated the pad rect, but if the bitmap does not had a ninepatch chunk, then the pad will be ignored. If we could change this to lazily @@ -1214,33 +1207,6 @@ public abstract class Drawable { return null; } - private static Drawable getBitmapDrawable(Resources res, TypedValue value, InputStream is) { - try { - ImageDecoder.Source source = null; - if (value != null) { - int density = Bitmap.DENSITY_NONE; - if (value.density == TypedValue.DENSITY_DEFAULT) { - density = DisplayMetrics.DENSITY_DEFAULT; - } else if (value.density != TypedValue.DENSITY_NONE) { - density = value.density; - } - source = ImageDecoder.createSource(res, is, density); - } else { - source = ImageDecoder.createSource(res, is); - } - - return ImageDecoder.decodeDrawable(source, (decoder, info, src) -> { - decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE); - }); - } catch (IOException e) { - /* do nothing. - If the exception happened on decode, the drawable will be null. - */ - Log.e("Drawable", "Unable to decode stream: " + e); - } - return null; - } - /** * Create a drawable from an XML document. For more information on how to * create resources in XML, see @@ -1340,10 +1306,11 @@ public abstract class Drawable { } Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, pathName); - try (FileInputStream stream = new FileInputStream(pathName)) { - return getBitmapDrawable(null, null, stream); - } catch(IOException e) { - // Do nothing; we will just return null if the FileInputStream had an error + try { + Bitmap bm = BitmapFactory.decodeFile(pathName); + if (bm != null) { + return drawableFromBitmap(null, bm, null, null, null, pathName); + } } finally { Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); } |