Package icyllis.arc3d.engine
Class ResourceCache
java.lang.Object
icyllis.arc3d.engine.ResourceCache
Manages the lifetime of all
Resource instances.
Resources may optionally have two types of keys:
- Scratch key. This is for resources whose allocations are cached but not their contents. Multiple resources can share the same scratch key. This is so a caller can have two resource instances with the same properties (e.g. multi-pass rendering that ping-pongs between two temporary surfaces). The scratch key is set at resource creation time and should never change. Resources need not have a scratch key.
- Unique key. This key's meaning is specific to the domain that created the key. Only one resource may have a given unique key. The unique key can be set, cleared, or changed anytime after resource creation.
A unique key always takes precedence over a shared key when a resource has both types of keys. If a resource has neither key type then it will be deleted as soon as the last reference to it is dropped.
-
Method Summary
Modifier and TypeMethodDescription@Nullable ResourcefindAndRefResource(@NonNull IResourceKey key, boolean budgeted, boolean shareable) Find a resource that matches a key.longReturns the number of bytes consumed by budgeted resources.intReturns the number of resources that count against the budget.longReturns the number of bytes that cached resources should count against.longReturns the number of bytes held by unlocked resources which are available for cleanup.intReturns the number of cached resources.voidinsertResource(@NonNull Resource resource, @NonNull IResourceKey key, boolean budgeted, boolean shareable) booleanvoidPerforms any pending maintenance operations needed by the cache.voidDeallocates unlocked resources as much as possible.voidpurgeResourcesNotUsedSince(long timeMillis) Deallocates unlocked resources not used since the passed point in time.voidsetMaxBudget(long maxBytes) Sets the max GPU memory byte size of the cache.voidsetSurfaceProvider(ImageProxyCache imageProxyCache) voidsetThreadSafeCache(ThreadSafeCache threadSafeCache) voidshutdown()Called by the ResourceProvider when it is dropping its ref to the ResourceCache.
-
Method Details
-
setMaxBudget
@VisibleForTesting public void setMaxBudget(long maxBytes) Sets the max GPU memory byte size of the cache. ApurgeAsNeeded()is followed by this method call. The passed value can be retrieved bygetMaxBudget(). -
getResourceCount
public int getResourceCount()Returns the number of cached resources. -
getBudgetedResourceCount
public int getBudgetedResourceCount()Returns the number of resources that count against the budget. -
getBudgetedBytes
public long getBudgetedBytes()Returns the number of bytes consumed by budgeted resources. -
getPurgeableBytes
public long getPurgeableBytes()Returns the number of bytes held by unlocked resources which are available for cleanup. -
getMaxBudget
public long getMaxBudget()Returns the number of bytes that cached resources should count against. This can be changed bysetMaxBudget(long). -
shutdown
public void shutdown()Called by the ResourceProvider when it is dropping its ref to the ResourceCache. After this is called no more Resources can be returned to the ResourceCache (besides those already in the return queue). Also no new Resources can be retrieved from the ResourceCache. -
findAndRefResource
public @Nullable Resource findAndRefResource(@NonNull IResourceKey key, boolean budgeted, boolean shareable) Find a resource that matches a key. -
setSurfaceProvider
-
setThreadSafeCache
-
purgeAsNeeded
@VisibleForTesting public void purgeAsNeeded()Performs any pending maintenance operations needed by the cache. In particular, deallocates resources to become under budget and processes resources with invalidated unique keys. -
purgeResources
public void purgeResources()Deallocates unlocked resources as much as possible. All purgeable resources will be deleted. -
purgeResourcesNotUsedSince
public void purgeResourcesNotUsedSince(long timeMillis) Deallocates unlocked resources not used since the passed point in time. The time-base isSystem.currentTimeMillis(). Otherwise, all purgeable resources older thantimeMilliswill be deleted.- Parameters:
timeMillis- the resources older than this time will be deleted
-
processReturnedResources
@VisibleForTesting public boolean processReturnedResources() -
insertResource
public void insertResource(@NonNull Resource resource, @NonNull IResourceKey key, boolean budgeted, boolean shareable)
-