4 Replies Latest reply on Dec 11, 2012 8:40 PM by 978435

    Scene stutters when making major node modifications

      I'm currently experiencing some fairly significant scene stuttering when I make large node modifications in to active nodes in the screen. Is there any way to push this work into he background to minimize this effect?

      The main case that I'd like to resolve is when I load up a new image of a decent size ( let's say 800 X 600 ). We are doing a port from Java2D to JavaFX, so for some of our rendering, we continue to render using Java2D onto BufferedImages, then, when that rendering is complete, we convert the BufferedImage into a JavaFX Image ( using SwingFXUtils), and then apply that image to an ImageView node that's already part of the scene. All of the work, up to the point where we actually set the image into to the ImageView is done on a background thread. This is all working, but when I do this, I often see cases where we miss several frames soon after the new image is applied. If we have any full motion animation on the screen we will see it stutter pretty noticeably. I've noticed the same problem when adding a WebView to a scene, and the stuttering is amplified in that case because of all the rendering work that is presumably going on in the WebView node while a web page is loading.

      My question is: Are there any tricks to minimize this? E.g Would this be better if I created and added a totally new ImageView, or hid the ImageView before adding the new Image and re-show afterwards? Or is this just an artifact of how the JavaFX pipeline works?
        • 1. Re: Scene stutters when making major node modifications
          I don't know where your flickering comes from and actually I think that JavaFX should be able to handle such a seemingly trivial case without problems but if you don't find any other more straight forward solution it might be worth using a Canvas instead of an ImageView. Some time ago I have seen here a quite impressive demo on how to integrate the VLC video player into JavaFX this way. The trick was to push the raw pixel data from a memory source into a canvas. This could be done in FullHD at a high frame rate without any flickering. I tried it myself on my laptop and it worked.
          • 2. Re: Scene stutters when making major node modifications
            Thanks, I'm going to experiment with Canvas a little to see if I can get any better results.

            I'll clarify my post a little to make sure it's totally clear what my problem is.

            I have a scene that is filled with dynamic content, and usually has 10-15 nodes in it. This scene is rendering at 1920x1080. I have an animation timer that runs at 60hz, and calls into my code on every pulse to update any nodes that have changes to apply to the scene. Some of the nodes swap in new images every 10-15 seconds using the mechanism described above, while other nodes update their position on every pulse to animate around the screen. I track the time between calls to handle() in my animation timer, and normally the pulses are 16.667ms apart very consistently. However, one or two pulses after I see a new image applied the scene, I see the time between pulses jump to 33.333ms ( like we just skipped a pulse ) and even up to close to 100 ms ( several pulses skipped ). Then after a couple of these laggy pulses, the animation timer returns to normal. When this long of a pause occurs between pulses, the animating content on the screen visibly halts, then jumps into position when the next pulse comes through. I also track how long my internal handle() work takes to make sure I'm not introducing the lag and I almost always return in less than 1ms.

            I hadn't considered the use of Canvas though. Assuming I can get that drawing operation completed within my handle window, that might resolve the issues I'm seeing.
            • 3. Re: Scene stutters when making major node modifications
              Here is the link
              Canvas performance
              And make sure you use the right WritablePixelFormat. See my last
              comment in this thread. It makes a big difference.
              • 4. Re: Scene stutters when making major node modifications
                I haven't had time to experiment with this yet, but I'll update the thread as soon I have some concrete results. Thanks again!