Skip to content

Commit

Permalink
Make BitmapPool.maxSize an Int. (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite authored May 12, 2020
1 parent 5de8f37 commit d44d8db
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 38 deletions.
6 changes: 3 additions & 3 deletions coil-base/api/coil-base.api
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public final class coil/api/ImageLoaders {
public abstract interface class coil/bitmappool/BitmapPool {
public static final field Companion Lcoil/bitmappool/BitmapPool$Companion;
public abstract fun clear ()V
public static fun create (JLcoil/util/Logger;)Lcoil/bitmappool/BitmapPool;
public static fun create (ILcoil/util/Logger;)Lcoil/bitmappool/BitmapPool;
public abstract fun get (IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;
public abstract fun getDirty (IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;
public abstract fun getDirtyOrNull (IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;
Expand All @@ -233,8 +233,8 @@ public abstract interface class coil/bitmappool/BitmapPool {
}

public final class coil/bitmappool/BitmapPool$Companion {
public final fun create (JLcoil/util/Logger;)Lcoil/bitmappool/BitmapPool;
public static synthetic fun create$default (Lcoil/bitmappool/BitmapPool$Companion;JLcoil/util/Logger;ILjava/lang/Object;)Lcoil/bitmappool/BitmapPool;
public final fun create (ILcoil/util/Logger;)Lcoil/bitmappool/BitmapPool;
public static synthetic fun create$default (Lcoil/bitmappool/BitmapPool$Companion;ILcoil/util/Logger;ILjava/lang/Object;)Lcoil/bitmappool/BitmapPool;
}

public final class coil/collection/SparseIntArraySet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BitmapFactoryDecoderTest {
@Before
fun before() {
context = ApplicationProvider.getApplicationContext()
pool = BitmapPool(Long.MAX_VALUE)
pool = BitmapPool(Int.MAX_VALUE)
service = BitmapFactoryDecoder(context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SystemCallbacksTest {

@Test
fun trimMemoryCallsArePassedThrough() {
val bitmapPool = BitmapPool(Long.MAX_VALUE)
val bitmapPool = BitmapPool(Int.MAX_VALUE)
val weakMemoryCache = RealWeakMemoryCache()
val referenceCounter = BitmapReferenceCounter(weakMemoryCache, bitmapPool, null)
val memoryCache = MemoryCache(weakMemoryCache, referenceCounter, Int.MAX_VALUE, null)
Expand Down
2 changes: 1 addition & 1 deletion coil-base/src/main/java/coil/ImageLoaderBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class ImageLoaderBuilder(context: Context) {
*/
fun build(): ImageLoader {
val availableMemorySize = Utils.calculateAvailableMemorySize(applicationContext, availableMemoryPercentage)
val bitmapPoolSize = (bitmapPoolPercentage * availableMemorySize).toLong()
val bitmapPoolSize = (bitmapPoolPercentage * availableMemorySize).toInt()
val memoryCacheSize = (availableMemorySize - bitmapPoolSize).toInt()

val bitmapPool = BitmapPool(bitmapPoolSize, logger)
Expand Down
2 changes: 1 addition & 1 deletion coil-base/src/main/java/coil/bitmappool/BitmapPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface BitmapPool {
// @JvmOverloads https://youtrack.jetbrains.com/issue/KT-35716
@JvmName("create")
operator fun invoke(
maxSize: Long,
maxSize: Int,
logger: Logger? = null
): BitmapPool = RealBitmapPool(maxSize = maxSize, logger = logger)
}
Expand Down
32 changes: 13 additions & 19 deletions coil-base/src/main/java/coil/bitmappool/RealBitmapPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import android.os.Build.VERSION.SDK_INT
import android.util.Log
import androidx.annotation.Px
import androidx.collection.arraySetOf
import androidx.core.graphics.createBitmap
import coil.bitmappool.strategy.BitmapPoolStrategy
import coil.util.Logger
import coil.util.getAllocationByteCountCompat
import coil.util.isHardware
import coil.util.log

/**
Expand All @@ -23,7 +25,7 @@ import coil.util.log
* Glide's license information is available [here](https://github.com/bumptech/glide/blob/master/LICENSE).
*/
internal class RealBitmapPool(
private val maxSize: Long,
private val maxSize: Int,
private val allowedConfigs: Set<Bitmap.Config> = getDefaultAllowedConfigs(),
private val strategy: BitmapPoolStrategy = BitmapPoolStrategy(),
private val logger: Logger? = null
Expand All @@ -47,11 +49,11 @@ internal class RealBitmapPool(
}
}

private var currentSize: Long = 0
private var hits: Int = 0
private var misses: Int = 0
private var puts: Int = 0
private var evictions: Int = 0
private var currentSize = 0
private var hits = 0
private var misses = 0
private var puts = 0
private var evictions = 0

init {
require(maxSize >= 0) { "maxSize must be >= 0." }
Expand Down Expand Up @@ -86,24 +88,20 @@ internal class RealBitmapPool(
}

override fun get(@Px width: Int, @Px height: Int, config: Bitmap.Config): Bitmap {
val result = getOrNull(width, height, config)
return result ?: Bitmap.createBitmap(width, height, config)
return getOrNull(width, height, config) ?: createBitmap(width, height, config)
}

override fun getOrNull(@Px width: Int, @Px height: Int, config: Bitmap.Config): Bitmap? {
val result = getDirtyOrNull(width, height, config)
result?.eraseColor(Color.TRANSPARENT)
return result
return getDirtyOrNull(width, height, config)?.apply { eraseColor(Color.TRANSPARENT) }
}

override fun getDirty(@Px width: Int, @Px height: Int, config: Bitmap.Config): Bitmap {
val result = getDirtyOrNull(width, height, config)
return result ?: Bitmap.createBitmap(width, height, config)
return getDirtyOrNull(width, height, config) ?: createBitmap(width, height, config)
}

@Synchronized
override fun getDirtyOrNull(@Px width: Int, @Px height: Int, config: Bitmap.Config): Bitmap? {
assertNotHardwareConfig(config)
require(!config.isHardware) { "Cannot create a mutable hardware bitmap." }

val result = strategy.get(width, height, config)
if (result == null) {
Expand Down Expand Up @@ -151,7 +149,7 @@ internal class RealBitmapPool(
}

@Synchronized
private fun trimToSize(size: Long) {
private fun trimToSize(size: Int) {
while (currentSize > size) {
val removed = strategy.removeLast()
if (removed == null) {
Expand All @@ -169,10 +167,6 @@ internal class RealBitmapPool(
}
}

private fun assertNotHardwareConfig(config: Bitmap.Config) {
require(SDK_INT < 26 || config != Bitmap.Config.HARDWARE) { "Cannot create a mutable hardware Bitmap." }
}

private fun dump() {
logger?.log(TAG, Log.VERBOSE) { computeUnchecked() }
}
Expand Down
2 changes: 1 addition & 1 deletion coil-base/src/test/java/coil/RealImageLoaderBasicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RealImageLoaderBasicTest {
@Before
fun before() {
context = ApplicationProvider.getApplicationContext()
val bitmapPool = BitmapPool(Long.MAX_VALUE)
val bitmapPool = BitmapPool(Int.MAX_VALUE)
val weakMemoryCache = RealWeakMemoryCache()
val referenceCounter = BitmapReferenceCounter(weakMemoryCache, bitmapPool, null)
memoryCache = MemoryCache(weakMemoryCache, referenceCounter, Int.MAX_VALUE, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package coil.decode

import android.graphics.Bitmap
import android.graphics.drawable.VectorDrawable
import coil.bitmappool.RealBitmapPool
import coil.bitmappool.BitmapPool
import coil.size.PixelSize
import coil.size.Scale
import coil.util.size
Expand All @@ -19,7 +19,7 @@ class DrawableDecoderServiceTest {

@Before
fun before() {
service = DrawableDecoderService(RealBitmapPool(0))
service = DrawableDecoderService(BitmapPool(0))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coil.memory

import coil.bitmappool.BitmapPool
import coil.bitmappool.RealBitmapPool
import coil.memory.MemoryCache.Key
import coil.util.DEFAULT_BITMAP_SIZE
import coil.util.count
Expand All @@ -25,7 +24,7 @@ class BitmapReferenceCounterTest {
@Before
fun before() {
weakMemoryCache = RealWeakMemoryCache()
pool = RealBitmapPool(DEFAULT_BITMAP_SIZE)
pool = BitmapPool(DEFAULT_BITMAP_SIZE)
counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
}

Expand Down
12 changes: 6 additions & 6 deletions coil-base/src/test/java/coil/memory/MemoryCacheTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MemoryCacheTest {
@Test
fun `can retrieve cached value`() {
val weakMemoryCache = EmptyWeakMemoryCache
val pool = BitmapPool(Long.MAX_VALUE)
val pool = BitmapPool(Int.MAX_VALUE)
val counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
val cache = MemoryCache(weakMemoryCache, counter, (2 * DEFAULT_BITMAP_SIZE).toInt(), null)

Expand All @@ -30,7 +30,7 @@ class MemoryCacheTest {
@Test
fun `least recently used value is evicted`() {
val weakMemoryCache = EmptyWeakMemoryCache
val pool = BitmapPool(Long.MAX_VALUE)
val pool = BitmapPool(Int.MAX_VALUE)
val counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
val cache = MemoryCache(weakMemoryCache, counter, (2 * DEFAULT_BITMAP_SIZE).toInt(), null)

Expand All @@ -49,7 +49,7 @@ class MemoryCacheTest {
@Test
fun `maxSize 0 disables memory cache`() {
val weakMemoryCache = EmptyWeakMemoryCache
val pool = BitmapPool(Long.MAX_VALUE)
val pool = BitmapPool(Int.MAX_VALUE)
val counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
val cache = MemoryCache(weakMemoryCache, counter, 0, null)

Expand All @@ -62,7 +62,7 @@ class MemoryCacheTest {
@Test
fun `value is removed after invalidate is called`() {
val weakMemoryCache = RealWeakMemoryCache()
val pool = BitmapPool(Long.MAX_VALUE)
val pool = BitmapPool(Int.MAX_VALUE)
val counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
val cache = MemoryCache(weakMemoryCache, counter, (2 * DEFAULT_BITMAP_SIZE).toInt(), null)

Expand All @@ -76,7 +76,7 @@ class MemoryCacheTest {
@Test
fun `valid evicted item is added to bitmap pool`() {
val weakMemoryCache = RealWeakMemoryCache()
val pool = BitmapPool(Long.MAX_VALUE)
val pool = BitmapPool(Int.MAX_VALUE)
val counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
val cache = MemoryCache(weakMemoryCache, counter, DEFAULT_BITMAP_SIZE.toInt(), null)

Expand All @@ -96,7 +96,7 @@ class MemoryCacheTest {
@Test
fun `invalid evicted item is added to weak memory cache`() {
val weakMemoryCache = RealWeakMemoryCache()
val pool = BitmapPool(Long.MAX_VALUE)
val pool = BitmapPool(Int.MAX_VALUE)
val counter = BitmapReferenceCounter(weakMemoryCache, pool, null)
val cache = MemoryCache(weakMemoryCache, counter, DEFAULT_BITMAP_SIZE.toInt(), null)

Expand Down
2 changes: 1 addition & 1 deletion coil-base/src/test/java/coil/util/TestFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.setMain
import org.robolectric.Shadows

const val DEFAULT_BITMAP_SIZE = 40000L // 4 * 100 * 100
const val DEFAULT_BITMAP_SIZE = 40000 // 4 * 100 * 100

fun createBitmap(
width: Int = 100,
Expand Down

0 comments on commit d44d8db

Please sign in to comment.