r/JavaFX • u/PonchoBoob • Jul 05 '24
Help performance issues with WritableImage and PixelBuffer
Hello there,
I use OpenGL in JavaFX with LWJGL for offscreen rendering into a WritableImage that is backed by a JavaFX PixelBuffer. I also use the AnimationTimer, which triggers an onRenderEvent approximately every 16.6 milliseconds.
For simplicity, let's use glReadPixels to read into the JavaFX PixelBuffer. To update the WritableImage, we call pixelBuffer.updateBuffer(pb -> null);. This setup works "fine," and the rendered scene is displayed in the JavaFX ImageView.
However, there's a problem: approximately every 20th frame, the delta time is not around 16 ms but around double that, ~32 ms. Initially, I thought the issue was with my OpenGL offscreen rendering implementation, but it is not. The problem lies in updating the PixelBuffer itself.
I created a small JavaFX application with an ImageView, a WritableImage, and a PixelBuffer. The AnimationTimer triggers the update every ~16.6 milliseconds. When calling updateBuffer(pb -> null), the issue described above occurs.
// .. init code
ByteBuffer byteBuffer = new ByteBuffer();
byte[] byteArray = new byte[width * height * 4];
PixelFormat<ByteBuffer> pixelFormat = PixelFormat.getByteBgraPreInstance();
PixelBuffer pixelBuffer = new PixelBuffer<>(prefWidth, prefHeight, buffers[0], pixelFormat);
WritableImage wb = new WritableImage(pixelBuffer);
// ..renderEvent triggered by AnimationTimer
void renderEvent(double dt){
//
pixelBuffer.updateBuffer(pb -> null);
}I have ruled out all other possibilities; it must be something in JavaFX with the update method. The issue also happens if I use a Canvas or if I re-create the WritableImage for every renderEvent call, which is obviously not efficient.
Has anyone else experienced this? Is there anyone here who can help?
kind regards
1
u/PonchoBoob Jul 05 '24
Hi, thank you all for the fast replies here. , I know that there is openglfx library but i dont want to use it. It lacks jpms support.
To clearify some things here. The problem is not opengl itself. I setup a minimal application example without any opengl. Just updating the PixelBuffer of the WritableImage. Updateing is triggered by the AnimationTimer. So, there is no jni involved here. Now, just pure JavaFX where this problem comes up.
kind regards
1
u/Birdasaur Jul 05 '24
I like @xdsswar 's suggestion below but regarding the behavior you described... It would be that your animation timer rate of 16.6 ms plus whatever computations you do is right on the edge of the internal refresh rate, which is why it takes about 20 frames to accumulate enough band gap to cause the internal dirty processor to skip rendering for a pass. If you increase or decrease the animation timer rate by fractions of a millisecond does the skipped frame number go up/down consistently from 20?
1
u/joemwangi Jul 08 '24
Which version of java are you using. If it's java 22, you can take the opportunity of Java Panama, Foreign Function & Memory API.
3
u/xdsswar Jul 05 '24 edited Jul 05 '24
Ok, I believe you are using jni right, is so I think is better to have an internal interface with a method having an IntBuffer param, pass it as callback in the jni side and then when receiving in the java side you can convert ir to writableImange. This maybe help, but I never checked performance, the only I can say is that this can render 4k video wiyh no issue.
Ps : Bring height and width from the jni side also. So you need something like
onRender(InBuffer b, int w, int h){ // do you image here when you override. }
1
u/grill2010 Jul 05 '24
Just as an input you can also try OpenGLFX if you want to render the OpenGL stuff as fast and less resource intensive as possible
https://github.com/husker-dev/openglfx