Repository navigation
feat(replay): capture screenshots at one pixel per dp by default with a filtered downscale #906
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| --- | ||
| "posthog-android": minor | ||
| --- | ||
|
|
||
| Session replay screenshots now default to one pixel per dp (about 1/9 of the pixels on a 3x screen, so smaller uploads), using a filtered downscale that keeps small text readable. Set `screenshotScale = 1f` to keep native resolution. | ||
|
|
||
| **Breaking change to the experimental `screenshotScale`:** its type changes from `Float` to `Float?`, and `null` (the new default) means one pixel per dp. Explicit values keep their meaning, and assigning one (`screenshotScale = 0.5f`) still compiles. Code compiled against 3.63.x must be rebuilt. Kotlin code that reads it into a `Float` no longer compiles, and Java code that reads it into a `float` throws `NullPointerException` while it is unset. |
This file contains hidden or 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 hidden or 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 hidden or 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
75 changes: 75 additions & 0 deletions
75
posthog-android/src/main/java/com/posthog/android/replay/internal/ScreenshotScaling.kt
This file contains hidden or 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,75 @@ | ||
| package com.posthog.android.replay.internal | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.graphics.Canvas | ||
| import android.graphics.Paint | ||
| import android.graphics.Rect | ||
| import com.posthog.android.internal.webpBase64 | ||
| import kotlin.math.ceil | ||
|
|
||
| internal const val MIN_SCREENSHOT_SCALE = 0.1f | ||
| internal const val MAX_SCREENSHOT_SCALE = 1f | ||
|
|
||
| internal fun resolveScreenshotScale( | ||
| configured: Float?, | ||
| density: Float, | ||
| ): Float { | ||
| val scale = configured ?: if (density.isFinite() && density > 0f) 1f / density else 1f | ||
| return scale.coerceIn(MIN_SCREENSHOT_SCALE, MAX_SCREENSHOT_SCALE) | ||
| } | ||
|
|
||
| internal fun scaledScreenshotDimension( | ||
| size: Int, | ||
| scale: Float, | ||
| ): Int { | ||
| // The tolerance keeps Float error, such as 350 * (1 / 3.5f) = 100.0000076, from adding a pixel. | ||
| return maxOf(1, ceil(size * scale - 0.001f).toInt()) | ||
| } | ||
|
|
||
| /** | ||
| * PixelCopy resamples with a single bilinear tap, which skips source pixels above 2:1. | ||
| * Returns the PixelCopy destination size when a second filtered downscale is needed, else null. | ||
| */ | ||
| internal fun twoStageCopySize( | ||
| sourceWidth: Int, | ||
| sourceHeight: Int, | ||
| targetWidth: Int, | ||
| targetHeight: Int, | ||
| ): Pair<Int, Int>? { | ||
| if (sourceWidth <= 2 * targetWidth && sourceHeight <= 2 * targetHeight) { | ||
| return null | ||
| } | ||
| return minOf(sourceWidth, 2 * targetWidth) to minOf(sourceHeight, 2 * targetHeight) | ||
| } | ||
|
|
||
| internal fun Bitmap.downscaleFiltered( | ||
| targetWidth: Int, | ||
| targetHeight: Int, | ||
| ): Bitmap { | ||
| val target = Bitmap.createBitmap(targetWidth, targetHeight, config ?: Bitmap.Config.ARGB_8888) | ||
| try { | ||
| Canvas(target).drawBitmap( | ||
| this, | ||
| Rect(0, 0, width, height), | ||
| Rect(0, 0, targetWidth, targetHeight), | ||
| Paint(Paint.FILTER_BITMAP_FLAG), | ||
| ) | ||
| return target | ||
| } catch (e: Throwable) { | ||
| target.recycle() | ||
| throw e | ||
| } | ||
| } | ||
|
|
||
| internal fun Bitmap.downscaledWebpBase64( | ||
| targetWidth: Int, | ||
| targetHeight: Int, | ||
| quality: Int, | ||
| ): String? { | ||
| val downscaled = downscaleFiltered(targetWidth, targetHeight) | ||
| try { | ||
| return downscaled.webpBase64(quality) | ||
| } finally { | ||
| downscaled.recycle() | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This allocates a new target bitmap every frame (about 1.4 MiB at 402x874 ARGB). #756 pooled the PixelCopy buffer to avoid per-frame allocations, so I think it might be worth pooling the 1x target too. Fine as a follow-up.
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.
Yeah, left it unpooled on purpose. Pooling it means a second slot in
PixelCopyBitmapBuffer's lease logic, and I'd rather not widen that in this PR. It only allocates on the two-stage path (copy ratio above 2:1), at most once per throttle tick, so I'll pick it up as a follow-up.