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 U? renderCallbackOwner;
final Future<Uint8List> Function(String) resourceLoader; final Future<Uint8List> Function(String) resourceLoader;
final U? platform; final U? platform;
final U? driver;
final U? sharedContext; final U? sharedContext;
final String? uberArchivePath; final String? uberArchivePath;
final int stereoscopicEyeCount; final int stereoscopicEyeCount;
@@ -24,7 +23,6 @@ class FilamentConfig<T, U> {
this.renderCallback, this.renderCallback,
this.renderCallbackOwner, this.renderCallbackOwner,
this.platform, this.platform,
this.driver,
this.sharedContext, this.sharedContext,
this.stereoscopicEyeCount = 1, this.stereoscopicEyeCount = 1,
this.disableHandleUseAfterFreeCheck = false}); this.disableHandleUseAfterFreeCheck = false});
@@ -306,4 +304,9 @@ abstract class FilamentApp<T> {
/// ///
/// ///
Future flush(); 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/ffi_view.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/thermion_viewer_ffi.dart'; import 'package:thermion_dart/src/viewer/src/ffi/src/thermion_viewer_ffi.dart';
import 'package:thermion_dart/thermion_dart.dart'; import 'package:thermion_dart/thermion_dart.dart';
import 'package:logging/logging.dart';
typedef RenderCallback = Pointer<NativeFunction<Void Function(Pointer<Void>)>>; typedef RenderCallback = Pointer<NativeFunction<Void Function(Pointer<Void>)>>;
@@ -22,7 +23,6 @@ class FFIFilamentConfig extends FilamentConfig<RenderCallback, Pointer<Void>> {
FFIFilamentConfig( FFIFilamentConfig(
{required super.resourceLoader, {required super.resourceLoader,
super.backend = Backend.DEFAULT, super.backend = Backend.DEFAULT,
super.driver = null,
super.platform = null, super.platform = null,
super.sharedContext = null, super.sharedContext = null,
super.uberArchivePath = null}); super.uberArchivePath = null});
@@ -41,6 +41,8 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
final Future<Uint8List> Function(String uri) resourceLoader; final Future<Uint8List> Function(String uri) resourceLoader;
late final _logger = Logger(this.runtimeType.toString());
FFIFilamentApp( FFIFilamentApp(
this.engine, this.engine,
this.gltfAssetLoader, this.gltfAssetLoader,
@@ -195,11 +197,13 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
/// ///
/// ///
Future destroySwapChain(SwapChain swapChain) async { Future destroySwapChain(SwapChain swapChain) async {
_logger.info("Destroying swapchain");
await withVoidCallback((callback) { await withVoidCallback((callback) {
Engine_destroySwapChainRenderThread( Engine_destroySwapChainRenderThread(
engine, (swapChain as FFISwapChain).swapChain, callback); engine, (swapChain as FFISwapChain).swapChain, callback);
}); });
_swapChains.remove(swapChain); _swapChains.remove(swapChain);
_logger.info("Destroyed swapchain");
} }
/// ///
@@ -227,6 +231,11 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
calloc.free(viewsPtr); calloc.free(viewsPtr);
FilamentApp.instance = null; 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 { Future flush() async {
await withVoidCallback((cb) => Engine_flushAndWaitRenderThead(engine, cb)); await withVoidCallback((cb) => Engine_flushAndWaitRenderThead(engine, cb));
} }
final _onDestroy = <Future Function()>[];
///
///
///
void onDestroy(Future Function() callback) {
_onDestroy.add(callback);
}
} }
class FinalizableUint8List implements Finalizable { class FinalizableUint8List implements Finalizable {

View File

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