Skip to content

Commit

Permalink
Convert FpsDebugFrameCallback/AnimationsDebugModule to Kotlin (facebo…
Browse files Browse the repository at this point in the history
…ok#44475)

Summary:
Pull Request resolved: facebook#44475

# Changelog:
[Internal] -

As the title says, this is preparing to complete the conversion of the whole corresponding module to Kotlin.

Reviewed By: christophpurrer

Differential Revision: D57095896

fbshipit-source-id: 87fe08aec974f5f1327189d0e74c3782c4391e85
  • Loading branch information
rshest authored and kosmydel committed Jun 11, 2024
1 parent fc1fdc0 commit 2ebc39c
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 457 deletions.
45 changes: 23 additions & 22 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3193,33 +3193,34 @@ public final class com/facebook/react/modules/debug/DevSettingsModule : com/face
public fun toggleElementInspector ()V
}

public class com/facebook/react/modules/debug/FpsDebugFrameCallback : android/view/Choreographer$FrameCallback {
public final class com/facebook/react/modules/debug/FpsDebugFrameCallback : android/view/Choreographer$FrameCallback {
public fun <init> (Lcom/facebook/react/bridge/ReactContext;)V
public fun doFrame (J)V
public fun get4PlusFrameStutters ()I
public fun getExpectedNumFrames ()I
public fun getFPS ()D
public fun getFpsInfo (J)Lcom/facebook/react/modules/debug/FpsDebugFrameCallback$FpsInfo;
public fun getJSFPS ()D
public fun getNumFrames ()I
public fun getNumJSFrames ()I
public fun getTotalTimeMS ()I
public fun reset ()V
public fun start ()V
public fun start (D)V
public fun startAndRecordFpsAtEachFrame ()V
public fun stop ()V
public final fun get4PlusFrameStutters ()I
public final fun getExpectedNumFrames ()I
public final fun getFps ()D
public final fun getFpsInfo (J)Lcom/facebook/react/modules/debug/FpsDebugFrameCallback$FpsInfo;
public final fun getJsFPS ()D
public final fun getNumFrames ()I
public final fun getNumJSFrames ()I
public final fun getTotalTimeMS ()I
public final fun reset ()V
public final fun start ()V
public final fun start (D)V
public static synthetic fun start$default (Lcom/facebook/react/modules/debug/FpsDebugFrameCallback;DILjava/lang/Object;)V
public final fun startAndRecordFpsAtEachFrame ()V
public final fun stop ()V
}

public class com/facebook/react/modules/debug/FpsDebugFrameCallback$FpsInfo {
public final field fps D
public final field jsFps D
public final field total4PlusFrameStutters I
public final field totalExpectedFrames I
public final field totalFrames I
public final field totalJsFrames I
public final field totalTimeMs I
public final class com/facebook/react/modules/debug/FpsDebugFrameCallback$FpsInfo {
public fun <init> (IIIIDDI)V
public final fun getFps ()D
public final fun getJsFps ()D
public final fun getTotal4PlusFrameStutters ()I
public final fun getTotalExpectedFrames ()I
public final fun getTotalFrames ()I
public final fun getTotalJsFrames ()I
public final fun getTotalTimeMs ()I
}

public class com/facebook/react/modules/debug/SourceCodeModule : com/facebook/fbreact/specs/NativeSourceCodeSpec {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.devsupport

import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.facebook.common.logging.FLog
import com.facebook.react.R
import com.facebook.react.bridge.ReactContext
import com.facebook.react.common.ReactConstants
import com.facebook.react.modules.debug.FpsDebugFrameCallback
import java.util.Locale

/**
* View that automatically monitors and displays the current app frame rate. Also logs the current
* FPS to logcat while active.
*
* NB: Requires API 16 for use of FpsDebugFrameCallback.
*/
internal class FpsView(reactContext: ReactContext?) : FrameLayout(reactContext!!) {
private val textView: TextView
private val frameCallback: FpsDebugFrameCallback
private val fpsMonitorRunnable: FPSMonitorRunnable

init {
inflate(reactContext, R.layout.fps_view, this)
textView = findViewById<View>(R.id.fps_text) as TextView
frameCallback = FpsDebugFrameCallback(reactContext!!)
fpsMonitorRunnable = FPSMonitorRunnable()
setCurrentFPS(0.0, 0.0, 0, 0)
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
frameCallback.reset()
frameCallback.start()
fpsMonitorRunnable.start()
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
frameCallback.stop()
fpsMonitorRunnable.stop()
}

private fun setCurrentFPS(
currentFPS: Double,
currentJSFPS: Double,
droppedUIFrames: Int,
total4PlusFrameStutters: Int
) {
val fpsString =
String.format(
Locale.US,
"UI: %.1f fps\n%d dropped so far\n%d stutters (4+) so far\nJS: %.1f fps",
currentFPS,
droppedUIFrames,
total4PlusFrameStutters,
currentJSFPS)
textView.text = fpsString
FLog.d(ReactConstants.TAG, fpsString)
}

/** Timer that runs every UPDATE_INTERVAL_MS ms and updates the currently displayed FPS. */
private inner class FPSMonitorRunnable : Runnable {
private var shouldStop = false
private var totalFramesDropped = 0
private var total4PlusFrameStutters = 0

override fun run() {
if (shouldStop) {
return
}
totalFramesDropped += frameCallback.expectedNumFrames - frameCallback.numFrames
total4PlusFrameStutters += frameCallback.get4PlusFrameStutters()
setCurrentFPS(
frameCallback.fps, frameCallback.jsFPS, totalFramesDropped, total4PlusFrameStutters)
frameCallback.reset()
postDelayed(this, UPDATE_INTERVAL_MS.toLong())
}

fun start() {
shouldStop = false
post(this)
}

fun stop() {
shouldStop = true
}
}

companion object {
private const val UPDATE_INTERVAL_MS = 500
}
}

This file was deleted.

Loading

0 comments on commit 2ebc39c

Please sign in to comment.