diff options
author | Sam Judd <judds@google.com> | 2014-10-29 07:47:12 -0700 |
---|---|---|
committer | Sam Judd <judds@google.com> | 2014-10-29 07:59:40 -0700 |
commit | 35ba01c93ca9ff3ed3b9943571a746c5cb242a24 (patch) | |
tree | f33a2a3da8f65bad327abd00ee25fca5cfd42c76 /library/src | |
parent | 05910d7bb9e9aafdf85893227d1dd50b82266771 (diff) | |
download | glide-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')
-rw-r--r-- | library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandlerTest.java (renamed from library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/PreFillerHandlerTest.java) | 86 | ||||
-rw-r--r-- | library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillerAllocationTest.java | 134 | ||||
-rw-r--r-- | library/src/main/java/com/bumptech/glide/Glide.java | 14 | ||||
-rw-r--r-- | library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandler.java | 6 | ||||
-rw-r--r-- | library/src/main/java/com/bumptech/glide/load/engine/prefill/BitmapPreFiller.java | 29 | ||||
-rw-r--r-- | library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillBitmapAttribute.java | 129 | ||||
-rw-r--r-- | library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillQueue.java | 12 | ||||
-rw-r--r-- | library/src/main/java/com/bumptech/glide/load/engine/prefill/PreFillType.java | 179 |
8 files changed, 357 insertions, 232 deletions
diff --git a/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/PreFillerHandlerTest.java b/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandlerTest.java index 49c7b604..c3478076 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/PreFillerHandlerTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillIdleHandlerTest.java @@ -36,7 +36,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(RobolectricTestRunner.class) -public class PreFillerHandlerTest { +public class BitmapPreFillIdleHandlerTest { private BitmapPreFillIdleHandler.Clock clock; private BitmapPool pool; private MemoryCache cache; @@ -52,15 +52,17 @@ public class PreFillerHandlerTest { when(cache.put(any(Key.class), any(Resource.class))).thenAnswer(new AddBitmapCacheAnswer(addedBitmaps)); } - private BitmapPreFillIdleHandler getHandler(Map<PreFillBitmapAttribute, Integer> allocationOrder) { + private BitmapPreFillIdleHandler getHandler(Map<PreFillType, Integer> allocationOrder) { return new BitmapPreFillIdleHandler(pool, cache, new PreFillQueue(allocationOrder), clock); } @Test public void testAllocatesABitmapPerSizeInAllocationOrder() { - PreFillBitmapAttribute size = new PreFillBitmapAttribute(100, 100); + PreFillType size = new PreFillType.Builder(100) + .setConfig(Bitmap.Config.ARGB_8888) + .build(); final int toAdd = 3; - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, toAdd); BitmapPreFillIdleHandler handler = getHandler(allocationOrder); handler.queueIdle(); @@ -71,17 +73,21 @@ public class PreFillerHandlerTest { @Test public void testAllocatesBitmapsInOrderGivenByAllocationOrder() { - PreFillBitmapAttribute smallWidth = new PreFillBitmapAttribute(50, 100); - PreFillBitmapAttribute smallHeight = new PreFillBitmapAttribute(100, 50); - - PreFillBitmapAttribute[] expectedOrder = new PreFillBitmapAttribute[] { + PreFillType smallWidth = new PreFillType.Builder(50, 100) + .setConfig(Bitmap.Config.ARGB_8888) + .build(); + PreFillType smallHeight = new PreFillType.Builder(100, 50) + .setConfig(Bitmap.Config.RGB_565) + .build(); + + PreFillType[] expectedOrder = new PreFillType[] { smallWidth, smallHeight, smallWidth, smallHeight, }; - HashMap<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + HashMap<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(smallWidth, 2); allocationOrder.put(smallHeight, 2); BitmapPreFillIdleHandler handler = getHandler(allocationOrder); @@ -90,7 +96,7 @@ public class PreFillerHandlerTest { Bitmap[] expectedBitmaps = new Bitmap[expectedOrder.length]; for (int i = 0; i < expectedBitmaps.length; i++) { - PreFillBitmapAttribute current = expectedOrder[i]; + PreFillType current = expectedOrder[i]; expectedBitmaps[i] = Bitmap.createBitmap(current.getWidth(), current.getHeight(), current.getConfig()); } @@ -105,8 +111,10 @@ public class PreFillerHandlerTest { @Test public void testStopsAllocatingBitmapsUntilNextIdleCallIfAllocationsTakeLongerThanLimit() { - PreFillBitmapAttribute size = new PreFillBitmapAttribute(1, 1); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + PreFillType size = new PreFillType.Builder(1) + .setConfig(Bitmap.Config.ARGB_8888) + .build(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, 3); when(clock.now()).thenReturn(0L).thenReturn(0L).thenReturn(BitmapPreFillIdleHandler.MAX_DURATION_MILLIS); BitmapPreFillIdleHandler handler = getHandler(allocationOrder); @@ -121,14 +129,16 @@ public class PreFillerHandlerTest { @Test public void testPreFillHandlerReturnsFalseFromQueueIdleIfHasNoBitmapsToAllocate() { - BitmapPreFillIdleHandler handler = getHandler(new HashMap<PreFillBitmapAttribute, Integer>()); + BitmapPreFillIdleHandler handler = getHandler(new HashMap<PreFillType, Integer>()); assertFalse(handler.queueIdle()); } @Test public void testPreFillHandlerReturnsTrueFromQueueIdleIfHasBitmapsToAllocate() { - PreFillBitmapAttribute size = new PreFillBitmapAttribute(1, 1); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + PreFillType size = new PreFillType.Builder(1) + .setConfig(Bitmap.Config.ARGB_8888) + .build(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, 2); BitmapPreFillIdleHandler handler = getHandler(allocationOrder); when(clock.now()).thenReturn(0L).thenReturn(0L).thenReturn(BitmapPreFillIdleHandler.MAX_DURATION_MILLIS); @@ -137,8 +147,10 @@ public class PreFillerHandlerTest { @Test public void testPreFillHandlerReturnsFalseFromQueueIdleIfHasBitmapsButIsCancelled() { - PreFillBitmapAttribute size = new PreFillBitmapAttribute(1, 1); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + PreFillType size = new PreFillType.Builder(1) + .setConfig(Bitmap.Config.ARGB_8888) + .build(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, 2); BitmapPreFillIdleHandler handler = getHandler(allocationOrder); @@ -153,9 +165,10 @@ public class PreFillerHandlerTest { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(Util.getBitmapByteSize(bitmap)); - PreFillBitmapAttribute size = new PreFillBitmapAttribute(bitmap.getWidth(), bitmap.getHeight(), - bitmap.getConfig(), 1); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()) + .setConfig(bitmap.getConfig()) + .build(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, 1); getHandler(allocationOrder).queueIdle(); @@ -170,9 +183,10 @@ public class PreFillerHandlerTest { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(0); - PreFillBitmapAttribute size = new PreFillBitmapAttribute(bitmap.getWidth(), bitmap.getHeight(), - bitmap.getConfig(), 1); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()) + .setConfig(bitmap.getConfig()) + .build(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, 1); getHandler(allocationOrder).queueIdle(); @@ -187,9 +201,10 @@ public class PreFillerHandlerTest { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(Util.getBitmapByteSize(bitmap) / 2); - PreFillBitmapAttribute size = new PreFillBitmapAttribute(bitmap.getWidth(), bitmap.getHeight(), - bitmap.getConfig(), 1); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()) + .setConfig(bitmap.getConfig()) + .build(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, 1); getHandler(allocationOrder).queueIdle(); @@ -202,14 +217,16 @@ public class PreFillerHandlerTest { @Test public void testDoesAGetFromPoolBeforeAddingForEachSize() { Bitmap first = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444); - PreFillBitmapAttribute firstSize = new PreFillBitmapAttribute(first.getWidth(), first.getHeight(), - first.getConfig(), 1); + PreFillType firstSize = new PreFillType.Builder(first.getWidth(), first.getHeight()) + .setConfig(first.getConfig()) + .build(); Bitmap second = Bitmap.createBitmap(200, 200, Bitmap.Config.RGB_565); - PreFillBitmapAttribute secondSize = new PreFillBitmapAttribute(second.getWidth(), second.getHeight(), - second.getConfig(), 1); + PreFillType secondSize = new PreFillType.Builder(second.getWidth(), second.getHeight()) + .setConfig(second.getConfig()) + .build(); - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(firstSize, 1); allocationOrder.put(secondSize, 1); @@ -227,11 +244,12 @@ public class PreFillerHandlerTest { @Test public void testDoesNotGetMoreThanOncePerSize() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444); - PreFillBitmapAttribute size = new PreFillBitmapAttribute(bitmap.getWidth(), bitmap.getHeight(), - bitmap.getConfig(), 1); + PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()) + .setConfig(bitmap.getConfig()) + .build(); final int numBitmaps = 5; - Map<PreFillBitmapAttribute, Integer> allocationOrder = new HashMap<PreFillBitmapAttribute, Integer>(); + Map<PreFillType, Integer> allocationOrder = new HashMap<PreFillType, Integer>(); allocationOrder.put(size, numBitmaps); getHandler(allocationOrder).queueIdle(); diff --git a/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillerAllocationTest.java b/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillerAllocationTest.java index 85d1595a..c55bbeb1 100644 --- a/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillerAllocationTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/load/engine/prefill/BitmapPreFillerAllocationTest.java @@ -1,6 +1,7 @@ package com.bumptech.glide.load.engine.prefill; import android.graphics.Bitmap; +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; @@ -26,7 +27,7 @@ import static org.mockito.Mockito.when; public class BitmapPreFillerAllocationTest { private static final int DEFAULT_BITMAP_WIDTH = 100; private static final int DEFAULT_BITMAP_HEIGHT = 50; - private static final Bitmap.Config DEFAULT_BITMAP_CONFIG = PreFillBitmapAttribute.DEFAULT_CONFIG; + private static final Bitmap.Config DEFAULT_BITMAP_CONFIG = PreFillType.DEFAULT_CONFIG; private static final Bitmap DEFAULT_BITMAP = Bitmap.createBitmap(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT, DEFAULT_BITMAP_CONFIG); private static final int DEFAULT_BITMAP_SIZE = Util.getBitmapByteSize(DEFAULT_BITMAP); @@ -47,14 +48,16 @@ public class BitmapPreFillerAllocationTest { cache = mock(MemoryCache.class); when(cache.getMaxSize()).thenReturn(CACHE_SIZE); - bitmapPreFiller = new BitmapPreFiller(cache, pool); + bitmapPreFiller = new BitmapPreFiller(cache, pool, DecodeFormat.DEFAULT); } @Test public void testAllocationOrderContainsEnoughSizesToFillPoolAndMemoryCache() { PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[]{ - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + new PreFillType[] { + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build() } ); @@ -63,22 +66,28 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderThatDoesNotFitExactlyIntoGivenSizeRoundsDown() { - PreFillBitmapAttribute[] sizes = new PreFillBitmapAttribute[] { - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT), - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT), - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + PreFillType[] sizes = new PreFillType[] { + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(), + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(), + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(), }; PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder(sizes); int byteSize = 0; while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute current = allocationOrder.remove(); + PreFillType current = allocationOrder.remove(); byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig()); } int expectedSize = 0; int maxSize = POOL_SIZE + CACHE_SIZE; - for (PreFillBitmapAttribute current : sizes) { + for (PreFillType current : sizes) { int currentSize = Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig()); expectedSize += currentSize * (maxSize / (3 * currentSize)); } @@ -89,16 +98,22 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderDoesNotOverFillWithMultipleSizes() { PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[] { - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT), - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT), - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + new PreFillType[] { + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(), + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(), + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build() } ); int byteSize = 0; while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute current = allocationOrder.remove(); + PreFillType current = allocationOrder.remove(); byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig()); } @@ -108,18 +123,24 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderDoesNotOverFillWithMultipleSizesAndWeights() { PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[]{ - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT, - DEFAULT_BITMAP_CONFIG, 4), - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT), - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 3, - DEFAULT_BITMAP_CONFIG, 3) + new PreFillType[]{ + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .setWeight(4) + .build(), + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(), + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 3) + .setConfig(DEFAULT_BITMAP_CONFIG) + .setWeight(3) + .build() } ); int byteSize = 0; while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute current = allocationOrder.remove(); + PreFillType current = allocationOrder.remove(); byteSize += Util.getBitmapByteSize(current.getWidth(), current.getHeight(), current.getConfig()); } @@ -129,13 +150,15 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderContainsSingleSizeIfSingleSizeIsProvided() { PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[] { - new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + new PreFillType[] { + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build() } ); while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute size = allocationOrder.remove(); + PreFillType size = allocationOrder.remove(); assertEquals(DEFAULT_BITMAP_WIDTH, size.getWidth()); assertEquals(DEFAULT_BITMAP_HEIGHT, size.getHeight()); assertEquals(DEFAULT_BITMAP_CONFIG, size.getConfig()); @@ -144,16 +167,20 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderSplitsEvenlyBetweenEqualSizesWithEqualWeights() { - PreFillBitmapAttribute smallWidth = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT); - PreFillBitmapAttribute smallHeight = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, - DEFAULT_BITMAP_HEIGHT / 2); + PreFillType smallWidth = new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); + PreFillType smallHeight = + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[] { smallWidth, smallHeight, } + new PreFillType[] { smallWidth, smallHeight, } ); int numSmallWidth = 0, numSmallHeight = 0; while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute current = allocationOrder.remove(); + PreFillType current = allocationOrder.remove(); if (smallWidth.equals(current)) { numSmallWidth++; } else if (smallHeight.equals(current)) { @@ -168,15 +195,21 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderSplitsByteSizeEvenlyBetweenUnEqualSizesWithEqualWeights() { - PreFillBitmapAttribute smallWidth = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT); - PreFillBitmapAttribute normal = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT); + PreFillType smallWidth = + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); + PreFillType normal = + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[] { smallWidth, normal } + new PreFillType[] { smallWidth, normal } ); int numSmallWidth = 0, numNormal = 0; while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute current = allocationOrder.remove(); + PreFillType current = allocationOrder.remove(); if (smallWidth.equals(current)) { numSmallWidth++; } else if (normal.equals(current)) { @@ -191,17 +224,21 @@ public class BitmapPreFillerAllocationTest { @Test public void testAllocationOrderSplitsByteSizeUnevenlyBetweenEqualSizesWithUnequalWeights() { - PreFillBitmapAttribute doubleWeight = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, - DEFAULT_BITMAP_HEIGHT, DEFAULT_BITMAP_CONFIG, 2); - PreFillBitmapAttribute normal = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2, - DEFAULT_BITMAP_CONFIG, 1); + PreFillType doubleWeight = + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .setWeight(2) + .build(); + PreFillType normal = new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[] { doubleWeight, normal } + new PreFillType[] { doubleWeight, normal } ); int numDoubleWeight = 0, numNormal = 0; while (!allocationOrder.isEmpty()) { - PreFillBitmapAttribute current = allocationOrder.remove(); + PreFillType current = allocationOrder.remove(); if (doubleWeight.equals(current)) { numDoubleWeight++; } else if (normal.equals(current)) { @@ -218,20 +255,25 @@ public class BitmapPreFillerAllocationTest { public void testAllocationOrderRoundRobinsDifferentSizes() { when(pool.getMaxSize()).thenReturn(DEFAULT_BITMAP_SIZE); when(cache.getMaxSize()).thenReturn(DEFAULT_BITMAP_SIZE); - PreFillBitmapAttribute smallWidth = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT); - PreFillBitmapAttribute smallHeight = new PreFillBitmapAttribute(DEFAULT_BITMAP_WIDTH, - DEFAULT_BITMAP_HEIGHT / 2); + PreFillType smallWidth = + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH / 2, DEFAULT_BITMAP_HEIGHT) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); + PreFillType smallHeight = + new PreFillType.Builder(DEFAULT_BITMAP_WIDTH, DEFAULT_BITMAP_HEIGHT / 2) + .setConfig(DEFAULT_BITMAP_CONFIG) + .build(); PreFillQueue allocationOrder = bitmapPreFiller.generateAllocationOrder( - new PreFillBitmapAttribute[] { smallWidth, smallHeight, } + new PreFillType[] { smallWidth, smallHeight, } ); - List<PreFillBitmapAttribute> attributes = new ArrayList<PreFillBitmapAttribute>(); + List<PreFillType> attributes = new ArrayList<PreFillType>(); while (!allocationOrder.isEmpty()) { attributes.add(allocationOrder.remove()); } - CombinableMatcher.CombinableEitherMatcher<Iterable<? extends PreFillBitmapAttribute>> either = + CombinableMatcher.CombinableEitherMatcher<Iterable<? extends PreFillType>> either = either(contains(smallWidth, smallHeight, smallWidth, smallHeight)); assertThat(attributes, either.or(contains(smallHeight, smallWidth, smallHeight, smallWidth))); } 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); + } + } +} |