Package gov.nasa.worldwind.cache
Class BasicGpuResourceCache
- java.lang.Object
-
- gov.nasa.worldwind.cache.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 (seesetLowWater(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
Nested Classes Modifier and Type Class Description static classBasicGpuResourceCache.CacheEntry
-
Field Summary
Fields Modifier and Type Field Description protected BasicMemoryCacheresources-
Fields inherited from interface gov.nasa.worldwind.cache.GpuResourceCache
DISPLAY_LISTS, TEXTURE, VBO_BUFFERS
-
-
Constructor Summary
Constructors Constructor Description BasicGpuResourceCache(long loWater, long hiWater)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all entries from this cache.protected longcomputeEntrySize(BasicGpuResourceCache.CacheEntry entry)protected longcomputeTextureSize(BasicGpuResourceCache.CacheEntry entry)booleancontains(java.lang.Object key)Indicates whether a resource is in the cache.protected BasicGpuResourceCache.CacheEntrycreateCacheEntry(java.lang.Object resource, java.lang.String resourceType)protected BasicGpuResourceCache.CacheEntrycreateCacheEntry(java.lang.Object resource, java.lang.String resourceType, long size)java.lang.Objectget(java.lang.Object key)Finds and returns a resource from this cache.longgetCapacity()Indicates this cache's capacity in bytes.longgetFreeCapacity()Indicates this cache's memory capacity not used by its cached objects.longgetLowWater()Returns the low water level in bytes.intgetNumObjects()Indicates the number of resources in this cache.com.jogamp.opengl.util.texture.TexturegetTexture(java.lang.Object key)Finds and returns a texture resource from this cache.longgetUsedCapacity()Indicates the amount of memory used by cached objects in this cache.protected voidonEntryRemoved(java.lang.Object key, java.lang.Object clientObject)voidput(java.lang.Object key, com.jogamp.opengl.util.texture.Texture texture)Add a resource to this cache.voidput(java.lang.Object key, java.lang.Object resource, java.lang.String resourceType, long size)Adds a new resource to this cache.voidremove(java.lang.Object key)Removes a resource from this cache.voidsetCapacity(long newCapacity)Sets the new capacity (in bytes) for the cache.voidsetLowWater(long loWater)Sets the new low water level in bytes, which controls how aggresively the cache discards items.
-
-
-
Field Detail
-
resources
protected final BasicMemoryCache resources
-
-
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:GpuResourceCacheAdd a resource to this cache.- Specified by:
putin interfaceGpuResourceCache- 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:GpuResourceCacheAdds a new resource to this cache.- Specified by:
putin interfaceGpuResourceCache- 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)
-
createCacheEntry
protected BasicGpuResourceCache.CacheEntry createCacheEntry(java.lang.Object resource, java.lang.String resourceType)
-
createCacheEntry
protected BasicGpuResourceCache.CacheEntry createCacheEntry(java.lang.Object resource, java.lang.String resourceType, long size)
-
get
public java.lang.Object get(java.lang.Object key)
Description copied from interface:GpuResourceCacheFinds and returns a resource from this cache.- Specified by:
getin interfaceGpuResourceCache- 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:GpuResourceCacheFinds and returns a texture resource from this cache.- Specified by:
getTexturein interfaceGpuResourceCache- 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:GpuResourceCacheRemoves a resource from this cache.- Specified by:
removein interfaceGpuResourceCache- Parameters:
key- the resource's key.
-
getNumObjects
public int getNumObjects()
Description copied from interface:GpuResourceCacheIndicates the number of resources in this cache.- Specified by:
getNumObjectsin interfaceGpuResourceCache- Returns:
- the number of resources in this cache.
-
getCapacity
public long getCapacity()
Description copied from interface:GpuResourceCacheIndicates this cache's capacity in bytes.- Specified by:
getCapacityin interfaceGpuResourceCache- Returns:
- this cache's capacity in bytes.
-
getUsedCapacity
public long getUsedCapacity()
Description copied from interface:GpuResourceCacheIndicates the amount of memory used by cached objects in this cache.- Specified by:
getUsedCapacityin interfaceGpuResourceCache- 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()
-
getFreeCapacity
public long getFreeCapacity()
Description copied from interface:GpuResourceCacheIndicates this cache's memory capacity not used by its cached objects.- Specified by:
getFreeCapacityin interfaceGpuResourceCache- Returns:
- the number of bytes of this cache's memory capacity not used by its cached objects.
- See Also:
GpuResourceCache.getCapacity()
-
contains
public boolean contains(java.lang.Object key)
Description copied from interface:GpuResourceCacheIndicates whether a resource is in the cache.- Specified by:
containsin interfaceGpuResourceCache- Parameters:
key- the resource's key.- Returns:
- true if the resource is in the cache, otherwise false.
-
clear
public void clear()
Description copied from interface:GpuResourceCacheRemoves all entries from this cache.- Specified by:
clearin interfaceGpuResourceCache
-
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:
setCapacityin interfaceGpuResourceCache- 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:
setLowWaterin interfaceGpuResourceCache- Parameters:
loWater- the new low water level in bytes.- See Also:
GpuResourceCache.setCapacity(long),GpuResourceCache.remove(Object)
-
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:
getLowWaterin interfaceGpuResourceCache- Returns:
- the low water level in bytes.
- See Also:
GpuResourceCache.setLowWater(long)
-
computeEntrySize
protected long computeEntrySize(BasicGpuResourceCache.CacheEntry entry)
-
computeTextureSize
protected long computeTextureSize(BasicGpuResourceCache.CacheEntry entry)
-
-