From 2b8f4b0c7e6e887bf61dadcb3c5fd0d99bd12649 Mon Sep 17 00:00:00 2001 From: jonathanmos <48201295+jonathanmos@users.noreply.github.com> Date: Tue, 17 Dec 2024 14:48:27 +0000 Subject: [PATCH] Fix cache misses due to wrong drawable --- .../recorder/resources/ResourceResolver.kt | 12 ++-- .../resources/ResourceResolverTest.kt | 56 +++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolver.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolver.kt index 0ff79eaeb5..e436a3cebb 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolver.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolver.kt @@ -160,7 +160,8 @@ internal class ResourceResolver( if (handledBitmap == null) { tryToDrawNewBitmap( - drawable = copiedDrawable, + originalDrawable = drawable, + copiedDrawable = copiedDrawable, drawableWidth = drawableWidth, drawableHeight = drawableHeight, displayMetrics = displayMetrics, @@ -244,12 +245,14 @@ 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, @@ -257,7 +260,7 @@ internal class ResourceResolver( resolveResourceCallback: ResolveResourceCallback ) { drawableUtils.createBitmapOfApproxSizeFromDrawable( - drawable = drawable, + drawable = copiedDrawable, drawableWidth = drawableWidth, drawableHeight = drawableHeight, displayMetrics = displayMetrics, @@ -273,7 +276,7 @@ internal class ResourceResolver( } resolveResourceHash( - drawable = drawable, + drawable = originalDrawable, bitmap = bitmap, compressedBitmapBytes = compressedBitmapBytes, shouldCacheBitmap = true, @@ -339,6 +342,7 @@ internal class ResourceResolver( val cacheKey = key ?: bitmapCachesManager.generateResourceKeyFromDrawable(drawable) ?: return null + return bitmapCachesManager.getFromResourceCache(cacheKey) } diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolverTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolverTest.kt index cee6006dac..61a04739f2 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolverTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/ResourceResolverTest.kt @@ -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() + ) + } }