Skip to content

Commit 47684ca

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Fix trailing frame capture after recording ended (#55704)
Summary: Pull Request resolved: #55704 Previously, this could lead to capturing an additional 120-240 new frames. Changelog: [Internal] Reviewed By: rubennorte Differential Revision: D93863310 fbshipit-source-id: 4c85c58274f3173cefe283b9ccb5887990309087
1 parent 3924768 commit 47684ca

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ internal class FrameTimingsObserver(
2828
private val onFrameTimingSequence: (sequence: FrameTimingSequence) -> Unit,
2929
) {
3030
private val isSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
31-
3231
private val mainHandler = Handler(Looper.getMainLooper())
33-
private var frameCounter: Int = 0
34-
private var isStarted: Boolean = false
3532

33+
private var frameCounter: Int = 0
34+
@Volatile private var isTracing: Boolean = false
3635
@Volatile private var currentWindow: Window? = null
3736

3837
fun start() {
@@ -41,7 +40,7 @@ internal class FrameTimingsObserver(
4140
}
4241

4342
frameCounter = 0
44-
isStarted = true
43+
isTracing = true
4544

4645
// Capture initial screenshot to ensure there's always at least one frame
4746
// recorded at the start of tracing, even if no UI changes occur
@@ -56,7 +55,7 @@ internal class FrameTimingsObserver(
5655
return
5756
}
5857

59-
isStarted = false
58+
isTracing = false
6059

6160
currentWindow?.removeOnFrameMetricsAvailableListener(frameMetricsListener)
6261
mainHandler.removeCallbacksAndMessages(null)
@@ -69,13 +68,18 @@ internal class FrameTimingsObserver(
6968

7069
currentWindow?.removeOnFrameMetricsAvailableListener(frameMetricsListener)
7170
currentWindow = window
72-
if (isStarted) {
71+
if (isTracing) {
7372
currentWindow?.addOnFrameMetricsAvailableListener(frameMetricsListener, mainHandler)
7473
}
7574
}
7675

7776
private val frameMetricsListener =
7877
Window.OnFrameMetricsAvailableListener { _, frameMetrics, _ ->
78+
// Guard against calls arriving after stop() has ended tracing. Async work scheduled from
79+
// previous frames will still finish.
80+
if (!isTracing) {
81+
return@OnFrameMetricsAvailableListener
82+
}
7983
val beginTimestamp = frameMetrics.getMetric(FrameMetrics.VSYNC_TIMESTAMP)
8084
val endTimestamp = beginTimestamp + frameMetrics.getMetric(FrameMetrics.TOTAL_DURATION)
8185
emitFrameTiming(beginTimestamp, endTimestamp)

0 commit comments

Comments
 (0)