Skip to content

Commit

Permalink
Convert FpsDebugFrameCallback/AnimationsDebugModule to Kotlin (#44475)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44475

# Changelog:
[Internal] -

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

Differential Revision: D57095896
  • Loading branch information
rshest authored and facebook-github-bot committed May 8, 2024
1 parent 1e25baf commit d6aa92d
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 353 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 @@ -3189,33 +3189,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,90 @@
/*
* 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.modules.debug

import android.widget.Toast
import com.facebook.common.logging.FLog
import com.facebook.fbreact.specs.NativeAnimationsDebugModuleSpec
import com.facebook.react.bridge.JSApplicationCausedNativeException
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.common.ReactConstants
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.modules.debug.interfaces.DeveloperSettings
import java.util.Locale

/**
* Module that records debug information during transitions (animated navigation events such as
* going from one screen to another).
*/
@ReactModule(name = NativeAnimationsDebugModuleSpec.NAME)
internal class AnimationsDebugModule(
reactContext: ReactApplicationContext?,
private val catalystSettings: DeveloperSettings?
) : NativeAnimationsDebugModuleSpec(reactContext) {
private var frameCallback: FpsDebugFrameCallback? = null

override fun startRecordingFps() {
if (catalystSettings == null || !catalystSettings.isAnimationFpsDebugEnabled) {
return
}
if (frameCallback != null) {
throw JSApplicationCausedNativeException("Already recording FPS!")
}
frameCallback = FpsDebugFrameCallback(getReactApplicationContext())
frameCallback?.startAndRecordFpsAtEachFrame()
}

/**
* Called when an animation finishes. The caller should include the animation stop time in ms
* (unix time) so that we know when the animation stopped from the JS perspective and we don't
* count time after as being part of the animation.
*/
override fun stopRecordingFps(animationStopTimeMs: Double) {
if (frameCallback == null) {
return
}
frameCallback!!.stop()

// Casting to long is safe here since animationStopTimeMs is unix time and thus relatively small
val fpsInfo = frameCallback!!.getFpsInfo(animationStopTimeMs.toLong())
if (fpsInfo == null) {
Toast.makeText(getReactApplicationContext(), "Unable to get FPS info", Toast.LENGTH_LONG)
.show()
} else {
val fpsString =
String.format(
Locale.US,
"FPS: %.2f, %d frames (%d expected)",
fpsInfo.fps,
fpsInfo.totalFrames,
fpsInfo.totalExpectedFrames)
val jsFpsString =
String.format(
Locale.US,
"JS FPS: %.2f, %d frames (%d expected)",
fpsInfo.jsFps,
fpsInfo.totalJsFrames,
fpsInfo.totalExpectedFrames)
val debugString =
"""
$fpsString
$jsFpsString
Total Time MS: ${String.format(Locale.US, "%d", fpsInfo.totalTimeMs)}
"""
.trimIndent()
FLog.d(ReactConstants.TAG, debugString)
Toast.makeText(getReactApplicationContext(), debugString, Toast.LENGTH_LONG).show()
}
frameCallback = null
}

override fun invalidate() {
frameCallback?.stop()
frameCallback = null
}
}
Loading

0 comments on commit d6aa92d

Please sign in to comment.