aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com
diff options
context:
space:
mode:
authorSam Judd <judds@google.com>2014-10-29 07:47:12 -0700
committerSam Judd <judds@google.com>2014-10-29 07:59:40 -0700
commit35ba01c93ca9ff3ed3b9943571a746c5cb242a24 (patch)
treef33a2a3da8f65bad327abd00ee25fca5cfd42c76 /library/src/main/java/com
parent05910d7bb9e9aafdf85893227d1dd50b82266771 (diff)
downloadglide-35ba01c93ca9ff3ed3b9943571a746c5cb242a24.tar.gz
Obey default DecodeFormat in prefill API.
Also does some cleanup, including switching to use a builder and re-naming a few classes.
Diffstat (limited to 'library/src/main/java/com')
-rw-r--r--library/src/main/java/com/bumptech/glide/Glide.java14
-rw-r--r--library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandler.java6
-rw-r--r--library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFiller.java29
-rw-r--r--library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillBitmapAttribute.java129
-rw-r--r--library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java12
-rw-r--r--library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillType.java179
6 files changed, 217 insertions, 152 deletions
diff --git a/library/src/main/java/com/bumptech/glide/Glide.java b/library/src/main/java/com/bumptech/glide/Glide.java
index c088d699..db04d01e 100644
--- a/library/src/main/java/com/bumptech/glide/Glide.java
+++ b/library/src/main/java/com/bumptech/glide/Glide.java
@@ -17,7 +17,7 @@ import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.Engine;
-import com.bumptech.glide.load.engine.prefill.PreFillBitmapAttribute;
+import com.bumptech.glide.load.engine.prefill.PreFillType;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.prefill.BitmapPreFiller;
import com.bumptech.glide.load.engine.cache.MemoryCache;
@@ -190,7 +190,7 @@ public class Glide {
this.decodeFormat = decodeFormat;
loaderFactory = new GenericLoaderFactory(context);
mainHandler = new Handler(Looper.getMainLooper());
- bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool);
+ bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool, decodeFormat);
dataLoadProviderRegistry = new DataLoadProviderRegistry();
@@ -334,12 +334,12 @@ public class Glide {
* pre-filling only happens when the Activity is first created, rather than on every rotation.
* </p>
*
- * @param bitmapAttributes The list of {@link com.bumptech.glide.load.engine.prefill.PreFillBitmapAttribute}s
- * representing individual sizes and configurations of {@link android.graphics.Bitmap}s to
- * be pre-filled.
+ * @param bitmapAttributeBuilders The list of
+ * {@link com.bumptech.glide.load.engine.prefill.PreFillType.Builder Builders} representing
+ * individual sizes and configurations of {@link android.graphics.Bitmap}s to be pre-filled.
*/
- public void preFillBitmapPool(PreFillBitmapAttribute... bitmapAttributes) {
- bitmapPreFiller.preFill(bitmapAttributes);
+ public void preFillBitmapPool(PreFillType.Builder... bitmapAttributeBuilders) {
+ bitmapPreFiller.preFill(bitmapAttributeBuilders);
}
/**
diff --git a/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandler.java b/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandler.java
index 1d32f4ed..3fef55c2 100644
--- a/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandler.java
+++ b/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandler.java
@@ -30,8 +30,8 @@ final class BitmapPreFillIdleHandler implements MessageQueue.IdleHandler {
private final MemoryCache memoryCache;
private final PreFillQueue toPrefill;
private final Clock clock;
- private final Set<PreFillBitmapAttribute> seenAttributes =
- new HashSet<PreFillBitmapAttribute>();
+ private final Set<PreFillType> seenAttributes =
+ new HashSet<PreFillType>();
private boolean isCancelled;
@@ -57,7 +57,7 @@ final class BitmapPreFillIdleHandler implements MessageQueue.IdleHandler {
public boolean queueIdle() {
long start = clock.now();
while (!toPrefill.isEmpty() && (clock.now() - start) < MAX_DURATION_MILLIS) {
- PreFillBitmapAttribute toAllocate = toPrefill.remove();
+ PreFillType toAllocate = toPrefill.remove();
Bitmap bitmap = Bitmap.createBitmap(toAllocate.getWidth(), toAllocate.getHeight(),
toAllocate.getConfig());
diff --git a/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFiller.java b/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFiller.java
index b4d9f1c4..4671a275 100644
--- a/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFiller.java
+++ b/library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFiller.java
@@ -1,6 +1,8 @@
package com.bumptech.glide.load.engine.prefill;
+import android.graphics.Bitmap;
import android.os.Looper;
+import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.util.Util;
@@ -16,36 +18,49 @@ public final class BitmapPreFiller {
private final MemoryCache memoryCache;
private final BitmapPool bitmapPool;
+ private final DecodeFormat defaultFormat;
private BitmapPreFillIdleHandler current;
- public BitmapPreFiller(MemoryCache memoryCache, BitmapPool bitmapPool) {
+ public BitmapPreFiller(MemoryCache memoryCache, BitmapPool bitmapPool, DecodeFormat defaultFormat) {
this.memoryCache = memoryCache;
this.bitmapPool = bitmapPool;
+ this.defaultFormat = defaultFormat;
}
- public void preFill(PreFillBitmapAttribute... bitmapAttributes) {
+ public void preFill(PreFillType.Builder... bitmapAttributeBuilders) {
if (current != null) {
current.cancel();
}
+
+ PreFillType[] bitmapAttributes = new PreFillType[bitmapAttributeBuilders.length];
+ for (int i = 0; i < bitmapAttributeBuilders.length; i++) {
+ PreFillType.Builder builder = bitmapAttributeBuilders[i];
+ if (builder.getConfig() == null) {
+ builder.setConfig(defaultFormat == DecodeFormat.ALWAYS_ARGB_8888
+ ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
+ }
+ bitmapAttributes[i] = builder.build();
+ }
+
PreFillQueue allocationOrder = generateAllocationOrder(bitmapAttributes);
current = new BitmapPreFillIdleHandler(bitmapPool, memoryCache, allocationOrder);
Looper.myQueue().addIdleHandler(current);
}
// Visible for testing.
- PreFillQueue generateAllocationOrder(PreFillBitmapAttribute[] preFillSizes) {
+ PreFillQueue generateAllocationOrder(PreFillType[] preFillSizes) {
final int maxSize = memoryCache.getMaxSize() - memoryCache.getCurrentSize() + bitmapPool.getMaxSize();
int totalWeight = 0;
- for (PreFillBitmapAttribute size : preFillSizes) {
+ for (PreFillType size : preFillSizes) {
totalWeight += size.getWeight();
}
final float bytesPerWeight = maxSize / (float) totalWeight;
- Map<PreFillBitmapAttribute, Integer> attributeToCount = new HashMap<PreFillBitmapAttribute, Integer>();
- for (PreFillBitmapAttribute size : preFillSizes) {
+ Map<PreFillType, Integer> attributeToCount = new HashMap<PreFillType, Integer>();
+ for (PreFillType size : preFillSizes) {
int bytesForSize = Math.round(bytesPerWeight * size.getWeight());
int bytesPerBitmap = getSizeInBytes(size);
int bitmapsForSize = bytesForSize / bytesPerBitmap;
@@ -55,7 +70,7 @@ public final class BitmapPreFiller {
return new PreFillQueue(attributeToCount);
}
- private static int getSizeInBytes(PreFillBitmapAttribute size) {
+ private static int getSizeInBytes(PreFillType size) {
return Util.getBitmapByteSize(size.getWidth(), size.getHeight(), size.getConfig());
}
}
diff --git a/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillBitmapAttribute.java b/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillBitmapAttribute.java
deleted file mode 100644
index b84763fa..00000000
--- a/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillBitmapAttribute.java
+++ /dev/null
@@ -1,129 +0,0 @@
-package com.bumptech.glide.load.engine.prefill;
-
-import android.graphics.Bitmap;
-
-/**
- * A container for a set of options used to pre-fill a {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}
- * with {@link Bitmap Bitmaps} of a single size and configuration.
- */
-public final class PreFillBitmapAttribute {
- // Visible for testing.
- static final Bitmap.Config DEFAULT_CONFIG = Bitmap.Config.RGB_565;
- private final int width;
- private final int height;
- private final Bitmap.Config config;
- private final int weight;
-
- /**
- * Constructor for a single type of {@link android.graphics.Bitmap}.
- *
- * @see #PreFillBitmapAttribute(int, int, int)
- * @see #PreFillBitmapAttribute(int, int, android.graphics.Bitmap.Config, int)
- *
- * @param width The width in pixels of the {@link android.graphics.Bitmap} to pre-fill.
- * @param height The height in pixels of the {@link android.graphics.Bitmap} to pre-fill.
- */
- public PreFillBitmapAttribute(int width, int height) {
- this(width, height, 1);
- }
-
- /**
- * Constructor for a single type of {@link android.graphics.Bitmap}.
- *
- * @see #PreFillBitmapAttribute(int, int)
- * @see #PreFillBitmapAttribute(int, int, android.graphics.Bitmap.Config, int)
- *
- * @param width The width in pixels of the {@link android.graphics.Bitmap} to pre-fill.
- * @param height The height in pixels of the {@link android.graphics.Bitmap} to pre-fill.
- * @param weight An integer indicating how to balance pre-filling this size and configuration of
- * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
- */
- public PreFillBitmapAttribute(int width, int height, int weight) {
- this(width, height, DEFAULT_CONFIG, weight);
- }
-
- /**
- * Constructor for a single type of {@link android.graphics.Bitmap}.
- *
- * @see #PreFillBitmapAttribute(int, int)
- * @see #PreFillBitmapAttribute(int, int, int)
- *
- * @param width The width in pixels of the {@link android.graphics.Bitmap Bitmaps} to
- * pre-fill.
- * @param height The height in pixels of the {@link android.graphics.Bitmap Bitmaps} to
- * pre-fill.
- * @param config The {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps} to
- * pre-fill.
- * @param weight An integer indicating how to balance pre-filling this size and configuration of
- * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
- */
- public PreFillBitmapAttribute(int width, int height, Bitmap.Config config, int weight) {
- this.width = width;
- this.height = height;
- this.config = config;
- this.weight = weight;
- }
-
- /**
- * Returns the width in pixels of the {@link android.graphics.Bitmap Bitmaps}.
- */
- public int getWidth() {
- return width;
- }
-
- /**
- * Returns the height in pixels of the {@link android.graphics.Bitmap Bitmaps}.
- */
- public int getHeight() {
- return height;
- }
-
- /**
- * Returns the {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps}.
- */
- public Bitmap.Config getConfig() {
- return config;
- }
-
- /**
- * Returns the weight of the {@link android.graphics.Bitmap Bitmaps} of this type.
- */
- public int getWeight() {
- return weight;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- } else if (o == null || getClass() != o.getClass()) {
- return false;
- }
-
- PreFillBitmapAttribute size = (PreFillBitmapAttribute) o;
-
- return height == size.height
- && weight == size.weight
- && width == size.width
- && config == size.config;
- }
-
- @Override
- public int hashCode() {
- int result = width;
- result = 31 * result + height;
- result = 31 * result + config.hashCode();
- result = 31 * result + weight;
- return result;
- }
-
- @Override
- public String toString() {
- return "PreFillSize{"
- + "width=" + width
- + ", height=" + height
- + ", config=" + config
- + ", weight=" + weight
- + '}';
- }
-}
diff --git a/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java b/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java
index abdc9e2b..f88032a8 100644
--- a/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java
+++ b/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java
@@ -6,23 +6,23 @@ import java.util.Map;
final class PreFillQueue {
- private final Map<PreFillBitmapAttribute, Integer> bitmapsPerType;
- private final List<PreFillBitmapAttribute> keyList;
+ private final Map<PreFillType, Integer> bitmapsPerType;
+ private final List<PreFillType> keyList;
private int bitmapsRemaining;
private int keyIndex;
- public PreFillQueue(Map<PreFillBitmapAttribute, Integer> bitmapsPerType) {
+ public PreFillQueue(Map<PreFillType, Integer> bitmapsPerType) {
this.bitmapsPerType = bitmapsPerType;
// We don't particularly care about the initial order.
- keyList = new ArrayList<PreFillBitmapAttribute>(bitmapsPerType.keySet());
+ keyList = new ArrayList<PreFillType>(bitmapsPerType.keySet());
for (Integer count : bitmapsPerType.values()) {
bitmapsRemaining += count;
}
}
- public PreFillBitmapAttribute remove() {
- PreFillBitmapAttribute result = keyList.get(keyIndex);
+ public PreFillType remove() {
+ PreFillType result = keyList.get(keyIndex);
Integer countForResult = bitmapsPerType.get(result);
if (countForResult == 1) {
diff --git a/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillType.java b/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillType.java
new file mode 100644
index 00000000..33d0387e
--- /dev/null
+++ b/library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillType.java
@@ -0,0 +1,179 @@
+package com.bumptech.glide.load.engine.prefill;
+
+import android.graphics.Bitmap;
+
+/**
+ * A container for a set of options used to pre-fill a {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}
+ * with {@link Bitmap Bitmaps} of a single size and configuration.
+ */
+public final class PreFillType {
+ // Visible for testing.
+ static final Bitmap.Config DEFAULT_CONFIG = Bitmap.Config.RGB_565;
+ private final int width;
+ private final int height;
+ private final Bitmap.Config config;
+ private final int weight;
+
+ /**
+ * Constructor for a single type of {@link android.graphics.Bitmap}.
+ *
+ * @param width The width in pixels of the {@link android.graphics.Bitmap Bitmaps} to
+ * pre-fill.
+ * @param height The height in pixels of the {@link android.graphics.Bitmap Bitmaps} to
+ * pre-fill.
+ * @param config The {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps} to
+ * pre-fill.
+ * @param weight An integer indicating how to balance pre-filling this size and configuration of
+ * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
+ */
+ PreFillType(int width, int height, Bitmap.Config config, int weight) {
+ if (config == null) {
+ throw new NullPointerException("Config must not be null");
+ }
+ if (width <= 0) {
+ throw new IllegalArgumentException("Width must be > 0");
+ }
+ if (height <= 0) {
+ throw new IllegalArgumentException("Height must be > 0");
+ }
+ if (weight <= 0) {
+ throw new IllegalArgumentException("Weight must be > 0");
+ }
+
+ this.width = width;
+ this.height = height;
+ this.config = config;
+ this.weight = weight;
+ }
+
+ /**
+ * Returns the width in pixels of the {@link android.graphics.Bitmap Bitmaps}.
+ */
+ int getWidth() {
+ return width;
+ }
+
+ /**
+ * Returns the height in pixels of the {@link android.graphics.Bitmap Bitmaps}.
+ */
+ int getHeight() {
+ return height;
+ }
+
+ /**
+ * Returns the {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps}.
+ */
+ Bitmap.Config getConfig() {
+ return config;
+ }
+
+ /**
+ * Returns the weight of the {@link android.graphics.Bitmap Bitmaps} of this type.
+ */
+ int getWeight() {
+ return weight;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ PreFillType size = (PreFillType) o;
+
+ return height == size.height
+ && weight == size.weight
+ && width == size.width
+ && config == size.config;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = width;
+ result = 31 * result + height;
+ result = 31 * result + config.hashCode();
+ result = 31 * result + weight;
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "PreFillSize{"
+ + "width=" + width
+ + ", height=" + height
+ + ", config=" + config
+ + ", weight=" + weight
+ + '}';
+ }
+
+ /**
+ * Builder for {@link PreFillType}.
+ */
+ public static final class Builder {
+ private final int width;
+ private final int height;
+
+ private Bitmap.Config config;
+ private int weight = 1;
+
+ /**
+ * Constructor for a builder that uses the given size as the width and height of the Bitmaps to prefill.
+ * @param size The width and height in pixels of the Bitmaps to prefill.
+ */
+ public Builder(int size) {
+ this(size, size);
+ }
+
+ /**
+ * Constructor for a builder that uses the given dimensions as the dimensions of the Bitmaps to prefill.
+ * @param width The width in pixels of the Bitmaps to prefill.
+ * @param height The height in pixels of the Bitmaps to prefill.
+ */
+ public Builder(int width, int height) {
+ this.width = width;
+ this.height = height;
+ }
+
+ /**
+ * Sets the {@link android.graphics.Bitmap.Config} for the Bitmaps to pre-fill.
+ * @param config The config to use, or null to use Glide's default.
+ * @return This builder.
+ */
+ public Builder setConfig(Bitmap.Config config) {
+ this.config = config;
+ return this;
+ }
+
+ /**
+ * Returns the current {@link android.graphics.Bitmap.Config}.
+ */
+ Bitmap.Config getConfig() {
+ return config;
+ }
+
+ /**
+ * Sets the weight to use to balance how many Bitmaps of this type are prefilled relative to the other requested
+ * types.
+ * @param weight An integer indicating how to balance pre-filling this size and configuration of
+ * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
+ * @return This builder.
+ */
+ public Builder setWeight(int weight) {
+ if (weight <= 0) {
+ throw new IllegalArgumentException("Weight must be > 0");
+ }
+ this.weight = weight;
+ return this;
+ }
+
+ /**
+ * Returns a new {@link PreFillType}.
+ */
+ PreFillType build() {
+ return new PreFillType(width, height, config, weight);
+ }
+ }
+}