feat: adapt for the latest renderer plugin

This commit is contained in:
Kingtous
2023-02-19 15:25:30 +08:00
committed by fufesou
parent ea07b9690e
commit d3455f3ce2
7 changed files with 85 additions and 24 deletions

View File

@@ -19,6 +19,7 @@ import 'package:flutter_hbb/utils/multi_window_manager.dart';
import 'package:flutter_hbb/utils/platform_channel.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:texture_rgba_renderer/texture_rgba_renderer.dart';
import 'package:uni_links/uni_links.dart';
import 'package:uni_links_desktop/uni_links_desktop.dart';
import 'package:url_launcher/url_launcher.dart';
@@ -44,6 +45,10 @@ var isWeb = false;
var isWebDesktop = false;
var version = "";
int androidVersion = 0;
/// Incriment count for textureId.
int _textureId = 0;
int get newTextureId => _textureId ++;
final textureRenderer = TextureRgbaRenderer();
/// only available for Windows target
int windowsBuildNumber = 0;

View File

@@ -63,6 +63,8 @@ class _RemotePageState extends State<RemotePage>
late RxBool _zoomCursor;
late RxBool _remoteCursorMoved;
late RxBool _keyboardEnabled;
late RxInt _textureId;
late int _textureKey;
final _blockableOverlayState = BlockableOverlayState();
@@ -85,6 +87,8 @@ class _RemotePageState extends State<RemotePage>
_showRemoteCursor = ShowRemoteCursorState.find(id);
_keyboardEnabled = KeyboardEnabledState.find(id);
_remoteCursorMoved = RemoteCursorMovedState.find(id);
_textureKey = newTextureId;
_textureId = RxInt(-1);
}
void _removeStates(String id) {
@@ -119,6 +123,16 @@ class _RemotePageState extends State<RemotePage>
if (!Platform.isLinux) {
Wakelock.enable();
}
// Register texture.
_textureId.value = -1;
textureRenderer.createTexture(_textureKey).then((id) async {
if (id != -1) {
final ptr = await textureRenderer.getTexturePtr(_textureKey);
debugPrint("id: $id, texture_key: $_textureKey");
platformFFI.registerTexture(widget.id, ptr);
_textureId.value = id;
}
});
_ffi.ffiModel.updateEventListener(widget.id);
_ffi.qualityMonitorModel.checkShowQualityMonitor(widget.id);
// Session option should be set after models.dart/FFI.start
@@ -198,6 +212,7 @@ class _RemotePageState extends State<RemotePage>
Wakelock.disable();
}
Get.delete<FFI>(tag: widget.id);
textureRenderer.closeTexture(_textureKey);
super.dispose();
_removeStates(widget.id);
}
@@ -346,6 +361,7 @@ class _RemotePageState extends State<RemotePage>
cursorOverImage: _cursorOverImage,
keyboardEnabled: _keyboardEnabled,
remoteCursorMoved: _remoteCursorMoved,
textureId: _textureId,
listenerBuilder: (child) =>
_buildRawPointerMouseRegion(child, enterView, leaveView),
);
@@ -383,6 +399,7 @@ class ImagePaint extends StatefulWidget {
final RxBool cursorOverImage;
final RxBool keyboardEnabled;
final RxBool remoteCursorMoved;
final RxInt textureId;
final Widget Function(Widget)? listenerBuilder;
ImagePaint(
@@ -392,6 +409,7 @@ class ImagePaint extends StatefulWidget {
required this.cursorOverImage,
required this.keyboardEnabled,
required this.remoteCursorMoved,
required this.textureId,
this.listenerBuilder})
: super(key: key);
@@ -466,9 +484,15 @@ class _ImagePaintState extends State<ImagePaint> {
final imageWidth = c.getDisplayWidth() * s;
final imageHeight = c.getDisplayHeight() * s;
final imageSize = Size(imageWidth, imageHeight);
final imageWidget = CustomPaint(
size: imageSize,
painter: ImagePainter(image: m.image, x: 0, y: 0, scale: s),
print("width: $imageWidth/$imageHeight");
// final imageWidget = CustomPaint(
// size: imageSize,
// painter: ImagePainter(image: m.image, x: 0, y: 0, scale: s),
// );
final imageWidget = SizedBox(
width: imageHeight,
height: imageHeight,
child: Obx(() => Texture(textureId: widget.textureId.value)),
);
return NotificationListener<ScrollNotification>(
@@ -493,9 +517,15 @@ class _ImagePaintState extends State<ImagePaint> {
context, _buildListener(imageWidget), c.size, imageSize)),
));
} else {
final imageWidget = CustomPaint(
size: Size(c.size.width, c.size.height),
painter: ImagePainter(image: m.image, x: c.x / s, y: c.y / s, scale: s),
// final imageWidget = CustomPaint(
// size: Size(c.size.width, c.size.height),
// painter: ImagePainter(image: m.image, x: c.x / s, y: c.y / s, scale: s),
// );
final imageWidget = Center(
child: AspectRatio(
aspectRatio: c.size.width / c.size.height,
child: Obx(() => Texture(textureId: widget.textureId.value)),
),
);
return mouseRegion(child: _buildListener(imageWidget));
}

View File

@@ -1446,14 +1446,15 @@ class FFI {
}
} else if (message is EventToUI_Rgba) {
// Fetch the image buffer from rust codes.
final sz = platformFFI.getRgbaSize(id);
if (sz == null || sz == 0) {
return;
}
final rgba = platformFFI.getRgba(id, sz);
if (rgba != null) {
imageModel.onRgba(rgba);
}
// final sz = platformFFI.getRgbaSize(id);
// if (sz == null || sz == 0) {
// return;
// }
// final rgba = platformFFI.getRgba(id, sz);
// if (rgba != null) {
// imageModel.onRgba(rgba);
// }
// imageModel.onRgba(rgba);
}
}
debugPrint('Exit session event loop');

View File

@@ -30,6 +30,9 @@ typedef F4Dart = int Function(Pointer<Utf8>);
typedef F5 = Void Function(Pointer<Utf8>);
typedef F5Dart = void Function(Pointer<Utf8>);
typedef HandleEvent = Future<void> Function(Map<String, dynamic> evt);
// pub fn session_register_texture(id: *const char, ptr: usize)
typedef F6 = Void Function(Pointer<Utf8>, Uint64);
typedef F6Dart = void Function(Pointer<Utf8>, int);
/// FFI wrapper around the native Rust core.
/// Hides the platform differences.
@@ -52,6 +55,8 @@ class PlatformFFI {
F3? _session_get_rgba;
F4Dart? _session_get_rgba_size;
F5Dart? _session_next_rgba;
F6Dart? _session_register_texture;
static get localeName => Platform.localeName;
@@ -130,6 +135,13 @@ class PlatformFFI {
malloc.free(a);
}
void registerTexture(String id, int ptr) {
if (_session_register_texture == null) return;
final a = id.toNativeUtf8();
_session_register_texture!(a, ptr);
malloc.free(a);
}
/// Init the FFI class, loads the native Rust core library.
Future<void> init(String appType) async {
_appType = appType;
@@ -150,6 +162,7 @@ class PlatformFFI {
dylib.lookupFunction<F4, F4Dart>("session_get_rgba_size");
_session_next_rgba =
dylib.lookupFunction<F5, F5Dart>("session_next_rgba");
_session_register_texture = dylib.lookupFunction<F6, F6Dart>("session_register_texture");
try {
// SYSTEM user failed
_dir = (await getApplicationDocumentsDirectory()).path;