Skip to content

Commit 2b119fc

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

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,12 @@ internal class FrameTimingsObserver(
120120

121121
// Reuse bitmap if dimensions haven't changed
122122
val bitmap =
123-
bitmapBuffer?.let {
124-
if (it.width == width && it.height == height) {
125-
it
126-
} else {
127-
it.recycle()
128-
null
129-
}
130-
}
131-
?: Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
132-
bitmapBuffer = it
123+
bitmapBuffer?.takeIf { it.width == width && it.height == height }
124+
?: run {
125+
bitmapBuffer?.recycle()
126+
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
127+
bitmapBuffer = it
128+
}
133129
}
134130

135131
// Suspend for PixelCopy callback
@@ -150,17 +146,17 @@ internal class FrameTimingsObserver(
150146
withContext(Dispatchers.Default) {
151147
var scaledBitmap: Bitmap? = null
152148
try {
153-
val scaleFactor = 0.25f
154-
val scaledWidth = (width * scaleFactor).toInt()
155-
val scaledHeight = (height * scaleFactor).toInt()
149+
val density = window.context.resources.displayMetrics.density
150+
val scaledWidth = (width / density * SCREENSHOT_SCALE_FACTOR).toInt()
151+
val scaledHeight = (height / density * SCREENSHOT_SCALE_FACTOR).toInt()
156152
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
157153

158154
val compressFormat =
159155
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) Bitmap.CompressFormat.WEBP_LOSSY
160-
else Bitmap.CompressFormat.WEBP
156+
else Bitmap.CompressFormat.JPEG
161157

162158
ByteArrayOutputStream().use { outputStream ->
163-
scaledBitmap.compress(compressFormat, 0, outputStream)
159+
scaledBitmap.compress(compressFormat, SCREENSHOT_QUALITY, outputStream)
164160
Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
165161
}
166162
} catch (e: Exception) {
@@ -170,4 +166,9 @@ internal class FrameTimingsObserver(
170166
}
171167
}
172168
}
169+
170+
companion object {
171+
private const val SCREENSHOT_SCALE_FACTOR = 0.75f
172+
private const val SCREENSHOT_QUALITY = 80
173+
}
173174
}

0 commit comments

Comments
 (0)