Class BasicGpuResourceCache

  • All Implemented Interfaces:
    GpuResourceCache

    public class BasicGpuResourceCache
    extends java.lang.Object
    implements GpuResourceCache
    Provides the interface for caching of OpenGL resources that are stored on or registered with a GL context. This cache maintains a map of resources that fit within a specifiable memory capacity. If adding a resource would exceed this cache's capacity, existing but least recently used resources are removed from the cache to make room. The cache is reduced to the "low water" size in this case (see setLowWater(long).

    When a resource is removed from the cache, and if it is a recognized OpenGL resource -- a texture, a list of vertex buffer IDs, a list of display list IDs, etc. -- and there is a current Open GL context, the appropriate glDelete function is called to de-register the resource with the GPU. If there is no current OpenGL context the resource is not deleted and will likely remain allocated on the GPU until the GL context is destroyed.

    • Constructor Summary

      Constructors 
      Constructor Description
      BasicGpuResourceCache​(long loWater, long hiWater)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all entries from this cache.
      protected long computeEntrySize​(BasicGpuResourceCache.CacheEntry entry)  
      protected long computeTextureSize​(BasicGpuResourceCache.CacheEntry entry)  
      boolean contains​(java.lang.Object key)
      Indicates whether a resource is in the cache.
      protected BasicGpuResourceCache.CacheEntry createCacheEntry​(java.lang.Object resource, java.lang.String resourceType)  
      protected BasicGpuResourceCache.CacheEntry createCacheEntry​(java.lang.Object resource, java.lang.String resourceType, long size)  
      java.lang.Object get​(java.lang.Object key)
      Finds and returns a resource from this cache.
      long getCapacity()
      Indicates this cache's capacity in bytes.
      long getFreeCapacity()
      Indicates this cache's memory capacity not used by its cached objects.
      long getLowWater()
      Returns the low water level in bytes.
      int getNumObjects()
      Indicates the number of resources in this cache.
      com.jogamp.opengl.util.texture.Texture getTexture​(java.lang.Object key)
      Finds and returns a texture resource from this cache.
      long getUsedCapacity()
      Indicates the amount of memory used by cached objects in this cache.
      protected void onEntryRemoved​(java.lang.Object key, java.lang.Object clientObject)  
      void put​(java.lang.Object key, com.jogamp.opengl.util.texture.Texture texture)
      Add a resource to this cache.
      void put​(java.lang.Object key, java.lang.Object resource, java.lang.String resourceType, long size)
      Adds a new resource to this cache.
      void remove​(java.lang.Object key)
      Removes a resource from this cache.
      void setCapacity​(long newCapacity)
      Sets the new capacity (in bytes) for the cache.
      void setLowWater​(long loWater)
      Sets the new low water level in bytes, which controls how aggresively the cache discards items.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BasicGpuResourceCache

        public BasicGpuResourceCache​(long loWater,
                                     long hiWater)
    • Method Detail

      • onEntryRemoved

        protected void onEntryRemoved​(java.lang.Object key,
                                      java.lang.Object clientObject)
      • put

        public void put​(java.lang.Object key,
                        com.jogamp.opengl.util.texture.Texture texture)
        Description copied from interface: GpuResourceCache
        Add a resource to this cache.
        Specified by:
        put in interface GpuResourceCache
        Parameters:
        key - the key identifying the resource.
        texture - the resource to add to this cache.
      • put

        public void put​(java.lang.Object key,
                        java.lang.Object resource,
                        java.lang.String resourceType,
                        long size)
        Description copied from interface: GpuResourceCache
        Adds a new resource to this cache.
        Specified by:
        put in interface GpuResourceCache
        Parameters:
        key - the key identifying the resource.
        resource - the resource cached.
        resourceType - the type of resource, one of the resource-type constants described above.
        size - the size of the resource, expressed as the number of bytes it requires on the GPU.
        See Also:
        GpuResourceCache.put(Object, com.jogamp.opengl.util.texture.Texture)
      • get

        public java.lang.Object get​(java.lang.Object key)
        Description copied from interface: GpuResourceCache
        Finds and returns a resource from this cache.
        Specified by:
        get in interface GpuResourceCache
        Parameters:
        key - the resource's key.
        Returns:
        the resource associated with the key, or null if the key is not found in the cache.
      • getTexture

        public com.jogamp.opengl.util.texture.Texture getTexture​(java.lang.Object key)
        Description copied from interface: GpuResourceCache
        Finds and returns a texture resource from this cache.
        Specified by:
        getTexture in interface GpuResourceCache
        Parameters:
        key - the texture resource's key.
        Returns:
        the texture resource associated with the key, or null if the key is not found in the cache.
      • remove

        public void remove​(java.lang.Object key)
        Description copied from interface: GpuResourceCache
        Removes a resource from this cache.
        Specified by:
        remove in interface GpuResourceCache
        Parameters:
        key - the resource's key.
      • getNumObjects

        public int getNumObjects()
        Description copied from interface: GpuResourceCache
        Indicates the number of resources in this cache.
        Specified by:
        getNumObjects in interface GpuResourceCache
        Returns:
        the number of resources in this cache.
      • getCapacity

        public long getCapacity()
        Description copied from interface: GpuResourceCache
        Indicates this cache's capacity in bytes.
        Specified by:
        getCapacity in interface GpuResourceCache
        Returns:
        this cache's capacity in bytes.
      • getUsedCapacity

        public long getUsedCapacity()
        Description copied from interface: GpuResourceCache
        Indicates the amount of memory used by cached objects in this cache.
        Specified by:
        getUsedCapacity in interface GpuResourceCache
        Returns:
        the number of bytes of memory used by objects in this cache, as determined by the size associated with each resource.
        See Also:
        GpuResourceCache.getCapacity()
      • contains

        public boolean contains​(java.lang.Object key)
        Description copied from interface: GpuResourceCache
        Indicates whether a resource is in the cache.
        Specified by:
        contains in interface GpuResourceCache
        Parameters:
        key - the resource's key.
        Returns:
        true if the resource is in the cache, otherwise false.
      • setCapacity

        public void setCapacity​(long newCapacity)
        Sets the new capacity (in bytes) for the cache. When decreasing cache size, it is recommended to check that the lowWater variable is suitable. If the capacity infringes on items stored in the cache, these items are removed. Setting a new low water is up to the user, that is, it remains unchanged and may be higher than the maximum capacity. When the low water level is higher than or equal to the maximum capacity, it is ignored, which can lead to poor performance when adding entries.
        Specified by:
        setCapacity in interface GpuResourceCache
        Parameters:
        newCapacity - the new capacity of the cache.
        See Also:
        GpuResourceCache.setLowWater(long)
      • setLowWater

        public void setLowWater​(long loWater)
        Sets the new low water level in bytes, which controls how aggresively the cache discards items.

        When the cache fills, it removes items until it reaches the low water level.

        Setting a high loWater level will increase cache misses, but decrease average add time, but setting a low loWater will do the opposite.

        Specified by:
        setLowWater in interface GpuResourceCache
        Parameters:
        loWater - the new low water level in bytes.
        See Also:
        GpuResourceCache.setCapacity(long), GpuResourceCache.remove(Object)