简体   繁体   中英

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

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 effect?

  2. Should calling setCache actually improve performance ie frame rate some or most of the time? I am unable to observe any change in frame rate when I use setCache(true) and I apply scaling and other transforms.

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, (eg 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 ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM