Skip to content

Commit caf9f67

Browse files
huntiefacebook-github-bot
authored andcommitted
Increase frame capture quality, apply scaling after DPI normalization (#55731)
Summary: Changelog: [Internal] Differential Revision: D94256691
1 parent b2b4aa7 commit caf9f67

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/inspector/FrameTimingsObserver.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,11 @@ internal class FrameTimingsObserver(
124124

125125
// Reuse bitmap if dimensions haven't changed
126126
val bitmap =
127-
bitmapBuffer?.let {
128-
if (it.width == width && it.height == height) {
129-
it
130-
} else {
131-
it.recycle()
132-
null
133-
}
134-
} ?: Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also { bitmapBuffer = it }
127+
bitmapBuffer?.takeIf { it.width == width && it.height == height }
128+
?: run {
129+
bitmapBuffer?.recycle()
130+
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also { bitmapBuffer = it }
131+
}
135132

136133
PixelCopy.request(
137134
window,
@@ -141,19 +138,19 @@ internal class FrameTimingsObserver(
141138
CoroutineScope(Dispatchers.Default).launch {
142139
var scaledBitmap: Bitmap? = null
143140
try {
144-
val scaleFactor = 0.25f
145-
val scaledWidth = (width * scaleFactor).toInt()
146-
val scaledHeight = (height * scaleFactor).toInt()
141+
val density = window.context.resources.displayMetrics.density
142+
val scaledWidth = (width / density * SCREENSHOT_SCALE_FACTOR).toInt()
143+
val scaledHeight = (height / density * SCREENSHOT_SCALE_FACTOR).toInt()
147144
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
148145

149146
val compressFormat =
150147
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
151148
Bitmap.CompressFormat.WEBP_LOSSY
152-
else Bitmap.CompressFormat.WEBP
149+
else Bitmap.CompressFormat.JPEG
153150

154151
val base64 =
155152
ByteArrayOutputStream().use { outputStream ->
156-
scaledBitmap.compress(compressFormat, 0, outputStream)
153+
scaledBitmap.compress(compressFormat, SCREENSHOT_QUALITY, outputStream)
157154
Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
158155
}
159156

@@ -171,4 +168,9 @@ internal class FrameTimingsObserver(
171168
handler,
172169
)
173170
}
171+
172+
companion object {
173+
private const val SCREENSHOT_SCALE_FACTOR = 0.75f
174+
private const val SCREENSHOT_QUALITY = 80
175+
}
174176
}

0 commit comments

Comments
 (0)