Reduce the screenshots size (#54800)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/54800

# Changelog: [Internal]

From my observations, currently every base64 string for encoded screenshot takes on average ~100Kb. This is too much. These changes reduce the size to ~6Kb, at a cost of quality.

We can't accomodate such big strings, because there could be hundreds, if not thousands of frames for a ~10-20s trace.

Reviewed By: huntie

Differential Revision: D88489936

fbshipit-source-id: c9df916d80ea013391b1f353c9815196a6cde8dd
This commit is contained in:
Ruslan Lesiutin authored and meta-codesync[bot] committed 2025-12-06 02:23:08 -08:00
1 parent 6be156e7f9
commit e4a5a56dfb
1 file changed
+9 -5
@@ -92,17 +92,21 @@ internal class FrameTimingsObserver(
if (copyResult == PixelCopy.SUCCESS) {
CoroutineScope(Dispatchers.Default).launch {
try {
val scaleFactor = 0.5f
val scaleFactor = 0.25f
val scaledWidth = (width * scaleFactor).toInt()
val scaledHeight = (height * scaleFactor).toInt()
val scaledBitmap =
Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
val outputStream = ByteArrayOutputStream()
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream)
val jpegBytes = outputStream.toByteArray()
val jpegBase64 = Base64.encodeToString(jpegBytes, Base64.NO_WRAP)
continuation.resume(jpegBase64)
val compressFormat =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
Bitmap.CompressFormat.WEBP_LOSSY
else Bitmap.CompressFormat.WEBP
scaledBitmap.compress(compressFormat, 0, outputStream)
val bytes = outputStream.toByteArray()
val base64 = Base64.encodeToString(bytes, Base64.NO_WRAP)
continuation.resume(base64)
scaledBitmap.recycle()
} catch (e: Exception) {