Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an empty bitmap pool implementation. #561

Merged
merged 6 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion coil-base/src/main/java/coil/bitmap/BitmapPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ interface BitmapPool {
*/
@JvmStatic
@JvmName("create")
operator fun invoke(maxSize: Int): BitmapPool = RealBitmapPool(maxSize)
operator fun invoke(maxSize: Int): BitmapPool {
return if (maxSize == 0) EmptyBitmapPool() else RealBitmapPool(maxSize)
}
}

/**
Expand Down
35 changes: 35 additions & 0 deletions coil-base/src/main/java/coil/bitmap/EmptyBitmapPool.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package coil.bitmap

import android.graphics.Bitmap
import androidx.core.graphics.createBitmap
import coil.util.isHardware

/** A lock-free [BitmapPool] implementation that recycles any [Bitmap]s that are added to it. */
internal class EmptyBitmapPool : BitmapPool {

override fun put(bitmap: Bitmap) {
bitmap.recycle()
}

override fun get(width: Int, height: Int, config: Bitmap.Config) = getDirty(width, height, config)

override fun getOrNull(width: Int, height: Int, config: Bitmap.Config) = getDirtyOrNull(width, height, config)

override fun getDirty(width: Int, height: Int, config: Bitmap.Config): Bitmap {
assertNotHardware(config)
return createBitmap(width, height, config)
}

override fun getDirtyOrNull(width: Int, height: Int, config: Bitmap.Config): Bitmap? {
assertNotHardware(config)
return null
}

override fun trimMemory(level: Int) {}

override fun clear() {}

private fun assertNotHardware(config: Bitmap.Config) {
require(!config.isHardware) { "Cannot create a mutable hardware bitmap." }
}
}
34 changes: 31 additions & 3 deletions coil-base/src/test/java/coil/bitmap/RealBitmapPoolTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL
import android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
import android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
import android.graphics.Bitmap
import android.os.Build.VERSION.SDK_INT
import coil.util.DEFAULT_BITMAP_SIZE
import coil.util.createBitmap
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

@RunWith(RobolectricTestRunner::class)
Expand Down Expand Up @@ -55,7 +58,7 @@ class RealBitmapPoolTest {
@Test
fun `clear memory removes all bitmaps`() {
pool.fill(MAX_BITMAPS)
pool.clearMemory()
pool.clear()

assertEquals(MAX_BITMAPS, strategy.numRemoves)
}
Expand All @@ -64,7 +67,7 @@ class RealBitmapPoolTest {
fun `evicted bitmaps are recycled`() {
pool.fill(MAX_BITMAPS)
val bitmaps = strategy.bitmaps.toList()
pool.clearMemory()
pool.clear()

bitmaps.forEach { assertTrue(it.isRecycled) }
}
Expand Down Expand Up @@ -152,7 +155,32 @@ class RealBitmapPoolTest {
assertEquals(1, strategy.numPuts)
}

private fun RealBitmapPool.fill(fillCount: Int) {
@Test
fun `real - getting a hardware bitmap throws`() {
assumeTrue(SDK_INT >= 26)

val bitmap = createBitmap(config = Bitmap.Config.HARDWARE)

pool.put(bitmap)

assertFailsWith<IllegalArgumentException> { pool.get(bitmap.width, bitmap.height, bitmap.config) }
assertFailsWith<IllegalArgumentException> { pool.getOrNull(bitmap.width, bitmap.height, bitmap.config) }
}

@Test
fun `empty - getting a hardware bitmap throws`() {
assumeTrue(SDK_INT >= 26)

val pool = EmptyBitmapPool()
val bitmap = createBitmap(config = Bitmap.Config.HARDWARE)

pool.put(bitmap)

assertFailsWith<IllegalArgumentException> { pool.get(bitmap.width, bitmap.height, bitmap.config) }
assertFailsWith<IllegalArgumentException> { pool.getOrNull(bitmap.width, bitmap.height, bitmap.config) }
}

private fun BitmapPool.fill(fillCount: Int) {
repeat(fillCount) { put(createBitmap()) }
}

Expand Down
3 changes: 2 additions & 1 deletion coil-base/src/test/java/coil/util/TestFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package coil.util

import android.graphics.Bitmap
import android.os.Build.VERSION.SDK_INT
import android.os.Looper
import androidx.core.graphics.createBitmap
import org.robolectric.Shadows
Expand All @@ -13,7 +14,7 @@ fun createBitmap(
width: Int = 100,
height: Int = 100,
config: Bitmap.Config = Bitmap.Config.ARGB_8888,
isMutable: Boolean = true
isMutable: Boolean = SDK_INT < 26 || config != Bitmap.Config.HARDWARE
): Bitmap {
val bitmap = createBitmap(width, height, config)
Shadows.shadowOf(bitmap).setMutable(isMutable)
Expand Down
1 change: 1 addition & 0 deletions coil-gif/api/coil-gif.api
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public final class coil/decode/ImageDecoderDecoder$Companion {
public final class coil/drawable/MovieDrawable : android/graphics/drawable/Drawable, androidx/vectordrawable/graphics/drawable/Animatable2Compat {
public static final field Companion Lcoil/drawable/MovieDrawable$Companion;
public static final field REPEAT_INFINITE I
public fun <init> (Landroid/graphics/Movie;)V
public fun <init> (Landroid/graphics/Movie;Lcoil/bitmap/BitmapPool;)V
public fun <init> (Landroid/graphics/Movie;Lcoil/bitmap/BitmapPool;Landroid/graphics/Bitmap$Config;)V
public fun <init> (Landroid/graphics/Movie;Lcoil/bitmap/BitmapPool;Landroid/graphics/Bitmap$Config;Lcoil/size/Scale;)V
Expand Down
2 changes: 1 addition & 1 deletion coil-gif/src/main/java/coil/drawable/MovieDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import coil.size.Scale
*/
class MovieDrawable @JvmOverloads constructor(
private val movie: Movie,
private val pool: BitmapPool,
private val pool: BitmapPool = BitmapPool(0),
val config: Bitmap.Config = Bitmap.Config.ARGB_8888,
val scale: Scale = Scale.FIT
) : Drawable(), Animatable2Compat {
Expand Down