Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8262100

PixelBuffer and externally allocated NIO buffers

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The PixelBuffer mechanism introduced in JavaFX 13 is quite useful but suffers from a flaw preventing to use it safely with externally-allocated buffers.

      Say you have an existing DirectBuffer containing Pixel that was allocated by a native library (e.g. for instance by OpenCV), you may create an Image using this buffer without copy.
      Then you can can display the image with an ImageView.
      At some point you change the image displayed by the ImageView. Then you would like to ask your native library to free the now unused DirectBuffer. However, the now unused Image still has a reference to the DirectBuffer and there is no way you can guarantee that the JVM won't try to access the buffer.
      And, in effect, even after a imageView.setImage(someNewImage), if the buffer is deallocated, the JVM can crash with dumps like:

      Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
      j com.sun.prism.es2.GLContext.nTexSubImage2D0(IIIIIIIILjava/lang/Object;I)V+0 javafx.graphics
      J 6253 c1 com.sun.prism.es2.GLContext.texSubImage2D(IIIIIIIILjava/nio/Buffer;)V javafx.graphics (65 bytes) @ 0x00007f7a64b2f254 [0x00007f7a64b2f100+0x0000000000000154]
      J 5393 c1 com.sun.prism.es2.ES2Texture.uploadPixels(Lcom/sun/prism/es2/GLContext;ILjava/nio/Buffer;Lcom/sun/prism/PixelFormat;IIIIIIIIIZZ)Z javafx.graphics (623 bytes) @ 0x00007f7a6552498c [0x00007f7a65522ee0+0x0000000000001aac]
      J 5421 c1 com.sun.prism.es2.ES2Texture.update(Ljava/nio/Buffer;Lcom/sun/prism/PixelFormat;IIIIIIIZ)V javafx.graphics (649 bytes) @ 0x00007f7a65534054 [0x00007f7a65533000+0x0000000000001054]

      So the PixelBuffer is not really usable with NIO buffer not allocated by the JVM. The only workaround I have found so far is to manage a set of weak references to the direct NIO buffers used by the pixel buffers and free the native buffers when the weak references are cleared.

      Please consider adding a way to secure the use of PixelBuffer with externally allocated NIO buffers. Maybe adding some method that invalidates the Image or the PixelBuffer and ensures the buffer won't be accessed anymore.



            kcr Kevin Rushforth
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: