Closed YeungKC closed 1 month ago
Found the reason, investigating how to fix it.
If we don't use application
/singleWindowApplication
, just Swing's ComposeWindow
:
import androidx.compose.animation.core.*
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.window.singleWindowApplication
import java.awt.Dimension
import javax.swing.SwingUtilities
fun main() = SwingUtilities.invokeLater {
ComposeWindow().apply {
size = Dimension(500, 500)
setContent {
val transition = rememberInfiniteTransition()
val frameIndex by transition.animateValue(
initialValue = 0,
targetValue = 63,
Int.VectorConverter,
animationSpec = infiniteRepeatable(
animation = keyframes {
durationMillis = 0
for (index in 0 until 64) {
index at durationMillis
durationMillis += 100
}
}
)
)
Text("frameIndex: $frameIndex")
}
isVisible = true
}
}
then CPU usage is 1% instead of 10% (on Windows)
Found the reason, investigating how to fix it.
If we don't use
application
/singleWindowApplication
, just Swing'sComposeWindow
:import androidx.compose.animation.core.* import androidx.compose.material.Text import androidx.compose.runtime.getValue import androidx.compose.ui.awt.ComposeWindow import androidx.compose.ui.window.singleWindowApplication import java.awt.Dimension import javax.swing.SwingUtilities fun main() = SwingUtilities.invokeLater { ComposeWindow().apply { size = Dimension(500, 500) setContent { val transition = rememberInfiniteTransition() val frameIndex by transition.animateValue( initialValue = 0, targetValue = 63, Int.VectorConverter, animationSpec = infiniteRepeatable( animation = keyframes { durationMillis = 0 for (index in 0 until 64) { index at durationMillis durationMillis += 100 } } ) ) Text("frameIndex: $frameIndex") } isVisible = true } }
then CPU usage is 1% instead of 10% (on Windows)
This also work for macOS, then CPU usage is around 9%.
The bug with singleWindowApplication
is fixed in v1.0.0-alpha4-build396.
I leave this issue open, as there are more ways to improve CPU/GPU usage.
For example, when we call animateValue
or withFrameNanos
, we constantly redraw the whole window content with 1 frame per 16ms, even if the animation should be updated as 1 frame per 100ms (or per 500ms as TextField cursor).
To fix that, we should add a new feature in skiko, which will allow to keep the buffer intact if there is no changes.
I'm not sure if is is related to animation, but CPU usage is about 100% when TextField
has focus and cursor is blinking in it.
I'm not sure if is is related to animation, but CPU usage is about 100% when TextField has focus and cursor is blinking in it.
Blinking of the cursor is an animation.
Is CPU usage low in 1.0.0-alpha4-build396?
Is CPU usage low in 1.0.0-alpha4-build396?
Yes. Now CPU usage is about 10%
10% is still high for just blinking of the cursor, and I believe we can reduce it, when we optimize as described here. It probably can't be fixed in the user code.
Wow! When will this improvement be adapted to Toolbox App?
It is better to ask Toolbox team. I think their next public release will have this issue fixed.
Wow! When will this improvement be adapted to Toolbox App?
Toolbox doesn't use application
/singleWindowApplication
, it uses ComposeWindow directly. So the issue with the high CPU usage in Toolbox probably is another issue. Don't know if it will be fixed in the next build.
In Compose/Skiko there were two more fixes regarding CPU usage:
Thanks!!
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.
example 0:
example 1: