add NativeHandle interface and refactor SwapChain to use getNativeHandle

This commit is contained in:
Nick Fisher
2025-06-24 10:35:33 +08:00
parent 9aec83fd70
commit c7d4d9cb33
4 changed files with 33 additions and 9 deletions

View File

@@ -145,7 +145,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
}
}
RenderTicker_setRenderable(
renderTicker, swapChain.swapChain, viewsPtr, numRenderable);
renderTicker, swapChain.getNativeHandle(), viewsPtr, numRenderable);
_logger.info("Updated render order, $numRenderable renderable views");
}
}
@@ -223,7 +223,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
_logger.info("Destroying swapchain");
await withVoidCallback((requestId, callback) {
Engine_destroySwapChainRenderThread(
engine, (swapChain as FFISwapChain).swapChain, requestId, callback);
engine, swapChain.getNativeHandle(), requestId, callback);
});
_swapChains.remove(swapChain);
_logger.info("Destroyed swapchain");
@@ -575,7 +575,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
final view = _swapChains[swapchain]!.first;
await withBoolCallback((cb) {
Renderer_beginFrameRenderThread(
renderer, swapchain.swapChain, 0.toBigInt, cb);
renderer, swapchain.getNativeHandle(), 0.toBigInt, cb);
});
await withVoidCallback((requestId, cb) {
Renderer_renderRenderThread(
@@ -622,6 +622,13 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
await updateRenderOrder();
}
///
///
///
Future<Iterable<SwapChain>> getSwapChains() async {
return _swapChains.keys;
}
final _hooks = <Future Function()>[];
///
@@ -756,7 +763,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
final beginFrame = await withBoolCallback((cb) {
Renderer_beginFrameRenderThread(
renderer, swapChain!.swapChain, 0.toBigInt, cb);
renderer, swapChain!.getNativeHandle(), 0.toBigInt, cb);
});
final pixelBuffers = <(View, Uint8List)>[];
@@ -838,7 +845,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
// synchronous, so we'll add a ~2 frame delay to wait for this to be available.
if (FILAMENT_SINGLE_THREADED) {
await withBoolCallback((cb) => Renderer_beginFrameRenderThread(
renderer, swapChain!.swapChain, 0.toBigInt, cb));
renderer, swapChain!.getNativeHandle(), 0.toBigInt, cb));
for (final view in views) {
await withVoidCallback((requestId, cb) {
Renderer_renderRenderThread(

View File

@@ -1,7 +1,11 @@
import 'package:thermion_dart/thermion_dart.dart';
class FFISwapChain extends SwapChain {
final Pointer<TSwapChain> swapChain;
class FFISwapChain extends SwapChain<Pointer> {
final Pointer<TSwapChain> pointer;
FFISwapChain(this.swapChain);
T getNativeHandle<T>() => pointer as T;
FFISwapChain(this.pointer);
}

View File

@@ -0,0 +1,11 @@
/// A top-level interface for any object that need to be passed
/// across "native" boundaries (either via FFI or Javascript interop).
///
/// Currently, [T] must always be a [Pointer] (which is defined and implemented
/// differently depending on the target platform). However, T is unbounded so
/// this is never checked at compile-time (so getNativeHandle<Matrix4>() is
/// not a compile-time error).
///
abstract class NativeHandle<T> {
T getNativeHandle<T>();
}

View File

@@ -1,3 +1,5 @@
abstract class SwapChain {
import 'native_handle.dart';
abstract class SwapChain<T> extends NativeHandle<T> {
}