add onDestroy hooks for FilamentApp

This commit is contained in:
Nick Fisher
2025-04-02 22:27:12 +08:00
parent f7a7b6bedf
commit e0b2e1c3b9
3 changed files with 32 additions and 3 deletions

View File

@@ -11,7 +11,6 @@ class FilamentConfig<T, U> {
final U? renderCallbackOwner;
final Future<Uint8List> Function(String) resourceLoader;
final U? platform;
final U? driver;
final U? sharedContext;
final String? uberArchivePath;
final int stereoscopicEyeCount;
@@ -24,7 +23,6 @@ class FilamentConfig<T, U> {
this.renderCallback,
this.renderCallbackOwner,
this.platform,
this.driver,
this.sharedContext,
this.stereoscopicEyeCount = 1,
this.disableHandleUseAfterFreeCheck = false});
@@ -306,4 +304,9 @@ abstract class FilamentApp<T> {
///
///
Future flush();
///
///
///
void onDestroy(Future Function() callback);
}

View File

@@ -15,6 +15,7 @@ import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_texture.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_view.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/thermion_viewer_ffi.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:logging/logging.dart';
typedef RenderCallback = Pointer<NativeFunction<Void Function(Pointer<Void>)>>;
@@ -22,7 +23,6 @@ class FFIFilamentConfig extends FilamentConfig<RenderCallback, Pointer<Void>> {
FFIFilamentConfig(
{required super.resourceLoader,
super.backend = Backend.DEFAULT,
super.driver = null,
super.platform = null,
super.sharedContext = null,
super.uberArchivePath = null});
@@ -41,6 +41,8 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
final Future<Uint8List> Function(String uri) resourceLoader;
late final _logger = Logger(this.runtimeType.toString());
FFIFilamentApp(
this.engine,
this.gltfAssetLoader,
@@ -195,11 +197,13 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
///
///
Future destroySwapChain(SwapChain swapChain) async {
_logger.info("Destroying swapchain");
await withVoidCallback((callback) {
Engine_destroySwapChainRenderThread(
engine, (swapChain as FFISwapChain).swapChain, callback);
});
_swapChains.remove(swapChain);
_logger.info("Destroyed swapchain");
}
///
@@ -227,6 +231,11 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
calloc.free(viewsPtr);
FilamentApp.instance = null;
for (final callback in _onDestroy) {
await callback.call();
}
_onDestroy.clear();
}
///
@@ -914,6 +923,15 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
Future flush() async {
await withVoidCallback((cb) => Engine_flushAndWaitRenderThead(engine, cb));
}
final _onDestroy = <Future Function()>[];
///
///
///
void onDestroy(Future Function() callback) {
_onDestroy.add(callback);
}
}
class FinalizableUint8List implements Finalizable {

View File

@@ -146,12 +146,14 @@ class ThermionViewerFFI extends ThermionViewer {
}
final _onDispose = <Future Function()>[];
bool _disposed = false;
///
///
///
@override
Future dispose() async {
_disposed = true;
await setRendering(false);
await destroyAssets();
await destroyLights();
@@ -268,6 +270,10 @@ class ThermionViewerFFI extends ThermionViewer {
///
@override
Future removeSkybox() async {
if(_disposed) {
throw ViewerDisposedException();
}
if (skybox != null) {
await withVoidCallback(
(cb) => Engine_destroySkyboxRenderThread(app.engine, skybox!, cb));
@@ -713,3 +719,5 @@ class ThermionViewerFFI extends ThermionViewer {
await scene.remove(asset);
}
}
class ViewerDisposedException implements Exception {}