Commit b6624360 authored by Igor Demin's avatar Igor Demin

FPSCounter

parent 5c82039a
......@@ -53,3 +53,7 @@ dependencies {
application {
mainClassName = 'SkijaInjectSample.AppKt'
}
run {
systemProperty("skiko.fps.enabled", "true")
}
\ No newline at end of file
package org.jetbrains.skiko
import java.util.*
import kotlin.math.roundToInt
internal class FPSCounter(
private val count: Int,
private val probability: Double
) {
private var i = 0
private val times = LinkedList<Double>()
private var t1 = System.nanoTime()
/**
* [value] 0.0 - min, 1.0 - max, 0.5 - median
*/
private fun MutableList<Double>.quantile(value: Double) : Double {
sort()
val index = (value * (size - 1)).toInt()
return this[index]
}
fun tick() {
val t2 = System.nanoTime()
val frameTime = (t2 - t1) / 1E6
t1 = t2
i++
times.add(frameTime)
if (times.size > count) {
times.removeFirst()
}
if (i % count == 0) {
val quantile = (1 - probability) / 2.0
val average = (1000.0 / times.average()).roundToInt()
val min = (1000.0 / times.quantile(1 - quantile)).roundToInt()
val max = (1000.0 / times.quantile(quantile)).roundToInt()
val probability = (100 * probability).roundToInt()
println("FPS $average ($min-$max $probability%)")
}
}
}
\ No newline at end of file
......@@ -50,7 +50,16 @@ open class SkiaLayer() : HardwareLayer() {
renderer?.onDispose()
}
private val fpsCounter = FPSCounter(
count = System.getProperty("skiko.fps.count")?.toInt() ?: 500,
probability = System.getProperty("skiko.fps.probability")?.toDouble() ?: 0.97
)
override fun draw() {
if (System.getProperty("skiko.fps.enabled") == "true") {
fpsCounter.tick()
}
if (!inited) {
if (skijaState.context == null) {
skijaState.context = when (api) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment