opt: reduce copy and malloc times for both of flutter and rust

This commit is contained in:
Kingtous
2023-02-12 01:52:11 +08:00
parent f521b1665a
commit 01d30bce9e
7 changed files with 105 additions and 38 deletions

View File

@@ -1,10 +1,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:ffi' hide Size;
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:ffi/ffi.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hbb/consts.dart';
@@ -1367,6 +1369,9 @@ class FFI {
final stream = bind.sessionStart(id: id);
final cb = ffiModel.startEventListener(id);
() async {
// Preserved for the rgba data.
Pointer<Uint8>? buffer;
int? bufferSize;
await for (final message in stream) {
if (message is EventToUI_Event) {
try {
@@ -1377,13 +1382,30 @@ class FFI {
}
} else if (message is EventToUI_Rgba) {
// Fetch the image buffer from rust codes.
bind.sessionGetRgba(id: id).then((rgba) {
if (rgba != null) {
imageModel.onRgba(rgba);
final sz = platformFFI.getRgbaSize(id);
if (sz == null) {
return;
}
// The buffer does not exists or the bufferSize is not
// equal to the required size.
if (buffer == null || bufferSize != sz) {
// reallocate buffer
if (buffer != null) {
malloc.free(buffer);
}
});
buffer = malloc.allocate(sz);
bufferSize = sz;
}
final rgba = platformFFI.getRgba(id, buffer, bufferSize!);
if (rgba != null) {
imageModel.onRgba(rgba);
}
}
}
// Free the buffer allocated on the heap.
if (buffer != null) {
malloc.free(buffer);
}
}();
// every instance will bind a stream
this.id = id;

View File

@@ -23,7 +23,10 @@ class RgbaFrame extends Struct {
}
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = Void Function(Pointer<Utf8>, Pointer<Uint8>);
typedef F3Dart = void Function(Pointer<Utf8>, Pointer<Uint8>);
typedef F4 = Uint64 Function(Pointer<Utf8>);
typedef F4Dart = int Function(Pointer<Utf8>);
typedef HandleEvent = Future<void> Function(Map<String, dynamic> evt);
/// FFI wrapper around the native Rust core.
@@ -44,6 +47,8 @@ class PlatformFFI {
final _toAndroidChannel = const MethodChannel('mChannel');
RustdeskImpl get ffiBind => _ffiBind;
F3Dart? _session_get_rgba;
F4Dart? _session_get_rgba_size;
static get localeName => Platform.localeName;
@@ -92,6 +97,23 @@ class PlatformFFI {
return res;
}
Uint8List? getRgba(String id, Pointer<Uint8> buffer, int bufSize) {
if (_session_get_rgba == null) return null;
var a = id.toNativeUtf8();
_session_get_rgba!(a, buffer);
final data = buffer.asTypedList(bufSize);
malloc.free(a);
return data;
}
int? getRgbaSize(String id) {
if (_session_get_rgba_size == null) return null;
var a = id.toNativeUtf8();
final bufferSize = _session_get_rgba_size!(a);
malloc.free(a);
return bufferSize;
}
/// Init the FFI class, loads the native Rust core library.
Future<void> init(String appType) async {
_appType = appType;
@@ -107,6 +129,8 @@ class PlatformFFI {
debugPrint('initializing FFI $_appType');
try {
_translate = dylib.lookupFunction<F2, F2>('translate');
_session_get_rgba = dylib.lookupFunction<F3, F3Dart>("session_get_rgba");
_session_get_rgba_size = dylib.lookupFunction<F4, F4Dart>("session_get_rgba_size");
try {
// SYSTEM user failed
_dir = (await getApplicationDocumentsDirectory()).path;