perf: prevent OOM from oversized FILL bitmaps and BOTH-mode double allocation

Two memory optimizations:

1. ImageDecoder FILL path: use setCrop() to crop to canvas during decode
   instead of allocating the full fill-scale bitmap first. For a portrait
   2000x3000 image on a 4032x2992 parallax canvas, this avoids a ~97MB
   intermediate (4032x6048) — the output is directly 4032x2992 (~48MB).

2. BOTH mode: recycle the HOME bitmap before rendering the LOCK bitmap,
   so they are never in memory simultaneously. Removes the fallback that
   reused the HOME bitmap for LOCK (which kept it alive during LOCK
   rendering). If LOCK rendering fails, the lock screen retains its
   previous wallpaper rather than getting the oversized HOME bitmap.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude Opus 4.6 committed 2026-03-28 15:17:24 -07:00
1 parent 2abf5a1804
commit 83d1bafcf8
3 files changed
+69 -53

No files matched your search

@@ -199,28 +199,46 @@ fun retrieveBitmap(
val result = ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
val srcWidth = info.size.width
val srcHeight = info.size.height
val (targetWidth, targetHeight) = when (scaling) {
when (scaling) {
ScalingType.FILL -> {
// Scale to fill, then crop to canvas during decode.
// setCrop avoids allocating the full fill-scale bitmap (which can be
// much taller/wider than the canvas) — major OOM prevention.
val scale = maxOf(width.toFloat() / srcWidth, height.toFloat() / srcHeight)
Pair((srcWidth * scale).fastRoundToInt(), (srcHeight * scale).fastRoundToInt())
val targetW = (srcWidth * scale).fastRoundToInt()
val targetH = (srcHeight * scale).fastRoundToInt()
decoder.setTargetSize(targetW, targetH)
if (targetW > width || targetH > height) {
val cropX = ((targetW - width) / 2).coerceAtLeast(0)
val cropY = ((targetH - height) / 2).coerceAtLeast(0)
decoder.setCrop(android.graphics.Rect(
cropX, cropY, cropX + width, cropY + height
))
}
}
ScalingType.FIT -> {
val scale = minOf(width.toFloat() / srcWidth, height.toFloat() / srcHeight)
Pair((srcWidth * scale).fastRoundToInt(), (srcHeight * scale).fastRoundToInt())
decoder.setTargetSize(
(srcWidth * scale).fastRoundToInt(),
(srcHeight * scale).fastRoundToInt()
)
}
ScalingType.STRETCH -> Pair(width, height)
ScalingType.STRETCH -> decoder.setTargetSize(width, height)
ScalingType.NONE -> {
val maxWidth = width * 2
val maxHeight = height * 2
if (srcWidth > maxWidth || srcHeight > maxHeight) {
val scale = minOf(maxWidth.toFloat() / srcWidth, maxHeight.toFloat() / srcHeight)
Pair((srcWidth * scale).fastRoundToInt(), (srcHeight * scale).fastRoundToInt())
} else {
Pair(srcWidth, srcHeight)
decoder.setTargetSize(
(srcWidth * scale).fastRoundToInt(),
(srcHeight * scale).fastRoundToInt()
)
}
// else: decode at original resolution
}
}
decoder.setTargetSize(targetWidth, targetHeight)
decoder.isMutableRequired = true
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
}
@@ -188,36 +188,34 @@ class WallpaperChangeService : Service() {
Log.w(TAG, "Failed to sync LOCK queue in BOTH mode", e)
}
// Render a separate bitmap for the lock screen at physical
// screen dimensions. The HOME bitmap was sized for the
// launcher's parallax canvas which is typically wider than
// the screen; reusing it for LOCK would let Android
// center-crop it, defeating FIT/NONE scaling modes.
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
lockResult.onSuccess { lockBitmap ->
try {
wallpaperManager.setBitmap(
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
)
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
} catch (e: Exception) {
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
} finally {
lockBitmap.recycle()
}
}.onError {
// Fallback: use the HOME bitmap for LOCK (old behavior)
Log.w(TAG, "Lock rerender failed in BOTH mode, using HOME bitmap")
wallpaperManager.setBitmap(
bitmap, null, true, WallpaperManager.FLAG_LOCK
)
}
} catch (e: Exception) {
Log.e(TAG, "Error setting wallpaper for both screens", e)
} finally {
// Always recycle bitmap to prevent memory leaks
// Recycle HOME bitmap BEFORE rendering LOCK to avoid
// holding both in memory (~48MB + ~16MB).
bitmap.recycle()
}
// Render a separate bitmap for the lock screen at physical
// screen dimensions. The HOME bitmap was sized for the
// launcher's parallax canvas which is typically wider than
// the screen; reusing it for LOCK would let Android
// center-crop it, defeating FIT/NONE scaling modes.
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
lockResult.onSuccess { lockBitmap ->
try {
wallpaperManager.setBitmap(
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
)
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
} catch (e: Exception) {
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
} finally {
lockBitmap.recycle()
}
}.onError { error ->
Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}")
}
}.onError { error ->
if (error is EmptyAlbumException) {
handleEmptyAlbumError(ScreenType.BOTH)
@@ -143,32 +143,32 @@ class WallpaperChangeWorker @AssistedInject constructor(
Log.w(TAG, "Failed to sync LOCK queue in BOTH mode", e)
}
// Render a separate bitmap for the lock screen at physical
// screen dimensions (no parallax on lock screen).
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
lockResult.onSuccess { lockBitmap ->
try {
wallpaperManager.setBitmap(
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
)
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
} catch (e: Exception) {
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
} finally {
lockBitmap.recycle()
}
}.onError {
Log.w(TAG, "Lock rerender failed in BOTH mode, using HOME bitmap")
wallpaperManager.setBitmap(
bitmap, null, true, WallpaperManager.FLAG_LOCK
)
}
} catch (e: Exception) {
Log.e(TAG, "Error setting wallpaper for both screens", e)
throw e
} finally {
// Recycle HOME bitmap BEFORE rendering LOCK to avoid
// holding both in memory simultaneously.
bitmap.recycle()
}
// Render a separate bitmap for the lock screen at physical
// screen dimensions (no parallax on lock screen).
val lockResult = reapplyEffectsUseCase(homeAlbumId, ScreenType.LOCK)
lockResult.onSuccess { lockBitmap ->
try {
wallpaperManager.setBitmap(
lockBitmap, null, true, WallpaperManager.FLAG_LOCK
)
Log.d(TAG, "Lock wallpaper set separately in BOTH mode")
} catch (e: Exception) {
Log.e(TAG, "Error setting lock wallpaper in BOTH mode", e)
} finally {
lockBitmap.recycle()
}
}.onError { error ->
Log.w(TAG, "Lock rerender failed in BOTH mode: ${error.message}")
}
}.onError { error ->
if (error is EmptyAlbumException) {
Log.w(TAG, "Album is empty for BOTH screens — disabling changer")