Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,7 @@ internal class FrameTimingsObserver(
Window.OnFrameMetricsAvailableListener { _, frameMetrics, _dropCount ->
val beginTimestamp = frameMetrics.getMetric(FrameMetrics.VSYNC_TIMESTAMP)
val endTimestamp = beginTimestamp + frameMetrics.getMetric(FrameMetrics.TOTAL_DURATION)

val frameId = frameCounter++
val threadId = Process.myTid()

CoroutineScope(Dispatchers.Default).launch {
val screenshot = if (screenshotsEnabled) captureScreenshot() else null

onFrameTimingSequence(
FrameTimingSequence(
frameId,
threadId,
beginTimestamp,
endTimestamp,
screenshot,
)
)
}
emitFrameTiming(beginTimestamp, endTimestamp)
}

private suspend fun captureScreenshot(): String? = suspendCoroutine { continuation ->
Expand Down Expand Up @@ -123,9 +107,33 @@ internal class FrameTimingsObserver(
return
}

// Capture initial screenshot to ensure there's always at least one frame
// recorded at the start of tracing, even if no UI changes occur
val timestamp = System.nanoTime()
emitFrameTiming(timestamp, timestamp)

window.addOnFrameMetricsAvailableListener(frameMetricsListener, handler)
}

private fun emitFrameTiming(beginTimestamp: Long, endTimestamp: Long) {
val frameId = frameCounter++
val threadId = Process.myTid()

CoroutineScope(Dispatchers.Default).launch {
val screenshot = if (screenshotsEnabled) captureScreenshot() else null

onFrameTimingSequence(
FrameTimingSequence(
frameId,
threadId,
beginTimestamp,
endTimestamp,
screenshot,
)
)
}
}

fun stop() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return
Expand Down
Loading