summaryrefslogtreecommitdiffstats
path: root/graphics/java
diff options
context:
space:
mode:
authorDianne Hackborn <hackbod@google.com>2009-07-26 17:42:30 -0700
committerDianne Hackborn <hackbod@google.com>2009-07-26 17:42:30 -0700
commit96e240f25a97c10bba863df328ed73a82c34ff61 (patch)
treea1784b60cb1fd8f7e70ea95eeb3a0a76f473740b /graphics/java
parent11ea33471e1a14a8594f0b2cd012d86340dd3bd8 (diff)
downloadframeworks_base-96e240f25a97c10bba863df328ed73a82c34ff61.zip
frameworks_base-96e240f25a97c10bba863df328ed73a82c34ff61.tar.gz
frameworks_base-96e240f25a97c10bba863df328ed73a82c34ff61.tar.bz2
Fiddle with default densities to try to sanitize the API.
An issue with the density API is that bitmaps assumed the old default density, so new programs would have to explicitly set the correct density for every bitmap they create. This is an attempt to fix that situation, by define the default density of bitmaps to be the main screen's density, except for old apps where it is the original default density. Actually implementing this is not so great, though, because the Bitmap constructors can't really know anything about who is calling them to know which density to use. So at this level the compatibility mode is defined per-process -- meaning the initial package loaded into a process defines the default bitmap density, and everyone else loaded in later on has to live with that. In practice this shouldn't be much of a problem, there shouldn't be much mixing of old vs. new apps in a process. It does mean that, going forward, if a developer is going to use shared user IDs for this, they will need to make sure either that all of their apps are in the same compatibility mode, or that their code explicitly sets the density of bitmaps it receives. This isn't all that great, but I think it is worth the benefit of allowing people who write modern apps to not have to deal with bitmap densities. This change also does some cleanup of the density management (making sure to always copy over bitmap densities, etc) and adds java docs to explain the various ways density is set and used by the system.
Diffstat (limited to 'graphics/java')
-rw-r--r--graphics/java/android/graphics/Bitmap.java70
-rw-r--r--graphics/java/android/graphics/Canvas.java47
2 files changed, 86 insertions, 31 deletions
diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java
index bb19229..eef1096 100644
--- a/graphics/java/android/graphics/Bitmap.java
+++ b/graphics/java/android/graphics/Bitmap.java
@@ -46,10 +46,29 @@ public final class Bitmap implements Parcelable {
private boolean mRecycled;
// Package-scoped for fast access.
- /*package*/ int mDensity = DENSITY_NONE;
+ /*package*/ int mDensity = sDefaultDensity = getDefaultDensity();
private static volatile Matrix sScaleMatrix;
+ private static volatile int sDefaultDensity = -1;
+
+ /**
+ * For backwards compatibility, allows the app layer to change the default
+ * density when running old apps.
+ * @hide
+ */
+ public static void setDefaultDensity(int density) {
+ sDefaultDensity = density;
+ }
+
+ /*package*/ static int getDefaultDensity() {
+ if (sDefaultDensity >= 0) {
+ return sDefaultDensity;
+ }
+ sDefaultDensity = DisplayMetrics.DENSITY_DEVICE;
+ return sDefaultDensity;
+ }
+
/**
* @noinspection UnusedDeclaration
*/
@@ -72,9 +91,16 @@ public final class Bitmap implements Parcelable {
/**
* <p>Returns the density for this bitmap.</p>
*
- * <p>The default density scale is {@link #DENSITY_NONE}.</p>
+ * <p>The default density is the same density as the current display,
+ * unless the current application does not support different screen
+ * densities in which case it is
+ * {@link android.util.DisplayMetrics#DENSITY_DEFAULT}. Note that
+ * compatibility mode is determined by the application that was initially
+ * loaded into a process -- applications that share the same process should
+ * all have the same compatibility, or ensure they explicitly set the
+ * density of their bitmaps appropriately.</p>
*
- * @return A scaling factor of the default density (160) or {@link #DENSITY_NONE}
+ * @return A scaling factor of the default density or {@link #DENSITY_NONE}
* if the scaling factor is unknown.
*
* @see #setDensity(int)
@@ -272,7 +298,8 @@ public final class Bitmap implements Parcelable {
* Tries to make a new bitmap based on the dimensions of this bitmap,
* setting the new bitmap's config to the one specified, and then copying
* this bitmap's pixels into the new bitmap. If the conversion is not
- * supported, or the allocator fails, then this returns NULL.
+ * supported, or the allocator fails, then this returns NULL. The returned
+ * bitmap initially has the same density as the original.
*
* @param config The desired config for the resulting bitmap
* @param isMutable True if the resulting bitmap should be mutable (i.e.
@@ -281,7 +308,11 @@ public final class Bitmap implements Parcelable {
*/
public Bitmap copy(Config config, boolean isMutable) {
checkRecycled("Can't copy a recycled bitmap");
- return nativeCopy(mNativeBitmap, config.nativeInt, isMutable);
+ Bitmap b = nativeCopy(mNativeBitmap, config.nativeInt, isMutable);
+ if (b != null) {
+ b.mDensity = mDensity;
+ }
+ return b;
}
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
@@ -316,7 +347,8 @@ public final class Bitmap implements Parcelable {
/**
* Returns an immutable bitmap from the source bitmap. The new bitmap may
- * be the same object as source, or a copy may have been made.
+ * be the same object as source, or a copy may have been made. It is
+ * initialized with the same density as the original bitmap.
*/
public static Bitmap createBitmap(Bitmap src) {
return createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
@@ -325,7 +357,8 @@ public final class Bitmap implements Parcelable {
/**
* Returns an immutable bitmap from the specified subset of the source
* bitmap. The new bitmap may be the same object as source, or a copy may
- * have been made.
+ * have been made. It is
+ * initialized with the same density as the original bitmap.
*
* @param source The bitmap we are subsetting
* @param x The x coordinate of the first pixel in source
@@ -339,7 +372,8 @@ public final class Bitmap implements Parcelable {
/**
* Returns an immutable bitmap from subset of the source bitmap,
- * transformed by the optional matrix.
+ * transformed by the optional matrix. It is
+ * initialized with the same density as the original bitmap.
*
* @param source The bitmap we are subsetting
* @param x The x coordinate of the first pixel in source
@@ -406,18 +440,20 @@ public final class Bitmap implements Parcelable {
paint.setAntiAlias(true);
}
}
- canvas.setBitmap(bitmap);
- canvas.drawBitmap(source, srcR, dstR, paint);
-
+
// The new bitmap was created from a known bitmap source so assume that
// they use the same density
bitmap.mDensity = source.mDensity;
+
+ canvas.setBitmap(bitmap);
+ canvas.drawBitmap(source, srcR, dstR, paint);
return bitmap;
}
/**
- * Returns a mutable bitmap with the specified width and height.
+ * Returns a mutable bitmap with the specified width and height. Its
+ * initial density is as per {@link #getDensity}.
*
* @param width The width of the bitmap
* @param height The height of the bitmap
@@ -432,7 +468,8 @@ public final class Bitmap implements Parcelable {
/**
* Returns a immutable bitmap with the specified width and height, with each
- * pixel value set to the corresponding value in the colors array.
+ * pixel value set to the corresponding value in the colors array. Its
+ * initial density is as per {@link #getDensity}.
*
* @param colors Array of {@link Color} used to initialize the pixels.
* @param offset Number of values to skip before the first color in the
@@ -466,7 +503,8 @@ public final class Bitmap implements Parcelable {
/**
* Returns a immutable bitmap with the specified width and height, with each
- * pixel value set to the corresponding value in the colors array.
+ * pixel value set to the corresponding value in the colors array. Its
+ * initial density is as per {@link #getDensity}.
*
* @param colors Array of {@link Color} used to initialize the pixels.
* This array must be at least as large as width * height.
@@ -882,6 +920,9 @@ public final class Bitmap implements Parcelable {
* -2, -2, so that drawing the alpha bitmap offset by (-2, -2) and then
* drawing the original would result in the blur visually aligning with
* the original.
+ *
+ * <p>The initial density of the returned bitmap is the same as the original's.
+ *
* @param paint Optional paint used to modify the alpha values in the
* resulting bitmap. Pass null for default behavior.
* @param offsetXY Optional array that returns the X (index 0) and Y
@@ -899,6 +940,7 @@ public final class Bitmap implements Parcelable {
if (bm == null) {
throw new RuntimeException("Failed to extractAlpha on Bitmap");
}
+ bm.mDensity = mDensity;
return bm;
}
diff --git a/graphics/java/android/graphics/Canvas.java b/graphics/java/android/graphics/Canvas.java
index 8ecbfbd..bc2e42e 100644
--- a/graphics/java/android/graphics/Canvas.java
+++ b/graphics/java/android/graphics/Canvas.java
@@ -49,7 +49,7 @@ public class Canvas {
private DrawFilter mDrawFilter;
// Package-scoped for quick access.
- /*package*/ int mDensity = DisplayMetrics.DENSITY_DEFAULT;
+ /*package*/ int mDensity = Bitmap.DENSITY_NONE;
// Used by native code
@SuppressWarnings({"UnusedDeclaration"})
@@ -57,7 +57,9 @@ public class Canvas {
/**
* Construct an empty raster canvas. Use setBitmap() to specify a bitmap to
- * draw into.
+ * draw into. The initial target density is {@link Bitmap#DENSITY_NONE};
+ * this will typically be replaced when a target bitmap is set for the
+ * canvas.
*/
public Canvas() {
// 0 means no native bitmap
@@ -67,6 +69,9 @@ public class Canvas {
/**
* Construct a canvas with the specified bitmap to draw into. The bitmap
* must be mutable.
+ *
+ * <p>The initial target density of the canvas is the same as the given
+ * bitmap's density.
*
* @param bitmap Specifies a mutable bitmap for the canvas to draw into.
*/
@@ -78,9 +83,7 @@ public class Canvas {
throwIfRecycled(bitmap);
mNativeCanvas = initRaster(bitmap.ni());
mBitmap = bitmap;
- final int density = bitmap.mDensity;
- mDensity = density == Bitmap.DENSITY_NONE
- ? DisplayMetrics.DENSITY_DEFAULT : density;
+ mDensity = bitmap.mDensity;
}
/*package*/ Canvas(int nativeCanvas) {
@@ -88,6 +91,7 @@ public class Canvas {
throw new IllegalStateException();
}
mNativeCanvas = nativeCanvas;
+ mDensity = Bitmap.getDefaultDensity();
}
/**
@@ -96,10 +100,14 @@ public class Canvas {
* be supported in this mode (e.g. some GL implementations may not support
* antialiasing or certain effects like ColorMatrix or certain Xfermodes).
* However, no exception will be thrown in those cases.
+ *
+ * <p>The initial target density of the canvas is the same as the initial
+ * density of bitmaps as per {@link Bitmap#getDensity() Bitmap.getDensity()}.
*/
public Canvas(GL gl) {
mNativeCanvas = initGL();
mGL = gl;
+ mDensity = Bitmap.getDefaultDensity();
}
/**
@@ -120,9 +128,13 @@ public class Canvas {
}
/**
- * Specify a bitmap for the canvas to draw into.
+ * Specify a bitmap for the canvas to draw into. As a side-effect, also
+ * updates the canvas's target density to match that of the bitmap.
*
* @param bitmap Specifies a mutable bitmap for the canvas to draw into.
+ *
+ * @see #setDensity(int)
+ * @see #getDensity()
*/
public void setBitmap(Bitmap bitmap) {
if (!bitmap.isMutable()) {
@@ -135,9 +147,7 @@ public class Canvas {
native_setBitmap(mNativeCanvas, bitmap.ni());
mBitmap = bitmap;
- final int density = bitmap.mDensity;
- mDensity = density == Bitmap.DENSITY_NONE
- ? DisplayMetrics.DENSITY_DEFAULT : density;
+ mDensity = bitmap.mDensity;
}
/**
@@ -176,12 +186,12 @@ public class Canvas {
public native int getHeight();
/**
- * <p>Returns the density for this Canvas' backing bitmap.</p>
- *
- * <p>The default density scale is {@link Bitmap#DENSITY_NONE}.</p>
+ * <p>Returns the target density of the canvas. The default density is
+ * derived from the density of its backing bitmap, or
+ * {@link Bitmap#DENSITY_NONE} if there is not one.</p>
*
- * @return A scaling factor of the default density (160dpi) or
- * {@link Bitmap#DENSITY_NONE} if the scaling factor is unknown.
+ * @return Returns the current target density of the canvas, which is used
+ * to determine the scaling factor when drawing a bitmap into it.
*
* @see #setDensity(int)
* @see Bitmap#getDensity()
@@ -191,10 +201,13 @@ public class Canvas {
}
/**
- * <p>Specifies the density for this Canvas' backing bitmap.
+ * <p>Specifies the density for this Canvas' backing bitmap. This modifies
+ * the target density of the canvas itself, as well as the density of its
+ * backing bitmap via {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}.
*
- * @param density The density scaling factor to use with this bitmap or
- * {@link Bitmap#DENSITY_NONE} if the factor is unknown.
+ * @param density The new target density of the canvas, which is used
+ * to determine the scaling factor when drawing a bitmap into it. Use
+ * {@link Bitmap#DENSITY_NONE} to disable bitmap scaling.
*
* @see #getDensity()
* @see Bitmap#setDensity(int)