(flutter) add zero size check for texture descriptors

This commit is contained in:
Nick Fisher
2025-06-02 12:58:11 +08:00
parent 2191f9d44b
commit 3fd15ab91b

View File

@@ -87,11 +87,12 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
} }
final config = FFIFilamentConfig( final config = FFIFilamentConfig(
backend: backend, backend: backend,
loadResource: loadAsset, loadResource: loadAsset,
platform: platformPtr, platform: platformPtr,
sharedContext: sharedContextPtr, sharedContext: sharedContextPtr,
uberArchivePath: options.uberarchivePath); uberArchivePath: options.uberarchivePath,
);
if (FilamentApp.instance == null) { if (FilamentApp.instance == null) {
await FFIFilamentApp.create(config: config); await FFIFilamentApp.create(config: config);
@@ -122,9 +123,20 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
} }
Future<PlatformTextureDescriptor> createTextureDescriptor( Future<PlatformTextureDescriptor> createTextureDescriptor(
int width, int height) async { int width,
var result = int height,
await channel.invokeMethod("createTexture", [width, height, 0, 0]); ) async {
if (width == 0 || height == 0) {
throw Exception(
"Invalid dimensions for texture descriptor : ${width}x${height}",
);
}
var result = await channel.invokeMethod("createTexture", [
width,
height,
0,
0,
]);
if (result == null || (result[0] == -1)) { if (result == null || (result[0] == -1)) {
throw Exception("Failed to create texture"); throw Exception("Failed to create texture");
} }
@@ -133,7 +145,12 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
var window = result[2] as int?; // usually 0 for nullptr var window = result[2] as int?; // usually 0 for nullptr
return PlatformTextureDescriptor( return PlatformTextureDescriptor(
flutterId, hardwareId, window, width, height); flutterId,
hardwareId,
window,
width,
height,
);
} }
@override @override
@@ -145,7 +162,10 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
/// ///
/// ///
Future<PlatformTextureDescriptor?> createTextureAndBindToView( Future<PlatformTextureDescriptor?> createTextureAndBindToView(
View view, int width, int height) async { View view,
int width,
int height,
) async {
var descriptor = await createTextureDescriptor(width, height); var descriptor = await createTextureDescriptor(width, height);
if (Platform.isWindows) { if (Platform.isWindows) {
@@ -154,11 +174,14 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
await FilamentApp.instance!.destroySwapChain(_swapChain!); await FilamentApp.instance!.destroySwapChain(_swapChain!);
} }
_swapChain = await FilamentApp.instance! _swapChain = await FilamentApp.instance!.createHeadlessSwapChain(
.createHeadlessSwapChain(descriptor.width, descriptor.height); descriptor.width,
descriptor.height,
);
_logger.info( _logger.info(
"Created headless swapchain ${descriptor.width}x${descriptor.height}"); "Created headless swapchain ${descriptor.width}x${descriptor.height}",
);
await FilamentApp.instance!.register(_swapChain!, view); await FilamentApp.instance!.register(_swapChain!, view);
} else if (Platform.isAndroid) { } else if (Platform.isAndroid) {
@@ -166,33 +189,41 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
await FilamentApp.instance!.unregister(_swapChain!, view); await FilamentApp.instance!.unregister(_swapChain!, view);
await FilamentApp.instance!.destroySwapChain(_swapChain!); await FilamentApp.instance!.destroySwapChain(_swapChain!);
} }
_swapChain = await FilamentApp.instance! _swapChain = await FilamentApp.instance!.createSwapChain(
.createSwapChain(Pointer<Void>.fromAddress(descriptor.windowHandle!)); Pointer<Void>.fromAddress(descriptor.windowHandle!),
);
await FilamentApp.instance!.register(_swapChain!, view); await FilamentApp.instance!.register(_swapChain!, view);
} else { } else {
final color = await FilamentApp.instance! final color = await FilamentApp.instance!.createTexture(
.createTexture(descriptor.width, descriptor.height, descriptor.width,
importedTextureHandle: descriptor.hardwareId, descriptor.height,
flags: { importedTextureHandle: descriptor.hardwareId,
TextureUsage.TEXTURE_USAGE_BLIT_SRC, flags: {
TextureUsage.TEXTURE_USAGE_COLOR_ATTACHMENT, TextureUsage.TEXTURE_USAGE_BLIT_SRC,
TextureUsage.TEXTURE_USAGE_SAMPLEABLE TextureUsage.TEXTURE_USAGE_COLOR_ATTACHMENT,
}, TextureUsage.TEXTURE_USAGE_SAMPLEABLE,
textureFormat: TextureFormat.RGBA32F, },
textureSamplerType: TextureSamplerType.SAMPLER_2D); textureFormat: TextureFormat.RGBA32F,
final depth = await FilamentApp.instance! textureSamplerType: TextureSamplerType.SAMPLER_2D,
.createTexture(descriptor.width, descriptor.height, );
flags: { final depth = await FilamentApp.instance!.createTexture(
TextureUsage.TEXTURE_USAGE_BLIT_SRC, descriptor.width,
TextureUsage.TEXTURE_USAGE_DEPTH_ATTACHMENT, descriptor.height,
TextureUsage.TEXTURE_USAGE_SAMPLEABLE, flags: {
}, TextureUsage.TEXTURE_USAGE_BLIT_SRC,
textureFormat: TextureFormat.DEPTH32F, TextureUsage.TEXTURE_USAGE_DEPTH_ATTACHMENT,
textureSamplerType: TextureSamplerType.SAMPLER_2D); TextureUsage.TEXTURE_USAGE_SAMPLEABLE,
},
textureFormat: TextureFormat.DEPTH32F,
textureSamplerType: TextureSamplerType.SAMPLER_2D,
);
var renderTarget = await FilamentApp.instance!.createRenderTarget( var renderTarget = await FilamentApp.instance!.createRenderTarget(
descriptor.width, descriptor.height, descriptor.width,
color: color, depth: depth); descriptor.height,
color: color,
depth: depth,
);
await view.setRenderTarget(renderTarget); await view.setRenderTarget(renderTarget);
} }
@@ -204,16 +235,19 @@ class ThermionFlutterMethodChannelPlatform extends ThermionFlutterPlatform {
Future markTextureFrameAvailable(PlatformTextureDescriptor texture) async { Future markTextureFrameAvailable(PlatformTextureDescriptor texture) async {
if (!Platform.isAndroid) { if (!Platform.isAndroid) {
await channel.invokeMethod( await channel.invokeMethod(
"markTextureFrameAvailable", texture.flutterTextureId); "markTextureFrameAvailable",
texture.flutterTextureId,
);
} }
} }
@override @override
Future<PlatformTextureDescriptor> resizeTexture( Future<PlatformTextureDescriptor> resizeTexture(
PlatformTextureDescriptor texture, PlatformTextureDescriptor texture,
View view, View view,
int width, int width,
int height) async { 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();