NASA World Wind

gov.nasa.worldwind.cache
Class BasicGpuResourceCache

java.lang.Object
  extended by gov.nasa.worldwind.cache.BasicGpuResourceCache
All Implemented Interfaces:
GpuResourceCache

public class BasicGpuResourceCache
extends 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.


Nested Class Summary
static class BasicGpuResourceCache.CacheEntry
           
 
Field Summary
protected  BasicMemoryCache resources
           
 
Fields inherited from interface gov.nasa.worldwind.cache.GpuResourceCache
DISPLAY_LISTS, TEXTURE, VBO_BUFFERS
 
Constructor Summary
BasicGpuResourceCache(long loWater, long hiWater)
           
 
Method Summary
 void clear()
          Removes all entries from this cache.
protected  long computeEntrySize(BasicGpuResourceCache.CacheEntry entry)
           
protected  long computeTextureSize(BasicGpuResourceCache.CacheEntry entry)
           
 boolean contains(Object key)
          Indicates whether a resource is in the cache.
protected  BasicGpuResourceCache.CacheEntry createCacheEntry(Object resource, String resourceType)
           
protected  BasicGpuResourceCache.CacheEntry createCacheEntry(Object resource, String resourceType, long size)
           
 Object get(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.
 Texture getTexture(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(Object key, Object clientObject)
           
 void put(Object key, Object resource, String resourceType, long size)
          Adds a new resource to this cache.
 void put(Object key, Texture texture)
          Add a resource to this cache.
 void remove(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, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

resources

protected final BasicMemoryCache resources
Constructor Detail

BasicGpuResourceCache

public BasicGpuResourceCache(long loWater,
                             long hiWater)
Method Detail

clear

public void clear()
Description copied from interface: GpuResourceCache
Removes all entries from this cache.

Specified by:
clear in interface GpuResourceCache

computeEntrySize

protected long computeEntrySize(BasicGpuResourceCache.CacheEntry entry)

computeTextureSize

protected long computeTextureSize(BasicGpuResourceCache.CacheEntry entry)

contains

public boolean contains(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.

createCacheEntry

protected BasicGpuResourceCache.CacheEntry createCacheEntry(Object resource,
                                                            String resourceType)

createCacheEntry

protected BasicGpuResourceCache.CacheEntry createCacheEntry(Object resource,
                                                            String resourceType,
                                                            long size)

get

public Object get(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.

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.

getFreeCapacity

public long getFreeCapacity()
Description copied from interface: GpuResourceCache
Indicates this cache's memory capacity not used by its cached objects.

Specified by:
getFreeCapacity in interface GpuResourceCache
Returns:
the number of bytes of this cache's memory capacity not used by its cached objects.
See Also:
GpuResourceCache.getCapacity()

getLowWater

public long getLowWater()
Returns the low water level in bytes. When the cache fills, it removes items until it reaches the low water level.

Specified by:
getLowWater in interface GpuResourceCache
Returns:
the low water level in bytes.
See Also:
GpuResourceCache.setLowWater(long)

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.

getTexture

public Texture getTexture(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.

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()

onEntryRemoved

protected void onEntryRemoved(Object key,
                              Object clientObject)

put

public void put(Object key,
                Object resource,
                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)

put

public void put(Object key,
                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.

remove

public void remove(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.

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)

NASA World Wind