Skip to content

Commit bdf9ebc

Browse files
huntiefacebook-github-bot
authored andcommitted
Increase frame capture quality, apply scaling after DPI normalization
Summary: Changelog: [Internal] Differential Revision: D94256691
1 parent d263ce6 commit bdf9ebc

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

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

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

119119
// Reuse bitmap if dimensions haven't changed
120120
val bitmap =
121-
bitmapBuffer?.let {
122-
if (it.width == width && it.height == height) {
123-
it
124-
} else {
125-
it.recycle()
126-
null
127-
}
128-
}
129-
?: Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
130-
bitmapBuffer = it
121+
bitmapBuffer?.takeIf { it.width == width && it.height == height }
122+
?: run {
123+
bitmapBuffer?.recycle()
124+
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).also {
125+
bitmapBuffer = it
126+
}
131127
}
132128

133129
// Suspend for PixelCopy callback
@@ -148,17 +144,17 @@ internal class FrameTimingsObserver(
148144
withContext(Dispatchers.Default) {
149145
var scaledBitmap: Bitmap? = null
150146
try {
151-
val scaleFactor = 0.25f
152-
val scaledWidth = (width * scaleFactor).toInt()
153-
val scaledHeight = (height * scaleFactor).toInt()
147+
val density = window.context.resources.displayMetrics.density
148+
val scaledWidth = (width / density * SCREENSHOT_SCALE_FACTOR).toInt()
149+
val scaledHeight = (height / density * SCREENSHOT_SCALE_FACTOR).toInt()
154150
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
155151

156152
val compressFormat =
157153
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) Bitmap.CompressFormat.WEBP_LOSSY
158154
else Bitmap.CompressFormat.WEBP
159155

160156
ByteArrayOutputStream().use { outputStream ->
161-
scaledBitmap.compress(compressFormat, 0, outputStream)
157+
scaledBitmap.compress(compressFormat, SCREENSHOT_QUALITY, outputStream)
162158
Base64.encodeToString(outputStream.toByteArray(), Base64.NO_WRAP)
163159
}
164160
} catch (e: Exception) {
@@ -168,4 +164,9 @@ internal class FrameTimingsObserver(
168164
}
169165
}
170166
}
167+
168+
companion object {
169+
private const val SCREENSHOT_SCALE_FACTOR = 0.75f
170+
private const val SCREENSHOT_QUALITY = 80
171+
}
171172
}

0 commit comments

Comments
 (0)