Skip to content

Commit

Permalink
Avoid passing null configs to Bitmap.createBitmap in LruBitmapPool.
Browse files Browse the repository at this point in the history
We already handle null configs for our various strategies because some 
types of Bitmaps (GIFs mostly) on some API levels will have special 
native configs that have a null Java equivalent. As a result, 
LruBitmapPool does not throw as long as there is a matching Bitmap in 
the pool even if the config we’re asked for is null. 

This change fixes the inconsistent behavior where we would throw if
the pool happened to not have a matching Bitmap when the given config
is null. Now we universally handle null configs in get() and getDirty().
  • Loading branch information
sjudd committed Jan 3, 2018
1 parent 0deee30 commit c1036c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Bitmap get(int width, int height, Bitmap.Config config) {
// contents individually, so we do so here. See issue #131.
result.eraseColor(Color.TRANSPARENT);
} else {
result = Bitmap.createBitmap(width, height, config);
result = createBitmap(width, height, config);
}

return result;
Expand All @@ -139,11 +139,16 @@ public Bitmap get(int width, int height, Bitmap.Config config) {
public Bitmap getDirty(int width, int height, Bitmap.Config config) {
Bitmap result = getDirtyOrNull(width, height, config);
if (result == null) {
result = Bitmap.createBitmap(width, height, config);
result = createBitmap(width, height, config);
}
return result;
}

@NonNull
private static Bitmap createBitmap(int width, int height, @Nullable Bitmap.Config config) {
return Bitmap.createBitmap(width, height, config != null ? config : DEFAULT_CONFIG);
}

@TargetApi(Build.VERSION_CODES.O)
private static void assertNotHardwareConfig(Bitmap.Config config) {
// Avoid short circuiting on sdk int since it breaks on some versions of Android.
Expand All @@ -159,7 +164,8 @@ private static void assertNotHardwareConfig(Bitmap.Config config) {
}

@Nullable
private synchronized Bitmap getDirtyOrNull(int width, int height, Bitmap.Config config) {
private synchronized Bitmap getDirtyOrNull(
int width, int height, @Nullable Bitmap.Config config) {
assertNotHardwareConfig(config);
// Config will be null for non public config types, which can lead to transformations naively
// passing in null as the requested config here. See issue #194.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ public void testPassesArgb8888ToStrategyAsConfigForRequestsWithNullConfigsOnGetD
assertEquals(expected, result);
}

@Test
public void get_withNullConfig_andEmptyPool_returnsNewArgb8888Bitmap() {
Bitmap result = pool.get(100, 100, /*config=*/ null);
assertThat(result.getConfig()).isEqualTo(Bitmap.Config.ARGB_8888);
}

@Test
public void getDirty_withNullConfig_andEmptyPool_returnsNewArgb8888Bitmap() {
Bitmap result = pool.getDirty(100, 100, /*config=*/ null);
assertThat(result.getConfig()).isEqualTo(Bitmap.Config.ARGB_8888);
}

private void testTrimMemory(int fillSize, int trimLevel, int expectedSize) {
MockStrategy strategy = new MockStrategy();
LruBitmapPool pool = new LruBitmapPool(MAX_SIZE, strategy, ALLOWED_CONFIGS);
Expand Down Expand Up @@ -240,7 +252,7 @@ public void put(Bitmap bitmap) {

@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
return bitmaps.removeLast();
return bitmaps.isEmpty() ? null : bitmaps.removeLast();
}

@Override
Expand Down

0 comments on commit c1036c1

Please sign in to comment.