From e8b4b7806b3e4323d18b7bd89581ecce7f496cf2 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Wed, 13 Nov 2024 10:51:19 +0800 Subject: [PATCH] feat!: rename thermion_flutter_ffi package to thermion_flutter_method_channel --- .../lib/src/platform_texture.dart | 123 ---------------- .../lib/src/texture_cache_entry.dart | 11 -- .../lib/src/thermion_flutter_windows.dart | 135 ------------------ .../CHANGELOG.md | 0 .../LICENSE | 0 .../README.md | 0 ...rmion_flutter_method_channel_platform.dart | 61 ++++---- .../lib/thermion_flutter_method_channel.dart} | 1 - .../pubspec.yaml | 4 +- 9 files changed, 39 insertions(+), 296 deletions(-) delete mode 100644 thermion_flutter/thermion_flutter_ffi/lib/src/platform_texture.dart delete mode 100644 thermion_flutter/thermion_flutter_ffi/lib/src/texture_cache_entry.dart delete mode 100644 thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_windows.dart rename thermion_flutter/{thermion_flutter_ffi => thermion_flutter_method_channel}/CHANGELOG.md (100%) rename thermion_flutter/{thermion_flutter_ffi => thermion_flutter_method_channel}/LICENSE (100%) rename thermion_flutter/{thermion_flutter_ffi => thermion_flutter_method_channel}/README.md (100%) rename thermion_flutter/{thermion_flutter_ffi => thermion_flutter_method_channel}/lib/src/thermion_flutter_method_channel_platform.dart (77%) rename thermion_flutter/{thermion_flutter_ffi/lib/thermion_flutter_ffi.dart => thermion_flutter_method_channel/lib/thermion_flutter_method_channel.dart} (61%) rename thermion_flutter/{thermion_flutter_ffi => thermion_flutter_method_channel}/pubspec.yaml (87%) diff --git a/thermion_flutter/thermion_flutter_ffi/lib/src/platform_texture.dart b/thermion_flutter/thermion_flutter_ffi/lib/src/platform_texture.dart deleted file mode 100644 index 42c94a5f..00000000 --- a/thermion_flutter/thermion_flutter_ffi/lib/src/platform_texture.dart +++ /dev/null @@ -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 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 _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); -// } -// } diff --git a/thermion_flutter/thermion_flutter_ffi/lib/src/texture_cache_entry.dart b/thermion_flutter/thermion_flutter_ffi/lib/src/texture_cache_entry.dart deleted file mode 100644 index 64c88d8f..00000000 --- a/thermion_flutter/thermion_flutter_ffi/lib/src/texture_cache_entry.dart +++ /dev/null @@ -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(); -} \ No newline at end of file diff --git a/thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_windows.dart b/thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_windows.dart deleted file mode 100644 index b7ee6819..00000000 --- a/thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_windows.dart +++ /dev/null @@ -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 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 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 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; -// } - - -// } \ No newline at end of file diff --git a/thermion_flutter/thermion_flutter_ffi/CHANGELOG.md b/thermion_flutter/thermion_flutter_method_channel/CHANGELOG.md similarity index 100% rename from thermion_flutter/thermion_flutter_ffi/CHANGELOG.md rename to thermion_flutter/thermion_flutter_method_channel/CHANGELOG.md diff --git a/thermion_flutter/thermion_flutter_ffi/LICENSE b/thermion_flutter/thermion_flutter_method_channel/LICENSE similarity index 100% rename from thermion_flutter/thermion_flutter_ffi/LICENSE rename to thermion_flutter/thermion_flutter_method_channel/LICENSE diff --git a/thermion_flutter/thermion_flutter_ffi/README.md b/thermion_flutter/thermion_flutter_method_channel/README.md similarity index 100% rename from thermion_flutter/thermion_flutter_ffi/README.md rename to thermion_flutter/thermion_flutter_method_channel/README.md diff --git a/thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_method_channel_platform.dart b/thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart similarity index 77% rename from thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_method_channel_platform.dart rename to thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart index 22ba8a7a..bf018f05 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/src/thermion_flutter_method_channel_platform.dart +++ b/thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart @@ -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 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 createTexture( + Future 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 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 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 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(); } diff --git a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart b/thermion_flutter/thermion_flutter_method_channel/lib/thermion_flutter_method_channel.dart similarity index 61% rename from thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart rename to thermion_flutter/thermion_flutter_method_channel/lib/thermion_flutter_method_channel.dart index f4aa33a1..4d96ea44 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart +++ b/thermion_flutter/thermion_flutter_method_channel/lib/thermion_flutter_method_channel.dart @@ -1,3 +1,2 @@ library; -export 'src/thermion_flutter_windows.dart'; export 'src/thermion_flutter_method_channel_platform.dart'; diff --git a/thermion_flutter/thermion_flutter_ffi/pubspec.yaml b/thermion_flutter/thermion_flutter_method_channel/pubspec.yaml similarity index 87% rename from thermion_flutter/thermion_flutter_ffi/pubspec.yaml rename to thermion_flutter/thermion_flutter_method_channel/pubspec.yaml index 4eb550c6..6c165bfc 100644 --- a/thermion_flutter/thermion_flutter_ffi/pubspec.yaml +++ b/thermion_flutter/thermion_flutter_method_channel/pubspec.yaml @@ -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