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> {
ThermionFlutterTexture? _texture;
PlatformTextureDescriptor? _texture;
static final _views = <t.View>[];
@@ -51,7 +52,7 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
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<ThermionTextureWidget> {
setState(() {});
}
if(texture != null) {
ThermionFlutterPlatform.instance.destroyTexture(texture);
ThermionFlutterPlatform.instance.destroyTextureDescriptor(texture);
}
_views.clear();
@@ -228,8 +229,8 @@ class _ThermionTextureWidgetState extends State<ThermionTextureWidget> {
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,
))

View File

@@ -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<PlatformTextureDescriptor?> createTexture(
Future<PlatformTextureDescriptor> 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<ThermionFlutterTexture?> createTextureAndBindToView(
Future<PlatformTextureDescriptor?> 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<ThermionFlutterTexture> resizeTexture(ThermionFlutterTexture texture,
Future<PlatformTextureDescriptor> 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;
}

View File

@@ -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<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]
@@ -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<ThermionFlutterTexture?> createTextureAndBindToView(
Future<PlatformTextureDescriptor?> createTextureAndBindToView(
t.View view, int width, int height);
///
///
///
///
Future<ThermionFlutterTexture?> resizeTexture(
ThermionFlutterTexture texture, t.View view, int width, int height);
Future<PlatformTextureDescriptor?> resizeTexture(
PlatformTextureDescriptor texture, t.View view, int width, int height);
///
///
///
Future destroyTexture(ThermionFlutterTexture texture);
///
///
///
Future markTextureFrameAvailable(ThermionFlutterTexture texture);
Future markTextureFrameAvailable(PlatformTextureDescriptor 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 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);
}

View File

@@ -14,7 +14,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform {
}
@override
Future<ThermionFlutterTexture?> createTexture(double width, double height,
Future<PlatformTextureDescriptor?> 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<ThermionFlutterTexture?> resizeTexture(ThermionFlutterTexture texture,
Future<PlatformTextureDescriptor?> 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<ThermionViewer> createViewerWithOptions(