-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Paywalls: backwards compatible blurring #1327
Changes from 10 commits
4b22c1f
de56b10
cb65f39
1d51f1f
f28fce4
ed4e957
19ef51e
ef31b22
119c260
d5f4c95
1ef2a52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||
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( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's interesting that this is a suspend function... I guess in case the transformation takes a long time. |
||||||
input: Bitmap, | ||||||
size: Size, | ||||||
): Bitmap { | ||||||
return input.blur(context, radius) | ||||||
} | ||||||
|
||||||
override fun equals(other: Any?): Boolean { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I'm not sure if we need the equals and hashcode for something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, it uses |
||||||
if (this === other) return true | ||||||
return other is BlurTransformation && radius == other.radius | ||||||
} | ||||||
|
||||||
override fun hashCode(): Int = radius.hashCode() | ||||||
} | ||||||
|
||||||
// max radius supported by RenderScript | ||||||
private const val MAX_SUPPORTED_RADIUS = 25 | ||||||
|
||||||
internal fun Bitmap.blur(context: Context, radius: Float = 25f): Bitmap { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe? Or we could also define
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh thanks I forgot to use it here too. |
||||||
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 | ||||||
} |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disabled this so we don't run it every time unnecessarily. |
||
@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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I don't think we need the
return
here right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, fixed