-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid recycling Bitmaps/Drawables passed to load().
This is still best effort in that anyone can register a component that doesn’t follow our conventions and break things, but it should be safer by default at least.
- Loading branch information
Showing
12 changed files
with
414 additions
and
45 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
instrumentation/src/androidTest/java/com/bumptech/glide/LoadBitmapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.bumptech.glide; | ||
|
||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.drawable.Drawable; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter; | ||
import com.bumptech.glide.load.engine.cache.MemoryCacheAdapter; | ||
import com.bumptech.glide.request.FutureTarget; | ||
import com.bumptech.glide.test.ConcurrencyHelper; | ||
import com.bumptech.glide.test.GlideApp; | ||
import com.bumptech.glide.test.ResourceIds; | ||
import com.bumptech.glide.test.TearDownGlide; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class LoadBitmapTest { | ||
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); | ||
private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); | ||
private Context context; | ||
|
||
@Before | ||
public void setUp() { | ||
context = InstrumentationRegistry.getTargetContext(); | ||
} | ||
|
||
@Test | ||
public void clearFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
FutureTarget<Drawable> target = | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.asDrawable() | ||
.load(bitmap) | ||
.submit(100, 100)); | ||
Glide.with(context).clear(target); | ||
|
||
// Allow Glide's resource recycler to run on the main thread. | ||
concurrency.pokeMainThread(); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void transformFromRequestBuilder_asDrawable_withLoadedBitmap_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.asDrawable() | ||
.load(bitmap) | ||
.centerCrop() | ||
.submit(100, 100)); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void clearFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
FutureTarget<Drawable> target = | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.load(bitmap) | ||
.submit(100, 100)); | ||
Glide.with(context).clear(target); | ||
|
||
// Allow Glide's resource recycler to run on the main thread. | ||
concurrency.pokeMainThread(); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void transformFromRequestManager_withLoadedBitmap_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.load(bitmap) | ||
.centerCrop() | ||
.submit(100, 100)); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void clearFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
FutureTarget<Bitmap> target = | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.asBitmap() | ||
.load(bitmap) | ||
.submit(100, 100)); | ||
Glide.with(context).clear(target); | ||
|
||
// Allow Glide's resource recycler to run on the main thread. | ||
concurrency.pokeMainThread(); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void transformFromRequestBuilder_withLoadedBitmap_asBitmap_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.asBitmap() | ||
.load(bitmap) | ||
.centerCrop() | ||
.submit(100, 100)); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
instrumentation/src/androidTest/java/com/bumptech/glide/LoadDrawableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.bumptech.glide; | ||
|
||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.drawable.BitmapDrawable; | ||
import android.graphics.drawable.Drawable; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolAdapter; | ||
import com.bumptech.glide.load.engine.cache.MemoryCacheAdapter; | ||
import com.bumptech.glide.request.FutureTarget; | ||
import com.bumptech.glide.test.ConcurrencyHelper; | ||
import com.bumptech.glide.test.GlideApp; | ||
import com.bumptech.glide.test.ResourceIds; | ||
import com.bumptech.glide.test.TearDownGlide; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class LoadDrawableTest { | ||
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); | ||
private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); | ||
|
||
private Context context; | ||
|
||
@Before | ||
public void setUp() { | ||
context = InstrumentationRegistry.getTargetContext(); | ||
} | ||
|
||
@Test | ||
public void clear_withLoadedBitmapDrawable_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); | ||
FutureTarget<Drawable> target = | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.load(drawable) | ||
.submit(100, 100)); | ||
Glide.with(context).clear(target); | ||
|
||
// Allow Glide's resource recycler to run on the main thread. | ||
concurrency.pokeMainThread(); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void transform_withLoadedBitmapDrawable_doesNotRecycleBitmap() { | ||
Glide.init(context, new GlideBuilder() | ||
.setMemoryCache(new MemoryCacheAdapter()) | ||
.setBitmapPool(new BitmapPoolAdapter())); | ||
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), ResourceIds.raw.canonical); | ||
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); | ||
concurrency.wait( | ||
GlideApp.with(context) | ||
.load(drawable) | ||
.centerCrop() | ||
.submit(100, 100)); | ||
|
||
assertThat(bitmap.isRecycled()).isFalse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.