From 980bead518fc5ddb52ae8d00258f91b087c1f91a Mon Sep 17 00:00:00 2001 From: sergeyv Date: Thu, 29 Dec 2016 12:05:51 -0800 Subject: [PATCH] Prohibit copyPixelsToBuffer & copyPixelsFromBuffer Test: BitmapTest#testHardwareCopyPixels(From|To)Buffer bug:30999911 Change-Id: I3bfa2846bff574bc0bfd54674eac794d1a6a0ff9 --- graphics/java/android/graphics/Bitmap.java | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java index dd23f9989d986..a259937c0b731 100644 --- a/graphics/java/android/graphics/Bitmap.java +++ b/graphics/java/android/graphics/Bitmap.java @@ -371,6 +371,16 @@ public final class Bitmap implements Parcelable { } } + /** + * This is called by methods that want to throw an exception if the bitmap + * is {@link Config#HARDWARE}. + */ + private void checkHardware(String errorMessage) { + if (getConfig() == Config.HARDWARE) { + throw new IllegalStateException(errorMessage); + } + } + /** * Common code for checking that x and y are >= 0 * @@ -512,8 +522,11 @@ public final class Bitmap implements Parcelable { *

After this method returns, the current position of the buffer is * updated: the position is incremented by the number of elements written * in the buffer.

+ * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE} */ public void copyPixelsToBuffer(Buffer dst) { + checkHardware("unable to copyPixelsToBuffer, " + + "pixel access is not supported on Config#HARDWARE bitmaps"); int elements = dst.remaining(); int shift; if (dst instanceof ByteBuffer) { @@ -550,9 +563,11 @@ public final class Bitmap implements Parcelable { * updated: the position is incremented by the number of elements read from * the buffer. If you need to read the bitmap from the buffer again you must * first rewind the buffer.

+ * @throws IllegalStateException if the bitmap's config is {@link Config#HARDWARE} */ public void copyPixelsFromBuffer(Buffer src) { checkRecycled("copyPixelsFromBuffer called on recycled bitmap"); + checkHardware("unable to copyPixelsFromBuffer, Config#HARDWARE bitmaps are immutable"); int elements = src.remaining(); int shift; @@ -1435,9 +1450,8 @@ public final class Bitmap implements Parcelable { @ColorInt public int getPixel(int x, int y) { checkRecycled("Can't call getPixel() on a recycled bitmap"); - if (getConfig() == Config.HARDWARE) { - throw new IllegalStateException("Can't access pixels in hardware Bitmaps"); - } + checkHardware("unable to getPixel(), " + + "pixel access is not supported on Config#HARDWARE bitmaps"); checkPixelAccess(x, y); return nativeGetPixel(mNativePtr, x, y); } @@ -1469,9 +1483,8 @@ public final class Bitmap implements Parcelable { public void getPixels(@ColorInt int[] pixels, int offset, int stride, int x, int y, int width, int height) { checkRecycled("Can't call getPixels() on a recycled bitmap"); - if (getConfig() == Config.HARDWARE) { - throw new IllegalStateException("Can't access pixels in hardware Bitmaps"); - } + checkHardware("unable to getPixels(), " + + "pixel access is not supported on Config#HARDWARE bitmaps"); if (width == 0 || height == 0) { return; // nothing to do }