From 3bc438a06d15ce0b246d4b4d40201f21e2c6aa72 Mon Sep 17 00:00:00 2001 From: Anthonyy232 Date: Wed, 13 Aug 2025 19:50:49 -0700 Subject: [PATCH] Migrated blur to rendereffect and min version to 31 --- app/build.gradle.kts | 12 +-- .../anthonyla/paperize/core/WallpaperUtil.kt | 89 +++++++++++++++---- .../wallpaper/presentation/MainActivity.kt | 6 +- .../wallpaper_service/LockWallpaperService.kt | 14 +-- gradle/libs.versions.toml | 38 ++++---- 5 files changed, 102 insertions(+), 57 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4980466..f4d983e 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -13,14 +13,15 @@ ksp { android { namespace = "com.anthonyla.paperize" - compileSdk = 35 + compileSdk = 36 defaultConfig { applicationId = "com.anthonyla.paperize" - minSdk = 26 - targetSdk = 37 - versionCode = 37 - versionName = "3.0.0" + minSdk = 31 + targetSdk = 36 + versionCode = 38 + versionName = "3.1.0" + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary = true @@ -115,7 +116,6 @@ dependencies { implementation(libs.androidx.glance.appwidget) implementation(libs.androidx.glance.material3) implementation(libs.androidx.glance.material) - implementation(files("dependencies/renderscript-toolkit-release.aar")) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt b/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt index aa8c6c6..0966c9d 100644 --- a/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt +++ b/app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt @@ -8,6 +8,7 @@ import android.graphics.Canvas import android.graphics.Color import android.graphics.ColorMatrix import android.graphics.ColorMatrixColorFilter +import android.graphics.HardwareRenderer import android.graphics.ImageDecoder import android.graphics.Matrix import android.graphics.Paint @@ -32,7 +33,6 @@ import com.anthonyla.paperize.core.ScreenMetricsCompat.getScreenSize import com.anthonyla.paperize.feature.wallpaper.domain.model.Folder import com.anthonyla.paperize.feature.wallpaper.domain.model.Metadata import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper -import com.google.android.renderscript.Toolkit import com.lazygeniouz.dfc.file.DocumentFileCompat import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -41,6 +41,13 @@ import java.io.ByteArrayOutputStream import java.nio.charset.Charset import java.util.zip.DeflaterOutputStream import java.util.zip.InflaterInputStream +import android.graphics.PixelFormat +import android.graphics.RenderEffect +import android.graphics.RenderNode +import android.hardware.HardwareBuffer +import android.media.ImageReader +import kotlin.compareTo +import kotlin.or enum class Type { HOME, LOCK, SINGLE, REFRESH } @@ -313,14 +320,62 @@ fun darkenBitmap(source: Bitmap, brightnessToRetainPercent: Int): Bitmap { * Blur the bitmap by a certain percentage */ fun blurBitmap(source: Bitmap, percent: Int): Bitmap { - if (percent <= 0) return source - return try { - val radius = (percent.coerceIn(0, 100) * 0.25f).coerceIn(1f, 25f) - Toolkit.blur(source, radius.toInt()) - } catch (e: Exception) { - Log.e("WallpaperUtil", "Error blurring bitmap (Renderscript): $e") - source + val clampedPercent = percent.coerceIn(0, 100) + if (clampedPercent == 0) { + return source } + + val maxBlurRadius = 25.0f + val radius = (clampedPercent / 100.0f) * maxBlurRadius + + val imageReader = ImageReader.newInstance( + source.width, source.height, + PixelFormat.RGBA_8888, 1, + HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE or HardwareBuffer.USAGE_GPU_COLOR_OUTPUT + ) + + val renderNode = RenderNode("BlurEffect") + val hardwareRenderer = HardwareRenderer() + + var resultBitmap: Bitmap? + try { + hardwareRenderer.setSurface(imageReader.surface) + hardwareRenderer.setContentRoot(renderNode) + renderNode.setPosition(0, 0, source.width, source.height) + + val blurEffect = RenderEffect.createBlurEffect(radius, radius, Shader.TileMode.MIRROR) + renderNode.setRenderEffect(blurEffect) + + val canvas = renderNode.beginRecording() + canvas.drawBitmap(source, 0f, 0f, null) + renderNode.endRecording() + + hardwareRenderer.createRenderRequest() + .setWaitForPresent(true) + .syncAndDraw() + + val image = imageReader.acquireNextImage() + ?: throw IllegalStateException("Failed to acquire blurred image") + + val hardwareBuffer = image.hardwareBuffer + ?: throw IllegalStateException("Failed to acquire hardware buffer") + + val hardwareBitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, null) + ?: throw IllegalStateException("Failed to create bitmap from hardware buffer") + + // Convert hardware bitmap to regular bitmap for further processing + resultBitmap = hardwareBitmap.copy(Bitmap.Config.ARGB_8888, false) + + hardwareBuffer.close() + image.close() + + } finally { + hardwareRenderer.destroy() + renderNode.discardDisplayList() + imageReader.close() + } + + return resultBitmap } /** @@ -649,6 +704,12 @@ fun processBitmap( try { var processedBitmap = source + // Apply blur first on the original size for better quality + if (blur && blurPercent > 0) { + processedBitmap = blurBitmap(processedBitmap, blurPercent) + } + + // Then scale the bitmap processedBitmap = when (scaling) { ScalingConstants.FILL -> fillBitmap(processedBitmap, width, height) ScalingConstants.FIT -> fitBitmap(processedBitmap, width, height) @@ -656,8 +717,9 @@ fun processBitmap( ScalingConstants.NONE -> processedBitmap } + // Ensure mutability for color effects if (!processedBitmap.isMutable && (darken || vignette || grayscale)) { - Log.w("ProcessBitmap", "Bitmap became immutable before effects. Making a mutable copy.") + Log.w("ProcessBitmap", "Bitmap became immutable. Making a mutable copy.") processedBitmap = processedBitmap.copy(processedBitmap.config ?: Bitmap.Config.ARGB_8888, true) } @@ -665,15 +727,6 @@ fun processBitmap( processedBitmap = darkenBitmap(processedBitmap, darkenPercent) } - if (blur && blurPercent > 0) { - val blurred = blurBitmap(processedBitmap, blurPercent) - processedBitmap = blurred - if (!processedBitmap.isMutable && (vignette || grayscale)) { - Log.w("ProcessBitmap", "Bitmap became immutable after blur. Making a mutable copy.") - processedBitmap = processedBitmap.copy(processedBitmap.config ?: Bitmap.Config.ARGB_8888, true) - } - } - if (vignette && vignettePercent > 0) { processedBitmap = vignetteBitmap(processedBitmap, vignettePercent) } diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt index 610c9ef..86d8abd 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/presentation/MainActivity.kt @@ -92,11 +92,7 @@ class MainActivity : ComponentActivity() { val refreshIntent = Intent(this, HomeWallpaperService::class.java).apply { action = HomeWallpaperService.Actions.REFRESH.toString() } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { - startForegroundService(refreshIntent) - } else { - startService(refreshIntent) - } + startForegroundService(refreshIntent) val splashScreen = installSplashScreen() diff --git a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/LockWallpaperService.kt b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/LockWallpaperService.kt index 269522b..5fff58f 100644 --- a/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/LockWallpaperService.kt +++ b/app/src/main/java/com/anthonyla/paperize/feature/wallpaper/wallpaper_service/LockWallpaperService.kt @@ -297,12 +297,6 @@ class LockWallpaperService: Service() { } } } -// // Refreshing home wallpaper to keep dynamic theming color based on home screen wallpaper -// if (homeInterval != lockInterval) { -// delay(15000) -// val serviceIntent = Intent(context, HomeWallpaperService::class.java).apply { this.action = HomeWallpaperService.Actions.UPDATE.toString() } -// context.startService(serviceIntent) -// } } // Case: Set home and lock screen wallpapers using the same album (home screen album) settings.setHome && settings.setLock && !scheduleSeparately -> { /* handled by home wallpaper service */ return } @@ -329,14 +323,14 @@ class LockWallpaperService: Service() { context = context, wallpaper = wallpaper.decompress("content://com.android.externalstorage.documents/").toUri(), darken = settings.darken, - darkenPercent = if (settings.setHome) settings.homeDarkenPercentage else settings.lockDarkenPercentage, + darkenPercent = settings.lockDarkenPercentage, scaling = settings.scaling, blur = settings.blur, - blurPercent = if (settings.setHome) settings.homeBlurPercentage else settings.lockBlurPercentage, + blurPercent = settings.lockBlurPercentage, vignette = settings.vignette, - vignettePercent = if (settings.setHome) settings.homeVignettePercentage else settings.lockVignettePercentage, + vignettePercent = settings.lockVignettePercentage, grayscale = settings.grayscale, - grayscalePercent = if (settings.setHome) settings.homeGrayscalePercentage else settings.lockGrayscalePercentage + grayscalePercent = settings.lockGrayscalePercentage ) } else { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d7c721e..2d6d9c1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,30 +1,31 @@ [versions] -androidGradlePlugin = "8.10.1" -kotlin = "2.1.21" -hilt = "2.56.2" -ksp = "2.1.21-2.0.2" +androidGradlePlugin = "8.12.0" +kotlin = "2.2.0" +hilt = "2.57" +ksp = "2.2.0-2.0.2" +renderscriptToolkit = "1.0.0" [libraries] accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version = "0.37.3" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version = "1.10.1" } androidx-animation = { module = "androidx.compose.animation:animation", version = "1.8.3" } -androidx-compose-bom = { module = "androidx.compose:compose-bom", version = "2025.06.01" } +androidx-compose-bom = { module = "androidx.compose:compose-bom", version = "2025.07.00" } androidx-core-ktx = { module = "androidx.core:core-ktx", version = "1.16.0" } -androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version = "1.2.0-beta02" } +androidx-core-splashscreen = { module = "androidx.core:core-splashscreen", version = "1.2.0-rc01" } androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version = "1.1.7" } androidx-datastore = { module = "androidx.datastore:datastore", version = "1.1.7" } androidx-documentfile = { module = "androidx.documentfile:documentfile", version = "1.1.0" } -androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version = "3.6.1" } +androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version = "3.7.0" } androidx-foundation = { module = "androidx.compose.foundation:foundation", version = "1.8.3" } androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version = "1.2.0" } androidx-hilt-work = { module = "androidx.hilt:hilt-work", version = "1.2.0" } -androidx-junit = { module = "androidx.test.ext:junit", version = "1.2.1" } -androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version = "2.9.1" } -androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version = "2.9.1" } +androidx-junit = { module = "androidx.test.ext:junit", version = "1.3.0" } +androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version = "2.9.2" } +androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version = "2.9.2" } androidx-material = { module = "androidx.compose.material:material", version = "1.8.3" } androidx-material-icons-extended = { module = "androidx.compose.material:material-icons-extended", version = "1.7.8" } -androidx-material3 = { module = "androidx.compose.material3:material3", version = "1.4.0-alpha16" } -androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version = "2.9.0" } +androidx-material3 = { module = "androidx.compose.material3:material3", version = "1.4.0-beta01" } +androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version = "2.9.3" } androidx-profileinstaller = { module = "androidx.profileinstaller:profileinstaller", version = "1.4.1" } androidx-room-ktx = { module = "androidx.room:room-ktx", version = "2.7.2" } androidx-room-compiler = { module = "androidx.room:room-compiler", version = "2.7.2" } @@ -35,23 +36,24 @@ androidx-ui-test-junit4 = { module = "androidx.compose.ui:ui-test-junit4", versi androidx-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", version = "1.8.3" } androidx-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version = "1.8.3" } androidx-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version = "1.8.3" } -androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version = "2.10.2" } +androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version = "2.10.3" } dfc = { module = "com.lazygeniouz:dfc", version = "1.2" } gson = { module = "com.google.code.gson:gson", version = "2.13.1" } -hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version = "2.56.2" } -hilt-android = { module = "com.google.dagger:hilt-android", version = "2.56.2" } +hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" } +hilt-android-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "hilt" } junit = { module = "junit:junit", version = "4.13.2" } -kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.8.1" } -landscapist-glide = { module = "com.github.skydoves:landscapist-glide", version = "2.5.0" } +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.9.0" } +landscapist-glide = { module = "com.github.skydoves:landscapist-glide", version = "2.5.1" } lazycolumnscrollbar = { module = "com.github.nanihadesuka:LazyColumnScrollbar", version = "2.2.0" } lottie-compose = { module = "com.airbnb.android:lottie-compose", version = "6.6.7" } taskerpluginlibrary = { module = "com.joaomgcd:taskerpluginlibrary", version = "0.4.10" } toolbar-compose = { module = "me.onebone:toolbar-compose", version = "2.3.5" } -zoomable = { module = "net.engawapg.lib:zoomable", version = "2.8.0" } +zoomable = { module = "net.engawapg.lib:zoomable", version = "2.8.1" } reorderable = { group = "sh.calvin.reorderable", name = "reorderable", version = "2.5.1" } androidx-glance-appwidget = { group = "androidx.glance", name = "glance-appwidget", version = "1.1.1" } androidx-glance-material3 = { group = "androidx.glance", name = "glance-material3", version = "1.1.1" } androidx-glance-material = { group = "androidx.glance", name = "glance-material", version = "1.1.1" } +renderscript-toolkit = { group = "io.github.anthonyy232", name = "renderscript-toolkit", version.ref = "renderscriptToolkit" } [plugins] androidApplication = { id = "com.android.application", version.ref = "androidGradlePlugin" }