From dc690bb93a642b372bccef8be9f5b567149072fd Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Tue, 17 Dec 2024 20:23:51 +0800 Subject: [PATCH] refactor: rename ThermionFlutterTexture->PlatformTextureDescriptor --- .../widgets/src/thermion_texture_widget.dart | 11 ++-- ...rmion_flutter_method_channel_platform.dart | 55 +++++++------------ .../thermion_flutter_platform_interface.dart | 24 ++++---- .../lib/thermion_flutter_texture.dart | 15 ++--- .../lib/thermion_flutter_web.dart | 10 ++-- 5 files changed, 49 insertions(+), 66 deletions(-) diff --git a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_texture_widget.dart b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_texture_widget.dart index cd7c6b83..1a1535d9 100644 --- a/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_texture_widget.dart +++ b/thermion_flutter/thermion_flutter/lib/src/widgets/src/thermion_texture_widget.dart @@ -40,7 +40,8 @@ class ThermionTextureWidget extends StatefulWidget { } class _ThermionTextureWidgetState extends State { - ThermionFlutterTexture? _texture; + + PlatformTextureDescriptor? _texture; static final _views = []; @@ -51,7 +52,7 @@ class _ThermionTextureWidgetState extends State { super.dispose(); _views.remove(widget.view); if(_texture != null) { - ThermionFlutterPlatform.instance.destroyTexture(_texture!); + ThermionFlutterPlatform.instance.destroyTextureDescriptor(_texture!); } _states.remove(this); @@ -111,7 +112,7 @@ class _ThermionTextureWidgetState extends State { setState(() {}); } if(texture != null) { - ThermionFlutterPlatform.instance.destroyTexture(texture); + ThermionFlutterPlatform.instance.destroyTextureDescriptor(texture); } _views.clear(); @@ -228,8 +229,8 @@ class _ThermionTextureWidgetState extends State { child: Stack(children: [ Positioned.fill( child: Texture( - key: ObjectKey("flutter_texture_${_texture!.flutterId}"), - textureId: _texture!.flutterId, + key: ObjectKey("flutter_texture_${_texture!.flutterTextureId}"), + textureId: _texture!.flutterTextureId, filterQuality: FilterQuality.none, freeze: false, )) diff --git a/thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart b/thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart index bf018f05..f0baf44e 100644 --- a/thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart +++ b/thermion_flutter/thermion_flutter_method_channel/lib/src/thermion_flutter_method_channel_platform.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:ffi'; import 'dart:async'; import 'dart:io'; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:thermion_dart/thermion_dart.dart'; import 'package:thermion_dart/thermion_dart.dart' as t; @@ -84,7 +85,7 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform { return viewer!; } - Future createTexture( + Future createTextureDescriptor( int width, int height) async { var result = await channel.invokeMethod("createTexture", [width, height, 0, 0]); @@ -95,33 +96,22 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform { final hardwareId = result[1] as int; var window = result[2] as int?; // usually 0 for nullptr - return ( - flutterTextureId: flutterId, - hardwareId: hardwareId, - windowHandle: window - ); + return PlatformTextureDescriptor(flutterId, hardwareId, window, width, height); + + } + + @override + Future destroyTextureDescriptor(PlatformTextureDescriptor descriptor) async { + await channel.invokeMethod("destroyTexture", descriptor.flutterTextureId); } /// /// /// - Future createTextureAndBindToView( + Future createTextureAndBindToView( t.View view, 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 - - var texture = ThermionFlutterTexture( - flutterId: flutterId, - hardwareId: hardwareId, - height: height, - width: width, - window: window ?? 0); + var descriptor = await createTextureDescriptor(width, height); + if (Platform.isWindows) { if (_swapChain != null) { @@ -130,43 +120,38 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform { } _swapChain = - await viewer!.createHeadlessSwapChain(texture.width, texture.height); + await viewer!.createHeadlessSwapChain(descriptor.width, descriptor.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(descriptor.windowHandle!); } else { var renderTarget = await viewer!.createRenderTarget( - texture.width, texture.height, texture.hardwareId); + descriptor.width, descriptor.height, descriptor.hardwareId); await view.setRenderTarget(renderTarget!); } await view.setRenderable(true, _swapChain!); - return texture; + return descriptor; } @override - Future destroyTexture(ThermionFlutterTexture texture) async { - await channel.invokeMethod("destroyTexture", texture.flutterId); + Future markTextureFrameAvailable(PlatformTextureDescriptor texture) async { + await channel.invokeMethod("markTextureFrameAvailable", texture.flutterTextureId); } @override - Future markTextureFrameAvailable(ThermionFlutterTexture texture) async { - await channel.invokeMethod("markTextureFrameAvailable", texture.flutterId); - } - - @override - Future resizeTexture(ThermionFlutterTexture texture, + Future resizeTexture(PlatformTextureDescriptor texture, t.View view, int width, int height) async { var newTexture = await createTextureAndBindToView(view, width, height); if (newTexture == null) { throw Exception(); } - await destroyTexture(texture); + await destroyTextureDescriptor(texture); return newTexture; } diff --git a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart index 09f246b0..32767ef7 100644 --- a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart +++ b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_platform_interface.dart @@ -12,8 +12,7 @@ class ThermionFlutterOptions { const ThermionFlutterOptions.empty() : uberarchivePath = null; } -typedef PlatformTextureDescriptor = ( - {int flutterTextureId, int hardwareId, int? windowHandle}) ; + abstract class ThermionFlutterPlatform extends PlatformInterface { ThermionFlutterPlatform() : super(token: _token); @@ -40,7 +39,12 @@ abstract class ThermionFlutterPlatform extends PlatformInterface { /// This is internal; unless you are [thermion_*] package developer, don't /// call this yourself. May not be supported on all platforms. /// - Future createTexture(int width, int height); + Future createTextureDescriptor(int width, int height); + + /// + /// Destroys a raw rendering surface. + /// + Future destroyTextureDescriptor(PlatformTextureDescriptor descriptor); /// /// Create a rendering surface and binds to the given [View] @@ -48,23 +52,19 @@ abstract class ThermionFlutterPlatform extends PlatformInterface { /// This is internal; unless you are [thermion_*] package developer, don't /// call this yourself. May not be supported on all platforms. /// - Future createTextureAndBindToView( + Future createTextureAndBindToView( t.View view, int width, int height); /// /// /// /// - Future resizeTexture( - ThermionFlutterTexture texture, t.View view, int width, int height); + Future resizeTexture( + PlatformTextureDescriptor texture, t.View view, int width, int height); + /// /// /// - Future destroyTexture(ThermionFlutterTexture texture); - - /// - /// - /// - Future markTextureFrameAvailable(ThermionFlutterTexture texture); + Future markTextureFrameAvailable(PlatformTextureDescriptor texture); } diff --git a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_texture.dart b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_texture.dart index 5120ece8..04d5dc5e 100644 --- a/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_texture.dart +++ b/thermion_flutter/thermion_flutter_platform_interface/lib/thermion_flutter_texture.dart @@ -1,13 +1,10 @@ -class ThermionFlutterTexture { - +class PlatformTextureDescriptor { + final int flutterTextureId; + final int hardwareId; + final int? windowHandle; final int width; final int height; - final int flutterId; - final int hardwareId; - - final int window; - - ThermionFlutterTexture({required this.width, required this.height, required this.flutterId, required this.hardwareId, required this.window}); - + PlatformTextureDescriptor(this.flutterTextureId, this.hardwareId, this.windowHandle, this.width, this.height); } + diff --git a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart index db762d80..28931a93 100644 --- a/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart +++ b/thermion_flutter/thermion_flutter_web/lib/thermion_flutter_web.dart @@ -14,7 +14,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { } @override - Future createTexture(double width, double height, + Future createTexture(double width, double height, double offsetLeft, double offsetTop, double pixelRatio) async { await _viewer!.destroySwapChain(); await _viewer!.createSwapChain(width.ceil(), height.ceil()); @@ -33,16 +33,16 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { _viewer! .updateViewportAndCameraProjection(width.ceil(), height.ceil(), 1.0); - return ThermionFlutterTexture(null, null, 0, 0, null); + return PlatformTextureDescriptor(null, null, 0, 0, null); } @override - Future destroyTexture(ThermionFlutterTexture texture) async { + Future destroyTexture(PlatformTextureDescriptor texture) async { // noop } @override - Future resizeTexture(ThermionFlutterTexture texture, + Future resizeTexture(PlatformTextureDescriptor texture, int width, int height, int offsetLeft, int offsetTop, double pixelRatio) async { final canvas = document.getElementById("canvas") as HTMLCanvasElement; canvas.width = width; @@ -54,7 +54,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform { (canvas as HTMLElement).style.top = (offsetTop * pixelRatio).ceil().toString(); _viewer!.updateViewportAndCameraProjection(width, height, 1.0); - return ThermionFlutterTexture(null, null, 0, 0, null); + return PlatformTextureDescriptor(null, null, 0, 0, null); } Future createViewerWithOptions(