-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paywalls: backwards compatible blurring (#1327)
Add backwards compatible blur. This has 3 different ways of applying blur: - through native blur in API 31+ - through averaging the pixels in the image, based on https://github.com/T8RIN/BlurTransformation - through RenderScript in API < 31 It also adds some logic to benchmark... But I've been having a tough time actually running the benchmarks. From multiDex to instrumentation tests and roboelectric, it's been fun. Will reach out tomorrow to android devs for a hand in actually running the benchmarks. --------- Co-authored-by: NachoSoto <ignaciosoto90@gmail.com>
- Loading branch information
Showing
5 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ui/src/main/kotlin/com/revenuecat/purchases/ui/revenuecatui/helpers/BlurTransformation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.renderscript.Allocation | ||
import android.renderscript.Element | ||
import android.renderscript.RenderScript | ||
import android.renderscript.ScriptIntrinsicBlur | ||
import coil.size.Size | ||
import coil.transform.Transformation | ||
import kotlin.math.min | ||
|
||
/** | ||
* BlurTransformation class applies a blur on a given Bitmap image. | ||
* The blurring is performed with RenderScript, and should only be used in API level < 31, since newer versions | ||
* of Android have their own native blur implementation. | ||
* | ||
* @property radius - The radius of the square used for blurring. Higher values | ||
* produce a more pronounced blur effect. | ||
*/ | ||
internal class BlurTransformation( | ||
private val context: Context, | ||
private val radius: Float, | ||
) : Transformation { | ||
override val cacheKey: String = "${javaClass.name}-$radius" | ||
|
||
override suspend fun transform( | ||
input: Bitmap, | ||
size: Size, | ||
): Bitmap { | ||
return input.blur(context, radius) | ||
} | ||
} | ||
|
||
// max radius supported by RenderScript | ||
private const val MAX_SUPPORTED_RADIUS = 25f | ||
|
||
internal fun Bitmap.blur(context: Context, radius: Float = MAX_SUPPORTED_RADIUS): Bitmap { | ||
if (radius < 1f) { | ||
return this@blur | ||
} | ||
val updatedRadius = min(radius.toDouble(), MAX_SUPPORTED_RADIUS.toDouble()) | ||
|
||
val rs = RenderScript.create(context) | ||
val input = Allocation.createFromBitmap(rs, this) | ||
val output = Allocation.createTyped(rs, input.type) | ||
val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)) | ||
|
||
script.setRadius(updatedRadius.toFloat()) | ||
script.setInput(input) | ||
script.forEach(output) | ||
|
||
val blurredBitmap = Bitmap.createBitmap(width, height, config) | ||
output.copyTo(blurredBitmap) | ||
|
||
input.destroy() | ||
output.destroy() | ||
script.destroy() | ||
rs.destroy() | ||
|
||
return blurredBitmap | ||
} |
45 changes: 45 additions & 0 deletions
45
...tui/src/test/kotlin/com/revenuecat/purchases/ui/revenuecatui/helpers/BlurBenchmarkTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.helpers | ||
|
||
import android.content.ContentValues.TAG | ||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.util.Log | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import blur | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Ignore | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class BlurBenchmarkTest { | ||
@Ignore("Test is for checking performance only") | ||
@Test | ||
fun `Blur images using native blur`() = runBlocking { | ||
benchmarkBlur("Native Blur") { image, context, radius -> | ||
image.blur(context = context, radius = radius) | ||
} | ||
} | ||
|
||
private suspend fun benchmarkBlur(functionName: String, blurFunction: suspend (Bitmap, Context, Float) -> Unit) { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
val image = openImage() | ||
val radius = 25f | ||
val iterations = 100 | ||
val startTime = System.currentTimeMillis() | ||
repeat(iterations) { | ||
blurFunction(image, context, radius) | ||
} | ||
val endTime = System.currentTimeMillis() | ||
val totalTime = endTime - startTime | ||
val averageTime = totalTime / iterations | ||
Log.d(TAG, "$functionName:\nTotal time: $totalTime ms, Average time: $averageTime ms") | ||
} | ||
|
||
private fun openImage(): Bitmap { | ||
val inputStream = javaClass.classLoader!!.getResource("sample_blurring_image.jpeg").openStream() | ||
return BitmapFactory.decodeStream(inputStream) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.