-
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.
- Loading branch information
1 parent
8b325de
commit be526a0
Showing
5 changed files
with
162 additions
and
36 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
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
package com.datadog.reactnative | ||
|
||
import com.facebook.react.modules.core.ChoreographerCompat | ||
|
||
internal class FrameRateProvider( | ||
reactFrameRateCallback: ((Double) -> Unit)? | ||
) { | ||
private val frameCallback: FpsFrameCallback = FpsFrameCallback(reactFrameRateCallback) | ||
|
||
fun start() { | ||
frameCallback.reset() | ||
frameCallback.start() | ||
} | ||
|
||
fun stop() { | ||
frameCallback.stop() | ||
} | ||
} | ||
|
||
internal class FpsFrameCallback( | ||
private val reactFrameRateCallback: ((Double) -> Unit)? | ||
) : | ||
ChoreographerCompat.FrameCallback() { | ||
private var mChoreographer: ChoreographerCompat? = null | ||
|
||
private var mLastFrameTime = -1L | ||
|
||
override fun doFrame(time: Long) { | ||
if (mLastFrameTime != -1L) { | ||
reactFrameRateCallback?.let { it((time - mLastFrameTime).toDouble()) } | ||
} | ||
mLastFrameTime = time | ||
mChoreographer?.postFrameCallback(this) | ||
} | ||
|
||
fun start() { | ||
UiThreadExecutor.Provider.uiThreadExecutor.runOnUiThread { | ||
mChoreographer = ChoreographerCompat.getInstance() | ||
mChoreographer?.postFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun stop() { | ||
UiThreadExecutor.Provider.uiThreadExecutor.runOnUiThread { | ||
mChoreographer = ChoreographerCompat.getInstance() | ||
mChoreographer?.removeFrameCallback(this@FpsFrameCallback) | ||
} | ||
} | ||
|
||
fun reset() { | ||
mLastFrameTime = -1L | ||
} | ||
} |
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 | ||
|
||
internal 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" | ||
} | ||
} | ||
} | ||
|
||
internal class RealUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
UiThreadUtil.runOnUiThread(runnable) | ||
} | ||
} | ||
|
||
internal class TestUiThreadExecutor : UiThreadExecutor { | ||
override fun runOnUiThread(runnable: Runnable) { | ||
// Run immediately in the same thread for tests | ||
runnable.run() | ||
} | ||
} |
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