From e4a5a56dfb424b5c6794c94903484c8e8e84c6ec Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Sat, 6 Dec 2025 02:23:08 -0800 Subject: [PATCH] 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 --- .../devsupport/inspector/FrameTimingsObserver.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/FrameTimingsObserver.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/FrameTimingsObserver.kt index 4c10c3cc3c2..788356bd02e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/FrameTimingsObserver.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/FrameTimingsObserver.kt @@ -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) {