JavaFx animation poor performance, consumes all my CPU

前端 未结 1 1080
清酒与你
清酒与你 2020-12-04 22:56

I have written the following demonstration program. Four HBox(s) containing one Text node each, are added to the root (Group). The first and last are animated with a timeli

相关标签:
1条回答
  • 2020-12-04 23:51
    1. Cache the node (doing this with your sample dropped the CPU usage from 100% of a core to 1%).

      setCache(true);
      setCacheShape(true);
      setCacheHint(CacheHint.SPEED);
      
    2. Read the javadoc on node caching.

    3. Define your CSS rules in a stylesheet and not inline styles (if I recall that is more efficient due to the nature of the CSS implementation in JavaFX).

    4. Take a look at performance tips and tricks on the open-jfx wiki.

    5. Read related questions:

      • JavaFX 2 drawing performance
      • What is the best way to display millions of images in Java?
      • How do setCache() and CacheHint work together in JavaFX?

    I expected the computations to be done in GPU instead.

    JavaFX will do lots of computations on the GPU, however it does a tradeoff - some computations are better done on a CPU and JavaFX will use the CPU for these kinds of computations. Your issue is not one of having computations done on the GPU or CPU. Your issue is having too many computations done because appropriate caching hints have not been provided to the JavaFX system.

    Is it considered a bad practice to use css to decorate nodes that are going to be animated?

    Inline css styles are bad practice in general - put the css in a stylesheet. Using CSS on animated nodes is fine for most use cases.

    Indeed setting setCache(true) and setCacheHint(CacheHint.SPEED) improved the animation but it is still laggy.

    The animation does not lag on my machine, but then it is not really comparable because the hardware and software in my machine is from 2014, not 2008.

    Perhaps try updating your graphics drivers and updating JavaFX to the latest development build.

    Also, slow down the animation (e.g. give it a duration of five seconds rather than half a second). It can be hard to visually detect the smoothness of an animation when it moves very quickly.

    Log a bug report in the JavaFX issue tracker - the JavaFX developers should be able to supply additional information on how to do turn on detailed JavaFX performance tracking which logs the rendering steps in the JavaFX pipeline and measures their performance on a frame by frame basis (I don't know how to do this).

    0 讨论(0)
提交回复
热议问题