refactor: rename ThermionFlutterTexture->PlatformTextureDescriptor

This commit is contained in:
Nick Fisher
2024-12-17 20:23:51 +08:00
parent 342264eba9
commit dc690bb93a
5 changed files with 49 additions and 66 deletions

View File

@@ -40,7 +40,8 @@ class ThermionTextureWidget extends StatefulWidget {
} }
class _ThermionTextureWidgetState extends State<ThermionTextureWidget> { class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
ThermionFlutterTexture? _texture;
PlatformTextureDescriptor? _texture;
static final _views = <t.View>[]; static final _views = <t.View>[];
@@ -51,7 +52,7 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
super.dispose(); super.dispose();
_views.remove(widget.view); _views.remove(widget.view);
if(_texture != null) { if(_texture != null) {
ThermionFlutterPlatform.instance.destroyTexture(_texture!); ThermionFlutterPlatform.instance.destroyTextureDescriptor(_texture!);
} }
_states.remove(this); _states.remove(this);
@@ -111,7 +112,7 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
setState(() {}); setState(() {});
} }
if(texture != null) { if(texture != null) {
ThermionFlutterPlatform.instance.destroyTexture(texture); ThermionFlutterPlatform.instance.destroyTextureDescriptor(texture);
} }
_views.clear(); _views.clear();
@@ -228,8 +229,8 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
child: Stack(children: [ child: Stack(children: [
Positioned.fill( Positioned.fill(
child: Texture( child: Texture(
key: ObjectKey("flutter_texture_${_texture!.flutterId}"), key: ObjectKey("flutter_texture_${_texture!.flutterTextureId}"),
textureId: _texture!.flutterId, textureId: _texture!.flutterTextureId,
filterQuality: FilterQuality.none, filterQuality: FilterQuality.none,
freeze: false, freeze: false,
)) ))

View File

@@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:ffi'; import 'dart:ffi';
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:thermion_dart/thermion_dart.dart'; import 'package:thermion_dart/thermion_dart.dart';
import 'package:thermion_dart/thermion_dart.dart' as t; import 'package:thermion_dart/thermion_dart.dart' as t;
@@ -84,7 +85,7 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
return viewer!; return viewer!;
} }
Future<PlatformTextureDescriptor?> createTexture( Future<PlatformTextureDescriptor> createTextureDescriptor(
int width, int height) async { int width, int height) async {
var result = var result =
await channel.invokeMethod("createTexture", [width, height, 0, 0]); await channel.invokeMethod("createTexture", [width, height, 0, 0]);
@@ -95,33 +96,22 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
final hardwareId = result[1] as int; final hardwareId = result[1] as int;
var window = result[2] as int?; // usually 0 for nullptr var window = result[2] as int?; // usually 0 for nullptr
return ( return PlatformTextureDescriptor(flutterId, hardwareId, window, width, height);
flutterTextureId: flutterId,
hardwareId: hardwareId, }
windowHandle: window
); @override
Future destroyTextureDescriptor(PlatformTextureDescriptor descriptor) async {
await channel.invokeMethod("destroyTexture", descriptor.flutterTextureId);
} }
/// ///
/// ///
/// ///
Future<ThermionFlutterTexture?> createTextureAndBindToView( Future<PlatformTextureDescriptor?> createTextureAndBindToView(
t.View view, int width, int height) async { t.View view, int width, int height) async {
var result = var descriptor = await createTextureDescriptor(width, height);
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);
if (Platform.isWindows) { if (Platform.isWindows) {
if (_swapChain != null) { if (_swapChain != null) {
@@ -130,43 +120,38 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
} }
_swapChain = _swapChain =
await viewer!.createHeadlessSwapChain(texture.width, texture.height); await viewer!.createHeadlessSwapChain(descriptor.width, descriptor.height);
} else if (Platform.isAndroid) { } else if (Platform.isAndroid) {
if (_swapChain != null) { if (_swapChain != null) {
await view.setRenderable(false, _swapChain!); await view.setRenderable(false, _swapChain!);
await viewer!.destroySwapChain(_swapChain!); await viewer!.destroySwapChain(_swapChain!);
} }
_swapChain = await viewer!.createSwapChain(texture.window); _swapChain = await viewer!.createSwapChain(descriptor.windowHandle!);
} else { } else {
var renderTarget = await viewer!.createRenderTarget( var renderTarget = await viewer!.createRenderTarget(
texture.width, texture.height, texture.hardwareId); descriptor.width, descriptor.height, descriptor.hardwareId);
await view.setRenderTarget(renderTarget!); await view.setRenderTarget(renderTarget!);
} }
await view.setRenderable(true, _swapChain!); await view.setRenderable(true, _swapChain!);
return texture; return descriptor;
} }
@override @override
Future destroyTexture(ThermionFlutterTexture texture) async { Future markTextureFrameAvailable(PlatformTextureDescriptor texture) async {
await channel.invokeMethod("destroyTexture", texture.flutterId); await channel.invokeMethod("markTextureFrameAvailable", texture.flutterTextureId);
} }
@override @override
Future markTextureFrameAvailable(ThermionFlutterTexture texture) async { Future<PlatformTextureDescriptor> resizeTexture(PlatformTextureDescriptor texture,
await channel.invokeMethod("markTextureFrameAvailable", texture.flutterId);
}
@override
Future<ThermionFlutterTexture> resizeTexture(ThermionFlutterTexture texture,
t.View view, int width, int height) async { t.View view, int width, int height) async {
var newTexture = await createTextureAndBindToView(view, width, height); var newTexture = await createTextureAndBindToView(view, width, height);
if (newTexture == null) { if (newTexture == null) {
throw Exception(); throw Exception();
} }
await destroyTexture(texture); await destroyTextureDescriptor(texture);
return newTexture; return newTexture;
} }

View File

@@ -12,8 +12,7 @@ class ThermionFlutterOptions {
const ThermionFlutterOptions.empty() : uberarchivePath = null; const ThermionFlutterOptions.empty() : uberarchivePath = null;
} }
typedef PlatformTextureDescriptor = (
{int flutterTextureId, int hardwareId, int? windowHandle}) ;
abstract class ThermionFlutterPlatform extends PlatformInterface { abstract class ThermionFlutterPlatform extends PlatformInterface {
ThermionFlutterPlatform() : super(token: _token); ThermionFlutterPlatform() : super(token: _token);
@@ -40,7 +39,12 @@ abstract class ThermionFlutterPlatform extends PlatformInterface {
/// This is internal; unless you are [thermion_*] package developer, don't /// This is internal; unless you are [thermion_*] package developer, don't
/// call this yourself. May not be supported on all platforms. /// call this yourself. May not be supported on all platforms.
/// ///
Future<PlatformTextureDescriptor?> createTexture(int width, int height); Future<PlatformTextureDescriptor> createTextureDescriptor(int width, int height);
///
/// Destroys a raw rendering surface.
///
Future destroyTextureDescriptor(PlatformTextureDescriptor descriptor);
/// ///
/// Create a rendering surface and binds to the given [View] /// 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 /// This is internal; unless you are [thermion_*] package developer, don't
/// call this yourself. May not be supported on all platforms. /// call this yourself. May not be supported on all platforms.
/// ///
Future<ThermionFlutterTexture?> createTextureAndBindToView( Future<PlatformTextureDescriptor?> createTextureAndBindToView(
t.View view, int width, int height); t.View view, int width, int height);
/// ///
/// ///
/// ///
/// ///
Future<ThermionFlutterTexture?> resizeTexture( Future<PlatformTextureDescriptor?> resizeTexture(
ThermionFlutterTexture texture, t.View view, int width, int height); PlatformTextureDescriptor texture, t.View view, int width, int height);
/// ///
/// ///
/// ///
Future destroyTexture(ThermionFlutterTexture texture); Future markTextureFrameAvailable(PlatformTextureDescriptor texture);
///
///
///
Future markTextureFrameAvailable(ThermionFlutterTexture texture);
} }

View File

@@ -1,13 +1,10 @@
class ThermionFlutterTexture { class PlatformTextureDescriptor {
final int flutterTextureId;
final int hardwareId;
final int? windowHandle;
final int width; final int width;
final int height; final int height;
final int flutterId; PlatformTextureDescriptor(this.flutterTextureId, this.hardwareId, this.windowHandle, this.width, this.height);
final int hardwareId;
final int window;
ThermionFlutterTexture({required this.width, required this.height, required this.flutterId, required this.hardwareId, required this.window});
} }

View File

@@ -14,7 +14,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform {
} }
@override @override
Future<ThermionFlutterTexture?> createTexture(double width, double height, Future<PlatformTextureDescriptor?> createTexture(double width, double height,
double offsetLeft, double offsetTop, double pixelRatio) async { double offsetLeft, double offsetTop, double pixelRatio) async {
await _viewer!.destroySwapChain(); await _viewer!.destroySwapChain();
await _viewer!.createSwapChain(width.ceil(), height.ceil()); await _viewer!.createSwapChain(width.ceil(), height.ceil());
@@ -33,16 +33,16 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform {
_viewer! _viewer!
.updateViewportAndCameraProjection(width.ceil(), height.ceil(), 1.0); .updateViewportAndCameraProjection(width.ceil(), height.ceil(), 1.0);
return ThermionFlutterTexture(null, null, 0, 0, null); return PlatformTextureDescriptor(null, null, 0, 0, null);
} }
@override @override
Future destroyTexture(ThermionFlutterTexture texture) async { Future destroyTexture(PlatformTextureDescriptor texture) async {
// noop // noop
} }
@override @override
Future<ThermionFlutterTexture?> resizeTexture(ThermionFlutterTexture texture, Future<PlatformTextureDescriptor?> resizeTexture(PlatformTextureDescriptor texture,
int width, int height, int offsetLeft, int offsetTop, double pixelRatio) async { int width, int height, int offsetLeft, int offsetTop, double pixelRatio) async {
final canvas = document.getElementById("canvas") as HTMLCanvasElement; final canvas = document.getElementById("canvas") as HTMLCanvasElement;
canvas.width = width; canvas.width = width;
@@ -54,7 +54,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform {
(canvas as HTMLElement).style.top = (canvas as HTMLElement).style.top =
(offsetTop * pixelRatio).ceil().toString(); (offsetTop * pixelRatio).ceil().toString();
_viewer!.updateViewportAndCameraProjection(width, height, 1.0); _viewer!.updateViewportAndCameraProjection(width, height, 1.0);
return ThermionFlutterTexture(null, null, 0, 0, null); return PlatformTextureDescriptor(null, null, 0, 0, null);
} }
Future<ThermionViewer> createViewerWithOptions( Future<ThermionViewer> createViewerWithOptions(