How do setCache() and CacheHint work together in JavaFX?

前端 未结 1 1621
南笙
南笙 2020-12-16 03:21

With regard to JavaFX, I have the following questions:

  1. Do I need to use setCache(true) on a Node for a cache hint set by setCacheHint() to actually have any

相关标签:
1条回答
  • 2020-12-16 03:46

    Do I need to use setCache(true) on a Node for a cache hint set by setCacheHint() to actually have any effect?

    Yes.

    The cache property is a hint to the system whether the node rendering should be cached (as an internal image) at all or not.

    The cacheHint property is a hint to the system of what transforms are expected on the node so that the caching operation can be optimized for those transform types, (e.g. the cache hints rotate, scale or speed, etc).

    If the node is not set to be cached at all, the cacheHint is irrelevant.

    Should calling setCache actually improve performance i.e. frame rate some or most of the time?

    Not necessarily. JavaFX has a default frame rate cap of 60fps, so if performance is good enough that the frame rate is reached even without any cache hints, you won't see any visible difference. This is the case for many basic animations and transforms.

    Even if frame rate is not improved, the cache hint may make each transform a bit more efficient to perform so that it is less CPU or GPU intensive (usually by trading visual quality).

    There may be other things which have a far greater impact on your frame rate. These things may have nothing to do with rendering speed of cacheable items (for example a long running operation executed on the JavaFX application thread during a game loop or constantly changing node content).


    I have used a combination of setCache(true) and setCacheHint(CacheHint.SPEED) in small games I have written that featured multiple simultaneously animated nodes with multiple effects and translucency applied to the nodes. The settings did speed things up a lot (Mac OS X, Macbook Air 2012, Java FX 2.2).


    Rather than relying on hints to the rendering system, you can also manually take a snapshot of a node tree and manually replace the node tree with the snapshot Image. This snapshot technique is not always the best way to go, but it does give you an alternative if the hints aren't working out well in your case.

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