From 83d1bafcf8519cd52548bdceafe0638a59a455f5 Mon Sep 17 00:00:00 2001 From: Anthonyy232 Date: Sat, 28 Mar 2026 15:17:24 -0700 Subject: [PATCH] perf: prevent OOM from oversized FILL bitmaps and BOTH-mode double allocation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../paperize/core/util/WallpaperUtil.kt | 34 +++++++++---- .../wallpaper/WallpaperChangeService.kt | 48 +++++++++---------- .../service/worker/WallpaperChangeWorker.kt | 40 ++++++++-------- 3 files changed, 69 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt b/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt index a31d59f..a875897 100644 --- a/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt +++ b/app/src/main/java/com/anthonyla/paperize/core/util/WallpaperUtil.kt @@ -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 } diff --git a/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt b/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt index a35fa7a..7f76de4 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/wallpaper/WallpaperChangeService.kt @@ -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) diff --git a/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt b/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt index 1029170..ea563cf 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperChangeWorker.kt @@ -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")