fix resizing

This commit is contained in:
Nick Fisher
2023-10-13 11:24:29 +08:00
parent 72ec72660d
commit 7f107708b5

View File

@@ -53,11 +53,14 @@ class FilamentControllerFFI extends FilamentController {
_lib = NativeLibrary(dl); _lib = NativeLibrary(dl);
} }
bool _rendering = false;
@override @override
Future setRendering(bool render) async { Future setRendering(bool render) async {
if (_viewer == null || _resizing) { if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring"); throw Exception("No viewer available, ignoring");
} }
_rendering = render;
_lib.set_rendering_ffi(_viewer!, render); _lib.set_rendering_ffi(_viewer!, render);
} }
@@ -102,9 +105,14 @@ class FilamentControllerFFI extends FilamentController {
@override @override
Future destroyTexture() async { Future destroyTexture() async {
await _channel.invokeMethod("destroyTexture"); print("Destroying texture");
// we need to flush all references to the previous texture ID before calling destroy, otherwise the Texture widget will attempt to render a non-existent texture and crash.
// however, this is not a synchronous stream, so we need to ensure the Texture widget has been removed from the hierarchy before destroying
_textureId = null; _textureId = null;
_textureIdController.add(null); _textureIdController.add(null);
await _channel.invokeMethod("destroyTexture");
print("Texture destroyed");
} }
/// ///
@@ -202,17 +210,28 @@ class FilamentControllerFFI extends FilamentController {
print("No texture created, ignoring call to resize."); print("No texture created, ignoring call to resize.");
return; return;
} }
_resizing = true;
setRendering(false); if (_resizing) {
if (_viewer != null) { print("Resize currently underway, ignoring");
_lib.destroy_swap_chain(_viewer!); return;
} }
bool wasRendering = _rendering;
if (_viewer != null && _rendering) {
await setRendering(false);
}
_resizing = true;
if (_viewer != null) {
_lib.destroy_swap_chain_ffi(_viewer!);
}
await destroyTexture(); await destroyTexture();
size = ui.Size(width * _pixelRatio, height * _pixelRatio); size = ui.Size(width * _pixelRatio, height * _pixelRatio);
var textures = var textures =
await _channel.invokeMethod("createTexture", [size.width, size.height]); await _channel.invokeMethod("createTexture", [size.width, size.height]);
print("Created new texture");
var flutterTextureId = textures[0]; var flutterTextureId = textures[0];
_textureId = flutterTextureId; _textureId = flutterTextureId;
@@ -239,7 +258,9 @@ class FilamentControllerFFI extends FilamentController {
_textureIdController.add(_textureId); _textureIdController.add(_textureId);
_resizing = false; _resizing = false;
setRendering(true); if (wasRendering) {
setRendering(true);
}
} }
@override @override