From f1a2926bdf692c64d758d1f49c23baa3ff21e9bc Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Sat, 24 Aug 2024 16:24:06 +0800 Subject: [PATCH] fix!: (flutter) pass pixelRatio to createTexture --- .../lib/thermion_flutter_ffi.dart | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 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 805fe622..dce8d700 100644 --- a/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart +++ b/thermion_flutter/thermion_flutter_ffi/lib/thermion_flutter_ffi.dart @@ -98,8 +98,10 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { /// The current design doesn't accommodate this (for example, it seems we can /// only create a single native window from a Surface at any one time). /// - Future createTexture( - int width, int height, int offsetLeft, int offsetRight) async { + Future createTexture(double width, double height, + double offsetLeft, double offsetRight, double pixelRatio) async { + final physicalWidth = (width * pixelRatio).ceil(); + final physicalHeight = (height * pixelRatio).ceil(); // when a ThermionWidget is inserted, disposed then immediately reinserted // into the widget hierarchy (e.g. rebuilding due to setState(() {}) being called in an ancestor widget) // the first call to createTexture may not have completed before the second. @@ -113,7 +115,7 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { if (_textures.length > 1) { throw Exception("Multiple textures not yet supported"); } else if (_textures.length == 1) { - if (_textures.first.height == height && _textures.first.width == width) { + if (_textures.first.height == physicalHeight && _textures.first.width == physicalWidth) { return _textures.first; } else { await _viewer!.setRendering(false); @@ -126,7 +128,7 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { _creatingTexture = true; var result = await _channel - .invokeMethod("createTexture", [width, height, offsetLeft, offsetLeft]); + .invokeMethod("createTexture", [physicalWidth, physicalHeight, offsetLeft, offsetLeft]); if (result == null || (result[0] == -1)) { throw Exception("Failed to create texture"); @@ -138,12 +140,12 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { _logger.info( "Created texture with flutter texture id ${flutterTextureId}, hardwareTextureId $hardwareTextureId and surfaceAddress $surfaceAddress"); - _viewer?.viewportDimensions = (width.toDouble(), height.toDouble()); + _viewer?.viewportDimensions = (physicalWidth.toDouble(), physicalHeight.toDouble()); final texture = ThermionFlutterTexture( - flutterTextureId, hardwareTextureId, width, height, surfaceAddress); + flutterTextureId, hardwareTextureId, physicalWidth, physicalHeight, surfaceAddress); - await _viewer?.createSwapChain(width.toDouble(), height.toDouble(), + await _viewer?.createSwapChain(physicalWidth.toDouble(), physicalHeight.toDouble(), surface: texture.surfaceAddress == null ? nullptr : Pointer.fromAddress(texture.surfaceAddress!)); @@ -151,11 +153,11 @@ class ThermionFlutterFFI extends ThermionFlutterPlatform { if (texture.hardwareTextureId != null) { // ignore: unused_local_variable var renderTarget = await _viewer?.createRenderTarget( - width.toDouble(), height.toDouble(), texture.hardwareTextureId!); + physicalWidth.toDouble(), physicalHeight.toDouble(), texture.hardwareTextureId!); } await _viewer?.updateViewportAndCameraProjection( - width.toDouble(), height.toDouble()); + physicalWidth.toDouble(), physicalHeight.toDouble()); _viewer?.render(); _creatingTexture = false;