From 4edc8aa85b4ae49fd4a6b8b50182c669a921201c Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 5 Sep 2024 17:51:22 +0800 Subject: [PATCH] feat: (flutter) move DPR calculation to resizeTexture and add createViewerWithOptions method to ThermionFlutterFFI --- .../lib/thermion_flutter_ffi.dart | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart index dce8d700..83f742e4 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart +++ b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart @@ -26,7 +26,12 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { final _textures = {}; - Future createViewer({String? uberArchivePath}) async { + Future createViewerWithOptions( + ThermionFlutterOptions options) async { + return createViewer(uberarchivePath: options.uberarchivePath); + } + + Future createViewer({String? uberarchivePath}) async { var resourceLoader = Pointer.fromAddress( await _channel.invokeMethod("getResourceLoaderWrapper")); @@ -58,7 +63,7 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { renderCallbackOwner: renderCallbackOwner, driver: driverPtr, sharedContext: sharedContextPtr, - uberArchivePath: uberArchivePath); + uberArchivePath: uberarchivePath); await _viewer!.initialized; return _viewer!; } @@ -115,7 +120,8 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { if (_textures.length > 1) { throw Exception("Multiple textures not yet supported"); } else if (_textures.length == 1) { - if (_textures.first.height == physicalHeight && _textures.first.width == physicalWidth) { + if (_textures.first.height == physicalHeight && + _textures.first.width == physicalWidth) { return _textures.first; } else { await _viewer!.setRendering(false); @@ -127,8 +133,8 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { _creatingTexture = true; - var result = await _channel - .invokeMethod("createTexture", [physicalWidth, physicalHeight, offsetLeft, offsetLeft]); + var result = await _channel.invokeMethod("createTexture", + [physicalWidth, physicalHeight, offsetLeft, offsetLeft]); if (result == null || (result[0] == -1)) { throw Exception("Failed to create texture"); @@ -140,12 +146,14 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { _logger.info( "Created texture with flutter texture id ${flutterTextureId}, hardwareTextureId $hardwareTextureId and surfaceAddress $surfaceAddress"); - _viewer?.viewportDimensions = (physicalWidth.toDouble(), physicalHeight.toDouble()); + _viewer?.viewportDimensions = + (physicalWidth.toDouble(), physicalHeight.toDouble()); - final texture = ThermionFlutterTexture( - flutterTextureId, hardwareTextureId, physicalWidth, physicalHeight, surfaceAddress); + final texture = ThermionFlutterTexture(flutterTextureId, hardwareTextureId, + physicalWidth, physicalHeight, surfaceAddress); - await _viewer?.createSwapChain(physicalWidth.toDouble(), physicalHeight.toDouble(), + await _viewer?.createSwapChain( + physicalWidth.toDouble(), physicalHeight.toDouble(), surface: texture.surfaceAddress == null ? nullptr : Pointer.fromAddress(texture.surfaceAddress!)); @@ -153,7 +161,9 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { if (texture.hardwareTextureId != null) { // ignore: unused_local_variable var renderTarget = await _viewer?.createRenderTarget( - physicalWidth.toDouble(), physicalHeight.toDouble(), texture.hardwareTextureId!); + physicalWidth.toDouble(), + physicalHeight.toDouble(), + texture.hardwareTextureId!); } await _viewer?.updateViewportAndCameraProjection( @@ -186,12 +196,20 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { /// Called by [ThermionWidget] to resize a texture. Don't call this yourself. /// @override - Future resizeTexture(ThermionFlutterTexture texture, - int width, int height, int offsetLeft, int offsetRight) async { + Future resizeTexture( + ThermionFlutterTexture texture, + int width, + int height, + int offsetLeft, + int offsetTop, + double pixelRatio) async { if (_resizing) { throw Exception("Resize underway"); } + width = (width * pixelRatio).ceil(); + height = (height * pixelRatio).ceil(); + if ((width - _viewer!.viewportDimensions.$1).abs() < 0.001 || (height - _viewer!.viewportDimensions.$2).abs() < 0.001) { return texture;