opt: read uint8list directly from rust codes

This commit is contained in:
Kingtous
2023-02-12 10:28:04 +08:00
parent 01d30bce9e
commit d2e24173d0
6 changed files with 78 additions and 61 deletions

View File

@@ -417,8 +417,6 @@ class ImageModel with ChangeNotifier {
String id = '';
int decodeCount = 0;
WeakReference<FFI> parent;
final List<Function(String)> _callbacksOnFirstImage = [];
@@ -439,20 +437,16 @@ class ImageModel with ChangeNotifier {
}
}
if (decodeCount >= 1) {
return;
}
final pid = parent.target?.id;
decodeCount += 1;
ui.decodeImageFromPixels(
rgba,
parent.target?.ffiModel.display.width ?? 0,
parent.target?.ffiModel.display.height ?? 0,
isWeb ? ui.PixelFormat.rgba8888 : ui.PixelFormat.bgra8888, (image) {
decodeCount -= 1;
if (parent.target?.id != pid) return;
try {
// Unlock the rgba memory from rust codes.
platformFFI.nextRgba(id);
// my throw exception, because the listener maybe already dispose
update(image);
} catch (e) {
@@ -1370,8 +1364,6 @@ class FFI {
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 {
@@ -1383,29 +1375,15 @@ class FFI {
} else if (message is EventToUI_Rgba) {
// Fetch the image buffer from rust codes.
final sz = platformFFI.getRgbaSize(id);
if (sz == null) {
if (sz == null || sz == 0) {
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!);
final rgba = platformFFI.getRgba(id, sz);
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

@@ -9,6 +9,7 @@ import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hbb/consts.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:win32/win32.dart' as win32;
@@ -23,10 +24,11 @@ class RgbaFrame extends Struct {
}
typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>, Pointer<Utf8>);
typedef F3 = Void Function(Pointer<Utf8>, Pointer<Uint8>);
typedef F3Dart = void Function(Pointer<Utf8>, Pointer<Uint8>);
typedef F3 = Pointer<Uint8> Function(Pointer<Utf8>);
typedef F4 = Uint64 Function(Pointer<Utf8>);
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);
/// FFI wrapper around the native Rust core.
@@ -47,8 +49,9 @@ class PlatformFFI {
final _toAndroidChannel = const MethodChannel('mChannel');
RustdeskImpl get ffiBind => _ffiBind;
F3Dart? _session_get_rgba;
F3? _session_get_rgba;
F4Dart? _session_get_rgba_size;
F5Dart? _session_next_rgba;
static get localeName => Platform.localeName;
@@ -97,13 +100,19 @@ class PlatformFFI {
return res;
}
Uint8List? getRgba(String id, Pointer<Uint8> buffer, int bufSize) {
Uint8List? getRgba(String id, 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;
try {
final buffer = _session_get_rgba!(a);
if (buffer == nullptr) {
return null;
}
final data = buffer.asTypedList(bufSize);
return data;
} finally {
malloc.free(a);
}
}
int? getRgbaSize(String id) {
@@ -114,6 +123,13 @@ class PlatformFFI {
return bufferSize;
}
void nextRgba(String id) {
if (_session_next_rgba == null) return;
final a = id.toNativeUtf8();
_session_next_rgba!(a);
malloc.free(a);
}
/// Init the FFI class, loads the native Rust core library.
Future<void> init(String appType) async {
_appType = appType;
@@ -129,8 +145,11 @@ 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");
_session_get_rgba = dylib.lookupFunction<F3, F3>("session_get_rgba");
_session_get_rgba_size =
dylib.lookupFunction<F4, F4Dart>("session_get_rgba_size");
_session_next_rgba =
dylib.lookupFunction<F5, F5Dart>("session_next_rgba");
try {
// SYSTEM user failed
_dir = (await getApplicationDocumentsDirectory()).path;