Skip to content

Commit

Permalink
Fix cache misses due to wrong drawable
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Dec 17, 2024
1 parent 9f3d758 commit 2b8f4b0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ internal class ResourceResolver(

if (handledBitmap == null) {
tryToDrawNewBitmap(
drawable = copiedDrawable,
originalDrawable = drawable,
copiedDrawable = copiedDrawable,
drawableWidth = drawableWidth,
drawableHeight = drawableHeight,
displayMetrics = displayMetrics,
Expand Down Expand Up @@ -244,20 +245,22 @@ internal class ResourceResolver(
val key = customResourceIdCacheKey
?: bitmapCachesManager.generateResourceKeyFromDrawable(drawable)
?: return

bitmapCachesManager.putInResourceCache(key, resourceId)
}

@WorkerThread
private fun tryToDrawNewBitmap(
drawable: Drawable,
originalDrawable: Drawable,
copiedDrawable: Drawable,
drawableWidth: Int,
drawableHeight: Int,
displayMetrics: DisplayMetrics,
customResourceIdCacheKey: String?,
resolveResourceCallback: ResolveResourceCallback
) {
drawableUtils.createBitmapOfApproxSizeFromDrawable(
drawable = drawable,
drawable = copiedDrawable,
drawableWidth = drawableWidth,
drawableHeight = drawableHeight,
displayMetrics = displayMetrics,
Expand All @@ -273,7 +276,7 @@ internal class ResourceResolver(
}

resolveResourceHash(
drawable = drawable,
drawable = originalDrawable,
bitmap = bitmap,
compressedBitmapBytes = compressedBitmapBytes,
shouldCacheBitmap = true,
Expand Down Expand Up @@ -339,6 +342,7 @@ internal class ResourceResolver(
val cacheKey = key
?: bitmapCachesManager.generateResourceKeyFromDrawable(drawable)
?: return null

return bitmapCachesManager.getFromResourceCache(cacheKey)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,4 +1037,60 @@ internal class ResourceResolverTest {
applicationId = fakeApplicationid.toString(),
bitmapCachesManager = mockBitmapCachesManager
)

@Test
fun `M use original drawable for cache write W resolveResourceId() { cache miss }`(
@Mock mockCopiedDrawable: Drawable
) {
// Given
whenever(mockDrawableCopier.copy(mockDrawable, mockResources)).thenReturn(mockCopiedDrawable)

// When
testedResourceResolver.resolveResourceId(
resources = mockResources,
applicationContext = mockApplicationContext,
displayMetrics = mockDisplayMetrics,
originalDrawable = mockDrawable,
drawableCopier = mockDrawableCopier,
drawableWidth = mockDrawable.intrinsicWidth,
drawableHeight = mockDrawable.intrinsicHeight,
customResourceIdCacheKey = null,
resourceResolverCallback = mockSerializerCallback
)

// Then
verify(mockBitmapCachesManager, times(2)).generateResourceKeyFromDrawable(mockDrawable)
}

@Test
fun `M use copy of the drawable for creating the bitmap W resolveResourceId() { cache miss }`(
@Mock mockCopiedDrawable: Drawable
) {
// Given
whenever(mockDrawableCopier.copy(mockDrawable, mockResources)).thenReturn(mockCopiedDrawable)

// When
testedResourceResolver.resolveResourceId(
resources = mockResources,
applicationContext = mockApplicationContext,
displayMetrics = mockDisplayMetrics,
originalDrawable = mockDrawable,
drawableCopier = mockDrawableCopier,
drawableWidth = mockDrawable.intrinsicWidth,
drawableHeight = mockDrawable.intrinsicHeight,
customResourceIdCacheKey = null,
resourceResolverCallback = mockSerializerCallback
)

// Then
verify(mockDrawableUtils).createBitmapOfApproxSizeFromDrawable(
drawable = eq(mockCopiedDrawable),
drawableWidth = any(),
drawableHeight = any(),
displayMetrics = any(),
requestedSizeInBytes = any(),
config = any(),
bitmapCreationCallback = any()
)
}
}

0 comments on commit 2b8f4b0

Please sign in to comment.