allow passing uberarchive path to FilamentViewer and expose destroy_viewer

This commit is contained in:
Nick Fisher
2023-10-04 14:49:48 +08:00
parent 6980e5cb23
commit 45576466c9
16 changed files with 368 additions and 261 deletions

View File

@@ -22,7 +22,19 @@ abstract class FilamentController {
Future render();
Future setFrameRate(int framerate);
void setPixelRatio(double ratio);
///
/// Destroys the viewer and all backing textures. You can leave the FilamentWidget in the hierarchy after this is called, but
Future destroy();
///
/// Destroys the viewer only, leaving the texture intact. You probably want to call [destroy] instead of this; [destroyViewer] is exposed mostly for lifecycle changes which are handled by FilamentWidget.
///
Future destroyViewer();
///
/// Destroys the backing texture. You probably want to call [destroy] instead of this; this is exposed mostly for lifecycle changes which are handled by FilamentWidget.
///
Future destroyTexture();
///

View File

@@ -30,11 +30,13 @@ class FilamentControllerFFI extends FilamentController {
bool _resizing = false;
final String? uberArchivePath;
///
/// This controller uses platform channels to bridge Dart with the C/C++ code for the Filament API.
/// Setting up the context/texture (since this is platform-specific) and the render ticker are platform-specific; all other methods are passed through by the platform channel to the methods specified in PolyvoxFilamentApi.h.
///
FilamentControllerFFI() {
FilamentControllerFFI({this.uberArchivePath}) {
_channel.setMethodCallHandler((call) async {
throw Exception("Unknown method channel invocation ${call.method}");
});
@@ -74,14 +76,23 @@ class FilamentControllerFFI extends FilamentController {
print("Set pixel ratio to $ratio");
}
@override
Future destroy() async {
await destroyViewer();
await destroyTexture();
}
@override
Future destroyViewer() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var viewer = _viewer;
_viewer = null;
_assetManager = null;
_lib.destroy_filament_viewer_ffi(_viewer!);
_lib.destroy_filament_viewer_ffi(viewer!);
_isReadyForScene = Completer();
}
@@ -139,6 +150,7 @@ class FilamentControllerFFI extends FilamentController {
_viewer = _lib.create_filament_viewer_ffi(
Pointer<Void>.fromAddress(sharedContext ?? 0),
driver,
uberArchivePath?.toNativeUtf8().cast<Char>() ?? nullptr,
Pointer<ResourceLoaderWrapper>.fromAddress(loader),
renderCallback,
renderCallbackOwner);

View File

@@ -9,6 +9,10 @@ import 'filament_controller.dart';
typedef AssetManager = int;
///
/// This is a previous iteration of FilamentController that used platform channels for every distinct platform.
/// This is no longer used; currently kept only for reference/posterity.
///
class FilamentControllerMethodChannel extends FilamentController {
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
@@ -636,4 +640,10 @@ class FilamentControllerMethodChannel extends FilamentController {
throw Exception("Failed to reveal mesh $meshName");
}
}
@override
Future destroy() {
// TODO: implement destroy
throw UnimplementedError();
}
}

View File

@@ -23,11 +23,13 @@ class NativeLibrary {
ffi.Pointer<ffi.Void> context,
ffi.Pointer<ResourceLoaderWrapper> loader,
ffi.Pointer<ffi.Void> platform,
ffi.Pointer<ffi.Char> uberArchivePath,
) {
return _create_filament_viewer(
context,
loader,
platform,
uberArchivePath,
);
}
@@ -36,10 +38,14 @@ class NativeLibrary {
ffi.Pointer<ffi.Void> Function(
ffi.Pointer<ffi.Void>,
ffi.Pointer<ResourceLoaderWrapper>,
ffi.Pointer<ffi.Void>)>>('create_filament_viewer');
ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Char>)>>('create_filament_viewer');
late final _create_filament_viewer = _create_filament_viewerPtr.asFunction<
ffi.Pointer<ffi.Void> Function(ffi.Pointer<ffi.Void>,
ffi.Pointer<ResourceLoaderWrapper>, ffi.Pointer<ffi.Void>)>();
ffi.Pointer<ffi.Void> Function(
ffi.Pointer<ffi.Void>,
ffi.Pointer<ResourceLoaderWrapper>,
ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Char>)>();
void destroy_filament_viewer(
ffi.Pointer<ffi.Void> viewer,
@@ -1326,6 +1332,7 @@ class NativeLibrary {
ffi.Pointer<ffi.Void> create_filament_viewer_ffi(
ffi.Pointer<ffi.Void> context,
ffi.Pointer<ffi.Void> platform,
ffi.Pointer<ffi.Char> uberArchivePath,
ffi.Pointer<ResourceLoaderWrapper> loader,
ffi.Pointer<
ffi.NativeFunction<
@@ -1336,6 +1343,7 @@ class NativeLibrary {
return _create_filament_viewer_ffi(
context,
platform,
uberArchivePath,
loader,
renderCallback,
renderCallbackOwner,
@@ -1347,6 +1355,7 @@ class NativeLibrary {
ffi.Pointer<ffi.Void> Function(
ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Char>,
ffi.Pointer<ResourceLoaderWrapper>,
ffi.Pointer<
ffi.NativeFunction<
@@ -1358,6 +1367,7 @@ class NativeLibrary {
ffi.Pointer<ffi.Void> Function(
ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Void>,
ffi.Pointer<ffi.Char>,
ffi.Pointer<ResourceLoaderWrapper>,
ffi.Pointer<
ffi.NativeFunction<
@@ -2146,7 +2156,7 @@ class NativeLibrary {
_get_morph_target_name_count_ffiPtr.asFunction<
int Function(ffi.Pointer<ffi.Void>, int, ffi.Pointer<ffi.Char>)>();
int set_post_processing_ffi(
void set_post_processing_ffi(
ffi.Pointer<ffi.Void> viewer,
bool enabled,
) {
@@ -2158,10 +2168,10 @@ class NativeLibrary {
late final _set_post_processing_ffiPtr = _lookup<
ffi
.NativeFunction<ffi.Int Function(ffi.Pointer<ffi.Void>, ffi.Bool)>>(
.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void>, ffi.Bool)>>(
'set_post_processing_ffi');
late final _set_post_processing_ffi = _set_post_processing_ffiPtr
.asFunction<int Function(ffi.Pointer<ffi.Void>, bool)>();
.asFunction<void Function(ffi.Pointer<ffi.Void>, bool)>();
void ios_dummy_ffi() {
return _ios_dummy_ffi();