Package com.jme3.util

Class NativeObject

java.lang.Object
com.jme3.util.NativeObject
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
AudioData, BufferObject, Filter, FrameBuffer, Image, Shader, Shader.ShaderSource, VertexBuffer

public abstract class NativeObject extends Object implements Cloneable
Describes a native object. An encapsulation of a certain object on the native side of the graphics or audio library. This class is used to track when OpenGL and OpenAL native objects are collected by the garbage collector, and then invoke the proper destructor on the OpenGL library to delete it from memory.
  • Field Details

    • INVALID_ID

      public static final int INVALID_ID
      See Also:
    • OBJTYPE_VERTEXBUFFER

      protected static final int OBJTYPE_VERTEXBUFFER
      See Also:
    • OBJTYPE_TEXTURE

      protected static final int OBJTYPE_TEXTURE
      See Also:
    • OBJTYPE_FRAMEBUFFER

      protected static final int OBJTYPE_FRAMEBUFFER
      See Also:
    • OBJTYPE_SHADER

      protected static final int OBJTYPE_SHADER
      See Also:
    • OBJTYPE_SHADERSOURCE

      protected static final int OBJTYPE_SHADERSOURCE
      See Also:
    • OBJTYPE_AUDIOBUFFER

      protected static final int OBJTYPE_AUDIOBUFFER
      See Also:
    • OBJTYPE_AUDIOSTREAM

      protected static final int OBJTYPE_AUDIOSTREAM
      See Also:
    • OBJTYPE_FILTER

      protected static final int OBJTYPE_FILTER
      See Also:
    • OBJTYPE_BO

      protected static final int OBJTYPE_BO
      See Also:
    • objectManager

      protected NativeObjectManager objectManager
      The object manager to which this NativeObject is registered to.
    • id

      protected int id
      The ID of the object, usually depends on its type. Typically returned from calls like glGenTextures, glGenBuffers, etc.
    • handleRef

      protected Object handleRef
      A reference to a "handle". By hard referencing a certain object, it's possible to find when a certain GLObject is no longer used, and to delete its instance from the graphics library.
    • updateNeeded

      protected boolean updateNeeded
      True if the data represented by this GLObject has been changed and needs to be updated before used.
  • Constructor Details

    • NativeObject

      public NativeObject()
      Creates a new GLObject. Should be called by the subclasses.
    • NativeObject

      protected NativeObject(int id)
      Protected constructor that doesn't allocate handle ref. This is used in subclasses for the createDestructableClone().
      Parameters:
      id - the desired ID
  • Method Details

    • setId

      public void setId(int id)
      Sets the ID of the NativeObject. This method is used in Renderer and must not be called by the user.
      Parameters:
      id - The ID to set
    • getId

      public int getId()
      Returns:
      The ID of the object. Should not be used by user code in most cases.
    • setUpdateNeeded

      public void setUpdateNeeded()
      Internal use only. Indicates that the object has changed and its state needs to be updated.
    • clearUpdateNeeded

      public void clearUpdateNeeded()
      Internal use only. Indicates that the state changes were applied.
    • isUpdateNeeded

      public boolean isUpdateNeeded()
      Internal use only. Check if setUpdateNeeded() was called before.
      Returns:
      true if an update is needed, otherwise false
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      protected NativeObject clone()
      This should create a deep clone. For a shallow clone, use createDestructableClone().
      Overrides:
      clone in class Object
    • deleteNativeBuffers

      protected void deleteNativeBuffers()
      Deletes any associated native buffers. This is necessary because it is unlikely that native buffers will be garbage collected naturally (due to how GC works), therefore the collection must be handled manually. Only implementations that manage native buffers need to override this method. Note that the behavior that occurs when a deleted native buffer is used is not defined, therefore this method is protected
    • resetObject

      public abstract void resetObject()
      Called when the GL context is restarted to reset all IDs. Prevents "white textures" on display restart.
    • deleteObject

      public abstract void deleteObject(Object rendererObject)
      Deletes the GL object from the GPU when it is no longer used. Called automatically by the GL object manager.
      Parameters:
      rendererObject - The renderer to be used to delete the object
    • createDestructableClone

      public abstract NativeObject createDestructableClone()
      Creates a shallow clone of this GL Object. The deleteObject method should be functional for this object.
      Returns:
      a new instance
    • getUniqueId

      public abstract long getUniqueId()
      Returns a unique ID for this NativeObject. No other NativeObject shall have the same ID.
      Returns:
      unique ID for this NativeObject.
    • dispose

      public void dispose()
      Reclaims native resources used by this NativeObject. It should be safe to call this method or even use the object after it has been reclaimed, unless NativeObjectManager.UNSAFE is set to true, in that case native buffers are also reclaimed which may introduce instability.
    • getWeakRef

      public <T> WeakReference<T> getWeakRef()
      Acquire a weak reference to this NativeObject.
      Type Parameters:
      T - the type
      Returns:
      a weak reference (possibly a pre-existing one)