feat!: rename thermion_flutter_ffi package to thermion_flutter_method_channel

This commit is contained in:
Nick Fisher
2024-11-13 10:51:19 +08:00
parent 133367669d
commit e8b4b7806b
9 changed files with 39 additions and 296 deletions

View File

@@ -1,123 +0,0 @@
// import 'package:logging/logging.dart';
// import 'package:thermion_dart/thermion_dart.dart';
// import 'thermion_flutter_method_channel_interface.dart';
// class FlutterPlatformTexture extends ThermionFlutterTexture {
// final _logger = Logger("ThermionFlutterTexture");
// final ThermionViewer viewer;
// final View view;
// int flutterId = -1;
// int _lastFlutterId = -1;
// int _lastHardwareId = -1;
// int hardwareId = -1;
// int width = -1;
// int height = -1;
// SwapChain? swapChain;
// RenderTarget? _renderTarget;
// late bool destroySwapChainOnResize;
// bool destroyed = false;
// FlutterPlatformTexture(
// super.channel, this.viewer, this.view, this.swapChain) {
// if (swapChain == null) {
// destroySwapChainOnResize = true;
// } else {
// destroySwapChainOnResize = false;
// }
// }
// @override
// Future<void> resize(
// int newWidth, int newHeight, int newLeft, int newTop) async {
// _logger.info(
// "Resizing texture to $newWidth x $newHeight (offset $newLeft, $newTop)");
// if (newWidth == this.width &&
// newHeight == this.height &&
// newLeft == 0 &&
// newTop == 0) {
// _logger.info("Existing texture matches requested dimensions");
// return;
// }
// this.width = newWidth;
// this.height = newHeight;
// var result =
// await channel.invokeMethod("createTexture", [width, height, 0, 0]);
// if (result == null || (result[0] == -1)) {
// throw Exception("Failed to create texture");
// }
// _lastFlutterId = flutterId;
// _lastHardwareId = hardwareId;
// flutterId = result[0] as int;
// hardwareId = result[1] as int;
// var window = result[2] as int; // usually 0 for nullptr
// _logger.info("Created texture ${flutterId} / ${hardwareId}");
// if (destroySwapChainOnResize) {
// if (swapChain != null) {
// await viewer.destroySwapChain(swapChain!);
// }
// swapChain = await viewer.createSwapChain(window);
// await view.setRenderable(true, swapChain!);
// } else if (hardwareId != _lastHardwareId) {
// if (_renderTarget != null) {
// await viewer.destroyRenderTarget(_renderTarget!);
// }
// _renderTarget =
// await viewer.createRenderTarget(width, height, hardwareId);
// await view.setRenderTarget(_renderTarget!);
// await view.setRenderable(true, swapChain!);
// if (_lastFlutterId != -1 && _lastHardwareId != -1) {
// await _destroyTexture(_lastFlutterId, _lastHardwareId);
// _lastFlutterId = -1;
// _lastHardwareId = -1;
// }
// }
// }
// Future<void> _destroyTexture(
// int flutterTextureId, int hardwareTextureId) async {
// try {
// await channel.invokeMethod(
// "destroyTexture", [flutterTextureId, hardwareTextureId]);
// _logger.info("Destroyed texture $flutterTextureId / $hardwareTextureId");
// } catch (e) {
// _logger.severe("Failed to destroy texture: $e");
// }
// }
// bool destroying = false;
// Future destroy() async {
// if (destroyed || destroying) {
// return;
// }
// destroying = true;
// await view.setRenderTarget(null);
// if (_renderTarget != null) {
// await viewer.destroyRenderTarget(_renderTarget!);
// _renderTarget = null;
// }
// if (destroySwapChainOnResize && swapChain != null) {
// await viewer.destroySwapChain(swapChain!);
// swapChain = null;
// }
// await _destroyTexture(flutterId, hardwareId);
// flutterId = -1;
// hardwareId = -1;
// destroying = false;
// destroyed = true;
// }
// Future markFrameAvailable() async {
// await channel.invokeMethod("markTextureFrameAvailable", this.flutterId);
// }
// }

View File

@@ -1,11 +0,0 @@
class TextureCacheEntry {
final int flutterId;
final int hardwareId;
final DateTime creationTime;
DateTime? removalTime;
bool inUse;
TextureCacheEntry(this.flutterId, this.hardwareId,
{this.removalTime, this.inUse = true})
: creationTime = DateTime.now();
}

View File

@@ -1,135 +0,0 @@
// import 'dart:async';
// import 'package:flutter/services.dart';
// import 'package:thermion_dart/thermion_dart.dart';
// import 'package:thermion_flutter_ffi/src/thermion_flutter_method_channel_interface.dart';
// import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
// import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
// import 'package:logging/logging.dart';
// import 'package:thermion_flutter_platform_interface/thermion_flutter_window.dart';
// import 'platform_texture.dart';
// ///
// /// A Windows-only implementation of [ThermionFlutterPlatform] that uses
// /// a Flutter platform channel to create a rendering context,
// /// resource loader and a native HWND that will be sit behind the running
// /// Flutter application.
// ///
// class ThermionFlutterWindows
// extends ThermionFlutterMethodChannelPlatform {
// final _channel = const MethodChannel("dev.thermion.flutter/event");
// final _logger = Logger("ThermionFlutterWindows");
// ThermionViewer? _viewer;
// SwapChain? _swapChain;
// ThermionFlutterWindows._() {}
// static void registerWith() {
// ThermionFlutterPlatform.instance = ThermionFlutterWindows._();
// }
// @override
// Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async {
// if(_viewer != null) {
// throw Exception("Only one viewer should be instantiated over the life of the app");
// }
// _viewer = await super.createViewer(options: options);
// _viewer!.onDispose(() async {
// _viewer = null;
// });
// return _viewer!;
// }
// ///
// /// Not supported on Windows. Throws an exception.
// ///
// @override
// Future<ThermionFlutterTexture?> createTexture(View view, int width, int height) async {
// var texture = FlutterPlatformTexture(channel, viewer!, view, null);
// await texture.resize(width, height, 0, 0);
// return texture;
// }
// @override
// Future<ThermionFlutterWindow> createWindow(int width, int height, int offsetLeft, int offsetTop) async {
// var result = await _channel
// .invokeMethod("createWindow", [width, height, offsetLeft, offsetLeft]);
// if (result == null || result[2] == -1) {
// throw Exception("Failed to create window");
// }
// var window =
// ThermionFlutterWindowImpl(result[2], _channel, viewer!);
// await window.resize(width, height, offsetLeft, offsetTop);
// var view = await _viewer!.getViewAt(0);
// await view.updateViewport(width, height);
// _swapChain = await _viewer!.createSwapChain(window.handle);
// await view.setRenderable(true, _swapChain!);
// return window;
// }
// }
// class ThermionFlutterWindowImpl extends ThermionFlutterWindow {
// final ThermionViewer viewer;
// final int handle;
// int height = 0;
// int width = 0;
// int offsetLeft = 0;
// int offsetTop = 0;
// final MethodChannel _channel;
// ThermionFlutterWindowImpl(this.handle, this._channel, this.viewer);
// @override
// Future destroy() async {
// await _channel
// .invokeMethod("destroyWindow", this.handle);
// }
// @override
// Future markFrameAvailable() {
// // TODO: implement markFrameAvailable
// throw UnimplementedError();
// }
// bool _resizing = false;
// ///
// /// Called by [ThermionWidget] to resize the window. Don't call this yourself.
// ///
// @override
// Future resize(
// int width, int height, int offsetLeft, int offsetTop) async {
// if (_resizing) {
// throw Exception("Resize underway");
// }
// if (width == this.width && height == this.height && this.offsetLeft == offsetLeft && this.offsetTop == offsetTop) {
// return;
// }
// this.width = width;
// this.height = height;
// this.offsetLeft = offsetLeft;
// this.offsetTop = offsetTop;
// _resizing = true;
// await _channel
// .invokeMethod("resizeWindow", [width, height, offsetLeft, offsetTop]);
// _resizing = false;
// }
// }

View File

@@ -5,7 +5,6 @@ import 'dart:io';
import 'package:flutter/services.dart';
import 'package:thermion_dart/thermion_dart.dart';
import 'package:thermion_dart/thermion_dart.dart' as t;
import 'package:thermion_flutter_ffi/src/thermion_flutter_method_channel_platform.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_interface.dart';
import 'package:logging/logging.dart';
import 'package:thermion_flutter_platform_interface/thermion_flutter_texture.dart';
@@ -31,7 +30,7 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
ThermionFlutterPlatform.instance = instance!;
}
ThermionViewerFFI? viewer;
t.ThermionViewerFFI? viewer;
Future<ThermionViewer> createViewer({ThermionFlutterOptions? options}) async {
if (viewer != null) {
@@ -74,8 +73,10 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
}
// this implementation renders directly into a texture/render target
// we still need to create a (headless) swapchain, but the actual dimensions
// don't matter
// for some reason we still need to create a (headless) swapchain, but the
// actual dimensions don't matter
// TODO - see if we can use `renderStandaloneView` in FilamentViewer to
// avoid this
if (Platform.isMacOS || Platform.isIOS) {
_swapChain = await viewer!.createHeadlessSwapChain(1, 1);
}
@@ -83,7 +84,28 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
return viewer!;
}
Future<ThermionFlutterTexture?> createTexture(
Future<PlatformTextureDescriptor?> createTexture(
int width, int height) async {
var result =
await channel.invokeMethod("createTexture", [width, height, 0, 0]);
if (result == null || (result[0] == -1)) {
throw Exception("Failed to create texture");
}
final flutterId = result[0] as int;
final hardwareId = result[1] as int;
var window = result[2] as int?; // usually 0 for nullptr
return (
flutterTextureId: flutterId,
hardwareId: hardwareId,
windowHandle: window
);
}
///
///
///
Future<ThermionFlutterTexture?> createTextureAndBindToView(
t.View view, int width, int height) async {
var result =
await channel.invokeMethod("createTexture", [width, height, 0, 0]);
@@ -103,20 +125,18 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
if (Platform.isWindows) {
if (_swapChain != null) {
await view!.setRenderable(false, _swapChain!);
await view.setRenderable(false, _swapChain!);
await viewer!.destroySwapChain(_swapChain!);
}
_swapChain = await viewer!
.createHeadlessSwapChain(texture.width, texture.height);
} else if(Platform.isAndroid) {
if (_swapChain != null) {
await view!.setRenderable(false, _swapChain!);
_swapChain =
await viewer!.createHeadlessSwapChain(texture.width, texture.height);
} else if (Platform.isAndroid) {
if (_swapChain != null) {
await view.setRenderable(false, _swapChain!);
await viewer!.destroySwapChain(_swapChain!);
}
_swapChain = await viewer!
.createSwapChain(texture.window);
}
_swapChain = await viewer!.createSwapChain(texture.window);
} else {
var renderTarget = await viewer!.createRenderTarget(
texture.width, texture.height, texture.hardwareId);
@@ -128,13 +148,6 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
return texture;
}
@override
Future<ThermionFlutterWindow> createWindow(
int width, int height, int offsetLeft, int offsetTop) {
// TODO: implement createWindow
throw UnimplementedError();
}
@override
Future destroyTexture(ThermionFlutterTexture texture) async {
await channel.invokeMethod("destroyTexture", texture.flutterId);
@@ -148,7 +161,7 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
@override
Future<ThermionFlutterTexture> resizeTexture(ThermionFlutterTexture texture,
t.View view, int width, int height) async {
var newTexture = await createTexture(view, width, height);
var newTexture = await createTextureAndBindToView(view, width, height);
if (newTexture == null) {
throw Exception();
}

View File

@@ -1,3 +1,2 @@
library;
export 'src/thermion_flutter_windows.dart';
export 'src/thermion_flutter_method_channel_platform.dart';

View File

@@ -1,5 +1,5 @@
name: thermion_flutter_ffi
description: An FFI implementation for thermion_flutter (i.e. all platforms except web).
name: thermion_flutter_method_channel
description: Desktop + mobile implementation for texture creation + registration with Flutter.
repository: https://github.com/nmfisher/thermion_flutter/thermion_flutter
version: 0.2.1-dev.19.0