gltfio: Add ResourceLoader evict API.
This commit is contained in:
@@ -221,6 +221,7 @@ class ModelViewer(val engine: Engine) : android.view.View.OnTouchListener {
|
||||
fun destroyModel() {
|
||||
fetchResourcesJob?.cancel()
|
||||
resourceLoader.asyncCancelLoad()
|
||||
resourceLoader.evictResourceData()
|
||||
asset?.let { asset ->
|
||||
this.scene.removeEntities(asset.entities)
|
||||
assetLoader.destroyAsset(asset)
|
||||
|
||||
@@ -70,6 +70,13 @@ Java_com_google_android_filament_gltfio_ResourceLoader_nHasResourceData(JNIEnv*
|
||||
return status;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_google_android_filament_gltfio_ResourceLoader_nEvictResourceData(JNIEnv*, jclass,
|
||||
jlong nativeLoader) {
|
||||
ResourceLoader* loader = (ResourceLoader*) nativeLoader;
|
||||
loader->evictResourceData();
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_google_android_filament_gltfio_ResourceLoader_nLoadResources(JNIEnv*, jclass,
|
||||
jlong nativeLoader, jlong nativeAsset) {
|
||||
|
||||
@@ -101,6 +101,15 @@ public class ResourceLoader {
|
||||
return nHasResourceData(mNativeObject, uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees memory by evicting the URI cache that was populated via addResourceData.
|
||||
*
|
||||
* This can be called only after a model is fully loaded or after loading has been cancelled.
|
||||
*/
|
||||
public void evictResourceData() {
|
||||
nEvictResourceData(mNativeObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates through all external buffers and images and creates corresponding Filament objects
|
||||
* (vertex buffers, textures, etc), which become owned by the asset.
|
||||
@@ -161,6 +170,7 @@ public class ResourceLoader {
|
||||
private static native void nDestroyResourceLoader(long nativeLoader);
|
||||
private static native void nAddResourceData(long nativeLoader, String url, Buffer buffer,
|
||||
int remaining);
|
||||
private static native void nEvictResourceData(long nativeLoader);
|
||||
private static native boolean nHasResourceData(long nativeLoader, String url);
|
||||
private static native void nLoadResources(long nativeLoader, long nativeAsset);
|
||||
private static native boolean nAsyncBeginLoad(long nativeLoader, long nativeAsset);
|
||||
|
||||
@@ -93,6 +93,13 @@ public:
|
||||
*/
|
||||
bool hasResourceData(const char* uri) const;
|
||||
|
||||
/**
|
||||
* Frees memory by evicting the URI cache that was populated via addResourceData.
|
||||
*
|
||||
* This can be called only after a model is fully loaded or after loading has been cancelled.
|
||||
*/
|
||||
void evictResourceData();
|
||||
|
||||
/**
|
||||
* Loads resources for the given asset from the filesystem or data cache and "finalizes" the
|
||||
* asset by transforming the vertex data format if necessary, decoding image files, supplying
|
||||
|
||||
@@ -269,6 +269,11 @@ bool ResourceLoader::hasResourceData(const char* uri) const {
|
||||
return pImpl->mUriDataCache.find(uri) != pImpl->mUriDataCache.end();
|
||||
}
|
||||
|
||||
void ResourceLoader::evictResourceData() {
|
||||
// Note that this triggers BufferDescriptor callbacks.
|
||||
pImpl->mUriDataCache.clear();
|
||||
}
|
||||
|
||||
bool ResourceLoader::loadResources(FilamentAsset* asset) {
|
||||
FFilamentAsset* fasset = upcast(asset);
|
||||
return loadResources(fasset, false);
|
||||
|
||||
@@ -1017,6 +1017,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
filamentApp.setDropHandler([&] (std::string path) {
|
||||
app.resourceLoader->asyncCancelLoad();
|
||||
app.resourceLoader->evictResourceData();
|
||||
app.viewer->removeAsset();
|
||||
app.assetLoader->destroyAsset(app.asset);
|
||||
loadAsset(path);
|
||||
|
||||
Reference in New Issue
Block a user