|
| 1 | +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 2 | +package org.jetbrains.jewel.samples.standalone.components |
| 3 | + |
| 4 | +import androidx.compose.foundation.clickable |
| 5 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 6 | +import androidx.compose.foundation.layout.Arrangement |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.compose.runtime.LaunchedEffect |
| 10 | +import androidx.compose.runtime.derivedStateOf |
| 11 | +import androidx.compose.runtime.getValue |
| 12 | +import androidx.compose.runtime.mutableIntStateOf |
| 13 | +import androidx.compose.runtime.mutableStateOf |
| 14 | +import androidx.compose.runtime.remember |
| 15 | +import androidx.compose.runtime.setValue |
| 16 | +import androidx.compose.runtime.withFrameMillis |
| 17 | +import androidx.compose.ui.Modifier |
| 18 | +import androidx.compose.ui.graphics.Color |
| 19 | +import androidx.compose.ui.unit.dp |
| 20 | +import kotlin.math.roundToInt |
| 21 | +import kotlinx.coroutines.CoroutineDispatcher |
| 22 | +import kotlinx.coroutines.Dispatchers |
| 23 | +import kotlinx.coroutines.delay |
| 24 | +import kotlinx.coroutines.launch |
| 25 | +import org.jetbrains.jewel.ui.component.Text |
| 26 | + |
| 27 | +@Composable |
| 28 | +internal fun FpsCounter(modifier: Modifier = Modifier, dispatcher: CoroutineDispatcher = Dispatchers.Default) { |
| 29 | + var displayedFPS by remember { mutableIntStateOf(0) } |
| 30 | + var textContent by remember { mutableStateOf("FPS:") } |
| 31 | + var minMaxContent by remember { mutableStateOf("") } |
| 32 | + var fpsCountMethod by remember { mutableStateOf(FPSCountMethod.RealTime) } |
| 33 | + var minFps by remember { mutableIntStateOf(240) } |
| 34 | + var maxFps by remember { mutableIntStateOf(0) } |
| 35 | + |
| 36 | + val textColor by remember { |
| 37 | + derivedStateOf { |
| 38 | + when (fpsCountMethod) { |
| 39 | + FPSCountMethod.RealTime -> { |
| 40 | + textContent = "FPS(Realtime):$displayedFPS" |
| 41 | + } |
| 42 | + |
| 43 | + FPSCountMethod.FixedInterval -> { |
| 44 | + textContent = "FPS(last ${FPS_UPDATE_DELAY}ms):$displayedFPS" |
| 45 | + } |
| 46 | + |
| 47 | + FPSCountMethod.FixedFrameCount -> { |
| 48 | + textContent = "FPS(last ${FRAME_COUNT} frames):$displayedFPS" |
| 49 | + } |
| 50 | + } |
| 51 | + minMaxContent = "min:$minFps, max:$maxFps" |
| 52 | + displayedFPS.fpsCountColor |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + Row(modifier = modifier, horizontalArrangement = Arrangement.spacedBy(8.dp)) { |
| 57 | + Text( |
| 58 | + text = textContent, |
| 59 | + modifier = |
| 60 | + Modifier.clickable(interactionSource = remember { MutableInteractionSource() }, indication = null) { |
| 61 | + fpsCountMethod = |
| 62 | + when (fpsCountMethod) { |
| 63 | + FPSCountMethod.FixedInterval -> FPSCountMethod.FixedFrameCount |
| 64 | + FPSCountMethod.FixedFrameCount -> FPSCountMethod.RealTime |
| 65 | + FPSCountMethod.RealTime -> FPSCountMethod.FixedInterval |
| 66 | + } |
| 67 | + minFps = 240 |
| 68 | + maxFps = 0 |
| 69 | + }, |
| 70 | + color = textColor, |
| 71 | + ) |
| 72 | + |
| 73 | + Text( |
| 74 | + text = minMaxContent, |
| 75 | + modifier = |
| 76 | + Modifier.clickable(interactionSource = remember { MutableInteractionSource() }, indication = null) { |
| 77 | + minFps = 240 |
| 78 | + maxFps = 0 |
| 79 | + }, |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + LaunchedEffect(Unit) { |
| 84 | + val fpsArray = FloatArray(FRAME_COUNT) { 0f } |
| 85 | + var fpsCount = 0 |
| 86 | + var writeIndex = 0 |
| 87 | + var lastWriteIndex = 0 |
| 88 | + var lastUpdTime = System.currentTimeMillis() |
| 89 | + |
| 90 | + launch(dispatcher) { |
| 91 | + while (true) { |
| 92 | + delay(FPS_UPDATE_DELAY) |
| 93 | + |
| 94 | + displayedFPS = |
| 95 | + when (fpsCountMethod) { |
| 96 | + FPSCountMethod.FixedInterval -> fpsCount * 1000 / FPS_UPDATE_DELAY.toInt() |
| 97 | + FPSCountMethod.FixedFrameCount -> fpsArray.average().roundToInt() |
| 98 | + FPSCountMethod.RealTime -> fpsArray[lastWriteIndex].roundToInt() |
| 99 | + } |
| 100 | + if (displayedFPS > 0) { |
| 101 | + minFps = minOf(minFps, displayedFPS) |
| 102 | + } |
| 103 | + maxFps = maxOf(maxFps, displayedFPS) |
| 104 | + fpsCount = 0 |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + while (true) { |
| 109 | + withFrameMillis { frameTimeMillis -> |
| 110 | + fpsCount++ |
| 111 | + |
| 112 | + fpsArray[writeIndex] = 1000f / (frameTimeMillis - lastUpdTime) |
| 113 | + lastUpdTime = frameTimeMillis |
| 114 | + |
| 115 | + lastWriteIndex = writeIndex |
| 116 | + writeIndex = (writeIndex + 1) % fpsArray.size |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +private const val FPS_UPDATE_DELAY = 250L |
| 123 | +private const val FRAME_COUNT = 20 |
| 124 | + |
| 125 | +private val Int.fpsCountColor: Color |
| 126 | + get() = when { |
| 127 | + this > 55 -> Color.Green |
| 128 | + this > 45 -> Color.Yellow |
| 129 | + else -> Color.Red |
| 130 | + } |
| 131 | + |
| 132 | +internal enum class FPSCountMethod { |
| 133 | + FixedInterval, |
| 134 | + FixedFrameCount, |
| 135 | + RealTime, |
| 136 | +} |
0 commit comments