* An object managing a pool of Bitmap objects. * It currently only temporarily holds onto the cached bitmaps, * releasing them after a short time has passed without any consumer * claiming them. */
| 18 | * claiming them. |
| 19 | */ |
| 20 | class BitmapPool(context: Context, val bitmapConfig: Bitmap.Config, val logger: Logger) { |
| 21 | |
| 22 | val maxWidth: Int |
| 23 | val maxHeight: Int |
| 24 | |
| 25 | private val cachedBitmaps = mutableListOf<CachedBitmap>() |
| 26 | private var cleanUpTimer: Timer? = null |
| 27 | |
| 28 | init { |
| 29 | val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager |
| 30 | val displaySize = Point() |
| 31 | windowManager.defaultDisplay.getRealSize(displaySize) |
| 32 | maxWidth = displaySize.x |
| 33 | maxHeight = displaySize.y |
| 34 | } |
| 35 | |
| 36 | private class CachedBitmap(private val pool: BitmapPool, private var bitmap: Bitmap?): BitmapHandler { |
| 37 | |
| 38 | private val retainCount = AtomicInteger(1) |
| 39 | |
| 40 | var expirationTime = 0L |
| 41 | |
| 42 | private var isDirty = true |
| 43 | |
| 44 | val isUnused: Boolean |
| 45 | get() = retainCount.get() == 0 |
| 46 | |
| 47 | override fun getBitmap(): Bitmap { |
| 48 | return bitmap!! |
| 49 | } |
| 50 | |
| 51 | fun prepareForReuse() { |
| 52 | synchronized(this) { |
| 53 | if (!isDirty) { |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | isDirty = false |
| 58 | bitmap?.eraseColor(Color.TRANSPARENT) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fun markDirty() { |
| 63 | synchronized(this) { |
| 64 | isDirty = true |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fun destroy() { |
| 69 | synchronized(this) { |
| 70 | bitmap?.recycle() |
| 71 | bitmap = null |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | override fun retain() { |
| 76 | retainCount.incrementAndGet() |
| 77 | } |
no test coverage detected