-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented FrameRateProvider (W.I.P)
- Loading branch information
1 parent
8b325de
commit c3e62cb
Showing
7 changed files
with
255 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
packages/core/android/src/main/kotlin/com/datadog/reactnative/FrameRateProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.datadog.reactnative | ||
|
||
import com.facebook.infer.annotation.Assertions | ||
import com.facebook.react.bridge.ReactContext | ||
import com.facebook.react.bridge.UiThreadUtil | ||
import com.facebook.react.modules.core.ChoreographerCompat | ||
import com.facebook.react.modules.debug.DidJSUpdateUiDuringFrameDetector | ||
import com.facebook.react.modules.debug.FpsDebugFrameCallback | ||
import com.facebook.react.uimanager.UIManagerModule | ||
import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugListener | ||
import java.lang.ClassCastException | ||
import java.util.TreeMap | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.ScheduledExecutorService | ||
|
||
internal class FrameRateProvider( | ||
reactFrameRateCallback: ((Double) -> Unit)?, | ||
reactContext: ReactContext | ||
) { | ||
private val frameCallback: FpsFrameCallback = | ||
FpsFrameCallback(reactContext) | ||
|
||
private var backgroundExecutor: ScheduledExecutorService? = null | ||
|
||
private val monitor = Monitor(frameCallback, reactFrameRateCallback) | ||
|
||
fun start() { | ||
// Create an executor that executes tasks in a background thread. | ||
val executor = Executors.newSingleThreadScheduledExecutor() | ||
backgroundExecutor = executor | ||
frameCallback.reset() | ||
frameCallback.start() | ||
monitor.start(executor) | ||
} | ||
|
||
fun stop() { | ||
backgroundExecutor?.shutdown() | ||
backgroundExecutor = null | ||
frameCallback.stop() | ||
monitor.stop() | ||
} | ||
|
||
internal class Monitor( | ||
private val frameCallback: FpsFrameCallback, | ||
private val reactFrameRateCallback: ((Double) -> Unit)? | ||
): Runnable { | ||
|
||
private var mShouldStop = false | ||
|
||
override fun run() { | ||
if (mShouldStop) { | ||
return | ||
} | ||
|
||
// Send JS FPS info | ||
reactFrameRateCallback?.let { it(frameCallback.jSFPS) } | ||
} | ||
|
||
fun start(backgroundExecutor: ScheduledExecutorService) { | ||
mShouldStop = false | ||
backgroundExecutor.execute(this) | ||
} | ||
|
||
fun stop() { | ||
mShouldStop = true | ||
} | ||
} | ||
} | ||
|
||
internal class FpsFrameCallback(private val mReactContext: ReactContext) : | ||
ChoreographerCompat.FrameCallback() { | ||
private var mChoreographer: ChoreographerCompat? = null | ||
|
||
private val mUIManagerModule: UIManagerModule? get() { | ||
var uiManagerModule: UIManagerModule? = null | ||
try { | ||
uiManagerModule = mReactContext.getNativeModule( | ||
UIManagerModule::class.java | ||
) | ||
} catch (_: ClassCastException) {} | ||
return uiManagerModule | ||
} | ||
|
||
private val mDidJSUpdateUiDuringFrameDetector: DidJSUpdateUiDuringFrameDetector = | ||
DidJSUpdateUiDuringFrameDetector() | ||
private var mFirstFrameTime = -1L | ||
private var mLastFrameTime = -1L | ||
private var mNumFrameCallbacks = 0 | ||
private var mExpectedNumFramesPrev = 0 | ||
private var m4PlusFrameStutters = 0 | ||
private var mNumFrameCallbacksWithBatchDispatches = 0 | ||
private var mIsRecordingFpsInfoAtEachFrame = false | ||
private var mTimeToFps: TreeMap<Long?, FpsDebugFrameCallback.FpsInfo?>? = null | ||
|
||
val fPS: Double | ||
get() = if (mLastFrameTime == mFirstFrameTime) 0.0 else numFrames.toDouble() * 1.0E9 / (mLastFrameTime - mFirstFrameTime).toDouble() | ||
val jSFPS: Double | ||
get() = if (mLastFrameTime == mFirstFrameTime) 0.0 else numJSFrames.toDouble() * 1.0E9 / (mLastFrameTime - mFirstFrameTime).toDouble() | ||
val numFrames: Int | ||
get() = mNumFrameCallbacks - 1 | ||
val numJSFrames: Int | ||
get() = mNumFrameCallbacksWithBatchDispatches - 1 | ||
val expectedNumFrames: Int | ||
get() { | ||
val totalTimeMS = totalTimeMS.toDouble() | ||
return (totalTimeMS / 16.9 + 1.0).toInt() | ||
} | ||
val totalTimeMS: Int | ||
get() = (mLastFrameTime.toDouble() - mFirstFrameTime.toDouble()).toInt() / 1000000 | ||
|
||
|
||
override fun doFrame(l: Long) { | ||
if (mFirstFrameTime == -1L) { | ||
mFirstFrameTime = l | ||
} | ||
val lastFrameStartTime = mLastFrameTime | ||
mLastFrameTime = l | ||
if (mDidJSUpdateUiDuringFrameDetector.getDidJSHitFrameAndCleanup(lastFrameStartTime, l)) { | ||
++mNumFrameCallbacksWithBatchDispatches | ||
} | ||
++mNumFrameCallbacks | ||
val expectedNumFrames = expectedNumFrames | ||
val framesDropped = expectedNumFrames - mExpectedNumFramesPrev - 1 | ||
if (framesDropped >= 4) { | ||
++m4PlusFrameStutters | ||
} | ||
|
||
mExpectedNumFramesPrev = expectedNumFrames | ||
if (mChoreographer != null) { | ||
mChoreographer!!.postFrameCallback(this) | ||
} | ||
} | ||
|
||
fun start() { | ||
mReactContext.catalystInstance.addBridgeIdleDebugListener(mDidJSUpdateUiDuringFrameDetector) | ||
mUIManagerModule?.setViewHierarchyUpdateDebugListener(mDidJSUpdateUiDuringFrameDetector) | ||
UiThreadExecutor.Provider.uiThreadExecutor.runOnUiThread { | ||
mChoreographer = ChoreographerCompat.getInstance() | ||
mChoreographer?.postFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun stop() { | ||
mReactContext.catalystInstance.removeBridgeIdleDebugListener( | ||
mDidJSUpdateUiDuringFrameDetector | ||
) | ||
mUIManagerModule?.setViewHierarchyUpdateDebugListener(null as NotThreadSafeViewHierarchyUpdateDebugListener?) | ||
UiThreadExecutor.Provider.uiThreadExecutor.runOnUiThread { | ||
mChoreographer = ChoreographerCompat.getInstance() | ||
mChoreographer?.removeFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun reset() { | ||
mFirstFrameTime = -1L | ||
mLastFrameTime = -1L | ||
mNumFrameCallbacks = 0 | ||
m4PlusFrameStutters = 0 | ||
mNumFrameCallbacksWithBatchDispatches = 0 | ||
mIsRecordingFpsInfoAtEachFrame = false | ||
mTimeToFps = null | ||
} | ||
} | ||
|
||
|
||
|
34 changes: 34 additions & 0 deletions
34
packages/core/android/src/main/kotlin/com/datadog/reactnative/UiThreadExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.datadog.reactnative | ||
|
||
import com.facebook.react.bridge.UiThreadUtil | ||
|
||
interface UiThreadExecutor { | ||
fun runOnUiThread(runnable: Runnable) | ||
|
||
object Provider { | ||
val uiThreadExecutor: UiThreadExecutor by lazy { | ||
if (isRunningInTest()) { | ||
TestUiThreadExecutor() | ||
} else { | ||
RealUiThreadExecutor() | ||
} | ||
} | ||
|
||
private fun isRunningInTest(): Boolean { | ||
return System.getProperty("IS_UNIT_TEST") == "true" | ||
} | ||
} | ||
} | ||
|
||
class RealUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
UiThreadUtil.runOnUiThread(runnable) | ||
} | ||
} | ||
|
||
class TestUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
// Run immediately in the same thread for tests | ||
runnable.run() | ||
} | ||
} |
Oops, something went wrong.