use threadpool on C API

This commit is contained in:
Nick Fisher
2023-04-20 11:49:12 +08:00
parent dd01249547
commit c91362eefe
148 changed files with 17290 additions and 131 deletions

View File

@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'dart:ui' as ui;
import 'package:ffi/ffi.dart';
@@ -38,11 +39,17 @@ class FilamentController {
late FilamentViewer _viewer;
late AssetManager _assetManager;
final TickerProvider _tickerProvider;
Ticker? _ticker;
bool _rendering = false;
FilamentController(this._tickerProvider) {
final _port = ReceivePort();
///
/// This now uses an FFI implementation.
/// Platform channels are only used to setup the context/texture (since this is platform-specific) and the render ticker.
/// All other methods directly invoke the FFI functions defined in PolyvoxFilamentApi.cpp,
/// which itself uses a threadpool so that calls are run on a separate thread.
///
FilamentController() {
_channel.setMethodCallHandler((call) async {
throw Exception("Unknown method channel invocation ${call.method}");
});
@@ -63,6 +70,7 @@ class FilamentController {
Future setRendering(bool render) async {
_rendering = render;
_channel.invokeMethod("setRendering", render);
}
void render() {
@@ -78,7 +86,13 @@ class FilamentController {
_pixelRatio = ratio;
}
Future createTextureViewer(int width, int height) async {
Future createViewer(int width, int height) async {
// if (_viewer.address == 0) {
// throw Exception("TODO");
// }
_nativeLibrary.init_dart_api_dl(NativeApi.initializeApiDLData);
_nativeLibrary.register_filament_port(_port.sendPort.nativePort);
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
_textureId =
await _channel.invokeMethod("createTexture", [size.width, size.height]);
@@ -86,19 +100,15 @@ class FilamentController {
var glContext =
Pointer<Void>.fromAddress(await _channel.invokeMethod("getContext"));
final loadResource = Pointer<
NativeFunction<ResourceBuffer Function(Pointer<Char>)>>.fromAddress(
await _channel.invokeMethod("getLoadResourceFn"));
print("got $loadResource loadResource");
var freeResource =
Pointer<NativeFunction<Void Function(Uint32)>>.fromAddress(
await _channel.invokeMethod("getFreeResourceFn"));
_viewer = _nativeLibrary.create_filament_viewer(
glContext, loadResource, freeResource);
// don't pass a surface to the SwapChain as we are effectively creating a headless SwapChain that will render into a RenderTarget associated with a texture
_nativeLibrary.create_swap_chain(
_viewer, nullptr, size.width.toInt(), size.height.toInt());
@@ -112,14 +122,8 @@ class FilamentController {
_initialized.complete(true);
_assetManager = _nativeLibrary.get_asset_manager(_viewer);
print("got asset maanger $_assetManager");
_ticker = _tickerProvider.createTicker((elapsed) {
if (_rendering) {
render();
}
});
_ticker!.start();
await _channel.invokeMethod("setRenderTicker", _viewer.address);
}
Future resize(int width, int height,
@@ -358,7 +362,6 @@ class FilamentController {
void playAnimation(FilamentEntity asset, int index,
{bool loop = false, bool reverse = false}) async {
print("Playing animation @ $index");
_nativeLibrary.play_animation(
_assetManager, asset, index, loop ? 1 : 0, reverse ? 1 : 0);
}

View File

@@ -67,7 +67,7 @@ class _FilamentWidgetState extends State<FilamentWidget> {
print(
"Requesting creation of Filament back-end texture/viewer for viewport size $size");
await widget.controller
.createTextureViewer(size.width.toInt(), size.height.toInt());
.createViewer(size.width.toInt(), size.height.toInt());
print("Filament texture/viewer created.");
_listener!.cancel();
_listener = null;

View File

@@ -19,7 +19,34 @@ class NativeLibrary {
lookup)
: _lookup = lookup;
/// struct to facilitate passing bone animation frame data between Dart/native.
int init_dart_api_dl(
ffi.Pointer<ffi.Void> data,
) {
return _init_dart_api_dl(
data,
);
}
late final _init_dart_api_dlPtr =
_lookup<ffi.NativeFunction<ffi.IntPtr Function(ffi.Pointer<ffi.Void>)>>(
'init_dart_api_dl');
late final _init_dart_api_dl =
_init_dart_api_dlPtr.asFunction<int Function(ffi.Pointer<ffi.Void>)>();
void register_filament_port(
int port,
) {
return _register_filament_port(
port,
);
}
late final _register_filament_portPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(ffi.Int)>>(
'register_filament_port');
late final _register_filament_port =
_register_filament_portPtr.asFunction<void Function(int)>();
ffi.Pointer<ffi.Void> create_filament_viewer(
ffi.Pointer<ffi.Void> context,
ffi.Pointer<
@@ -52,6 +79,16 @@ class NativeLibrary {
ResourceBuffer Function(ffi.Pointer<ffi.Char>)>>,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Uint32)>>)>();
renderCallback make_render_callback() {
return _make_render_callback();
}
late final _make_render_callbackPtr =
_lookup<ffi.NativeFunction<renderCallback Function()>>(
'make_render_callback');
late final _make_render_callback =
_make_render_callbackPtr.asFunction<renderCallback Function()>();
void delete_filament_viewer(
ffi.Pointer<ffi.Void> viewer,
) {
@@ -1122,6 +1159,8 @@ class ResourceBuffer extends ffi.Struct {
external int id;
}
typedef renderCallback = ffi.Pointer<
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void> viewer)>>;
typedef EntityId = ffi.Int32;
const int _STDINT_H = 1;