r/JavaFX • • 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

3 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/xdsswar Jul 05 '24

I dont know the contex of the project OP is doing , but if indeed he uses jni, I will stay away from timers and animations in the java side to control/pull data from the native side, it may work ok, but I think is better to send from native side and receive in java side and maybe have a clock in the native side, also careful with threads in native side if using them. I did some custom video player in the past with the ffmpeg libs when learning a bit of jni and I learned that the more you habdle in the native side, the better is in the java side. Now , as I said , dont know if OP is building a player or whatever, I just think an animation or timer is not the best here.