can successfully compile openmvg into project.
This commit is contained in:
@ -1,131 +1,59 @@
|
||||
|
||||
import 'dart:async';
|
||||
// import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
import 'dart:typed_data';
|
||||
// import 'dart:isolate';
|
||||
// import 'dart:ui';
|
||||
import "package:ffi/ffi.dart";
|
||||
import "package:path/path.dart" show dirname, join;
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'archimedes_mobile_lib_bindings_generated.dart';
|
||||
|
||||
/// A very short-lived native function.
|
||||
///
|
||||
/// For very short-lived functions, it is fine to call them on the main isolate.
|
||||
/// They will block the Dart execution while running the native function, so
|
||||
/// only do this for native functions which are guaranteed to be short-lived.
|
||||
int sum(int a, int b) => _bindings.sum(a, b);
|
||||
|
||||
/// A longer lived native function, which occupies the thread calling it.
|
||||
///
|
||||
/// Do not call these kind of native functions in the main isolate. They will
|
||||
/// block Dart execution. This will cause dropped frames in Flutter applications.
|
||||
/// Instead, call these native functions on a separate isolate.
|
||||
///
|
||||
/// Modify this to suit your own use case. Example use cases:
|
||||
///
|
||||
/// 1. Reuse a single isolate for various different kinds of requests.
|
||||
/// 2. Use multiple helper isolates for parallel execution.
|
||||
Future<int> sumAsync(int a, int b) async {
|
||||
final SendPort helperIsolateSendPort = await _helperIsolateSendPort;
|
||||
final int requestId = _nextSumRequestId++;
|
||||
final _SumRequest request = _SumRequest(requestId, a, b);
|
||||
final Completer<int> completer = Completer<int>();
|
||||
_sumRequests[requestId] = completer;
|
||||
helperIsolateSendPort.send(request);
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
const String _libName = 'archimedes_mobile_lib';
|
||||
final here = dirname(Platform.script.path);
|
||||
final build = join(dirname(here), "build");
|
||||
|
||||
/// The dynamic library in which the symbols for [ArchimedesMobileLibBindings] can be found.
|
||||
final DynamicLibrary _dylib = () {
|
||||
if (Platform.isMacOS || Platform.isIOS) {
|
||||
return DynamicLibrary.open('$_libName.framework/$_libName');
|
||||
return DynamicLibrary.open(join(build, '$_libName.framework', _libName));
|
||||
}
|
||||
if (Platform.isAndroid || Platform.isLinux) {
|
||||
return DynamicLibrary.open('lib$_libName.so');
|
||||
return DynamicLibrary.open(join(build, 'lib$_libName.so'));
|
||||
}
|
||||
if (Platform.isWindows) {
|
||||
return DynamicLibrary.open('$_libName.dll');
|
||||
return DynamicLibrary.open(join('$_libName.dll'));
|
||||
}
|
||||
throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
|
||||
}();
|
||||
|
||||
/// The bindings to the native functions in [_dylib].
|
||||
final ArchimedesMobileLibBindings _bindings = ArchimedesMobileLibBindings(_dylib);
|
||||
final ArchimedesMobileLibBindings _bindings =
|
||||
ArchimedesMobileLibBindings(_dylib);
|
||||
|
||||
int getImageData(Pointer<Frame> frame, Pointer<UnsignedChar> data) =>
|
||||
_bindings.archimedes_get_image_data(frame, data);
|
||||
|
||||
/// A request to compute `sum`.
|
||||
///
|
||||
/// Typically sent from one isolate to another.
|
||||
class _SumRequest {
|
||||
final int id;
|
||||
final int a;
|
||||
final int b;
|
||||
Pointer<Frame> newFrame(
|
||||
Pointer<Uint8> data, int dataLen, int w, int h, int depth) =>
|
||||
_bindings.new_frame_from_data(data, dataLen, w, h, depth);
|
||||
|
||||
const _SumRequest(this.id, this.a, this.b);
|
||||
void main(List<String> arguments) {
|
||||
final assets = join(dirname(here), "assets");
|
||||
final testFrames = join(assets, "test", "frames");
|
||||
final firstFrame = join(testFrames, "0001.png");
|
||||
final f = File(firstFrame);
|
||||
final l = f.readAsBytesSync().cast();
|
||||
final myPointer = malloc<Uint8>(l.length);
|
||||
for (int i = 0; i < l.length; ++i) {
|
||||
myPointer[i] = l[i];
|
||||
print(myPointer[i]);
|
||||
}
|
||||
|
||||
Pointer<Frame> frame = newFrame(myPointer, l.length, 768, 768, 64);
|
||||
|
||||
Pointer<UnsignedChar> data = malloc<UnsignedChar>(l.length * 4);
|
||||
|
||||
getImageData(frame, data);
|
||||
}
|
||||
|
||||
/// A response with the result of `sum`.
|
||||
///
|
||||
/// Typically sent from one isolate to another.
|
||||
class _SumResponse {
|
||||
final int id;
|
||||
final int result;
|
||||
|
||||
const _SumResponse(this.id, this.result);
|
||||
}
|
||||
|
||||
/// Counter to identify [_SumRequest]s and [_SumResponse]s.
|
||||
int _nextSumRequestId = 0;
|
||||
|
||||
/// Mapping from [_SumRequest] `id`s to the completers corresponding to the correct future of the pending request.
|
||||
final Map<int, Completer<int>> _sumRequests = <int, Completer<int>>{};
|
||||
|
||||
/// The SendPort belonging to the helper isolate.
|
||||
Future<SendPort> _helperIsolateSendPort = () async {
|
||||
// The helper isolate is going to send us back a SendPort, which we want to
|
||||
// wait for.
|
||||
final Completer<SendPort> completer = Completer<SendPort>();
|
||||
|
||||
// Receive port on the main isolate to receive messages from the helper.
|
||||
// We receive two types of messages:
|
||||
// 1. A port to send messages on.
|
||||
// 2. Responses to requests we sent.
|
||||
final ReceivePort receivePort = ReceivePort()
|
||||
..listen((dynamic data) {
|
||||
if (data is SendPort) {
|
||||
// The helper isolate sent us the port on which we can sent it requests.
|
||||
completer.complete(data);
|
||||
return;
|
||||
}
|
||||
if (data is _SumResponse) {
|
||||
// The helper isolate sent us a response to a request we sent.
|
||||
final Completer<int> completer = _sumRequests[data.id]!;
|
||||
_sumRequests.remove(data.id);
|
||||
completer.complete(data.result);
|
||||
return;
|
||||
}
|
||||
throw UnsupportedError('Unsupported message type: ${data.runtimeType}');
|
||||
});
|
||||
|
||||
// Start the helper isolate.
|
||||
await Isolate.spawn((SendPort sendPort) async {
|
||||
final ReceivePort helperReceivePort = ReceivePort()
|
||||
..listen((dynamic data) {
|
||||
// On the helper isolate listen to requests and respond to them.
|
||||
if (data is _SumRequest) {
|
||||
final int result = _bindings.sum_long_running(data.a, data.b);
|
||||
final _SumResponse response = _SumResponse(data.id, result);
|
||||
sendPort.send(response);
|
||||
return;
|
||||
}
|
||||
throw UnsupportedError('Unsupported message type: ${data.runtimeType}');
|
||||
});
|
||||
|
||||
// Send the port to the main isolate on which we can receive requests.
|
||||
sendPort.send(helperReceivePort.sendPort);
|
||||
}, receivePort.sendPort);
|
||||
|
||||
// Wait until the helper isolate has sent us back the SendPort on which we
|
||||
// can start sending requests.
|
||||
return completer.future;
|
||||
}();
|
||||
|
@ -8,7 +8,7 @@
|
||||
// ignore_for_file: type=lint
|
||||
import 'dart:ffi' as ffi;
|
||||
|
||||
/// Bindings for `src/archimedes_mobile_lib.h`.
|
||||
/// Bindings for `src/image.h`.
|
||||
///
|
||||
/// Regenerate bindings with `flutter pub run ffigen --config ffigen.yaml`.
|
||||
///
|
||||
@ -27,44 +27,164 @@ class ArchimedesMobileLibBindings {
|
||||
lookup)
|
||||
: _lookup = lookup;
|
||||
|
||||
/// A very short-lived native function.
|
||||
///
|
||||
/// For very short-lived functions, it is fine to call them on the main isolate.
|
||||
/// They will block the Dart execution while running the native function, so
|
||||
/// only do this for native functions which are guaranteed to be short-lived.
|
||||
int sum(
|
||||
int a,
|
||||
int b,
|
||||
ffi.Pointer<Frame> new_frame_from_handle(
|
||||
ffi.Pointer<FILE> arg0,
|
||||
int arg1,
|
||||
int arg2,
|
||||
int arg3,
|
||||
) {
|
||||
return _sum(
|
||||
a,
|
||||
b,
|
||||
return _new_frame_from_handle(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
arg3,
|
||||
);
|
||||
}
|
||||
|
||||
late final _sumPtr =
|
||||
_lookup<ffi.NativeFunction<ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr)>>(
|
||||
'sum');
|
||||
late final _sum = _sumPtr.asFunction<int Function(int, int)>();
|
||||
late final _new_frame_from_handlePtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Pointer<Frame> Function(ffi.Pointer<FILE>, ffi.Int, ffi.Int,
|
||||
ffi.Int)>>('new_frame_from_handle');
|
||||
late final _new_frame_from_handle = _new_frame_from_handlePtr.asFunction<
|
||||
ffi.Pointer<Frame> Function(ffi.Pointer<FILE>, int, int, int)>();
|
||||
|
||||
/// A longer lived native function, which occupies the thread calling it.
|
||||
///
|
||||
/// Do not call these kind of native functions in the main isolate. They will
|
||||
/// block Dart execution. This will cause dropped frames in Flutter applications.
|
||||
/// Instead, call these native functions on a separate isolate.
|
||||
int sum_long_running(
|
||||
int a,
|
||||
int b,
|
||||
ffi.Pointer<Frame> new_frame_from_data(
|
||||
ffi.Pointer<ffi.Uint8> arg0,
|
||||
int arg1,
|
||||
int arg2,
|
||||
int arg3,
|
||||
int arg4,
|
||||
) {
|
||||
return _sum_long_running(
|
||||
a,
|
||||
b,
|
||||
return _new_frame_from_data(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
arg3,
|
||||
arg4,
|
||||
);
|
||||
}
|
||||
|
||||
late final _sum_long_runningPtr =
|
||||
_lookup<ffi.NativeFunction<ffi.IntPtr Function(ffi.IntPtr, ffi.IntPtr)>>(
|
||||
'sum_long_running');
|
||||
late final _sum_long_running =
|
||||
_sum_long_runningPtr.asFunction<int Function(int, int)>();
|
||||
late final _new_frame_from_dataPtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Pointer<Frame> Function(ffi.Pointer<ffi.Uint8>, ffi.Size, ffi.Int,
|
||||
ffi.Int, ffi.Int)>>('new_frame_from_data');
|
||||
late final _new_frame_from_data = _new_frame_from_dataPtr.asFunction<
|
||||
ffi.Pointer<Frame> Function(
|
||||
ffi.Pointer<ffi.Uint8>, int, int, int, int)>();
|
||||
|
||||
int archimedes_get_image_data(
|
||||
ffi.Pointer<Frame> arg0,
|
||||
ffi.Pointer<ffi.UnsignedChar> arg1,
|
||||
) {
|
||||
return _archimedes_get_image_data(
|
||||
arg0,
|
||||
arg1,
|
||||
);
|
||||
}
|
||||
|
||||
late final _archimedes_get_image_dataPtr = _lookup<
|
||||
ffi.NativeFunction<
|
||||
ffi.Int Function(ffi.Pointer<Frame>,
|
||||
ffi.Pointer<ffi.UnsignedChar>)>>('archimedes_get_image_data');
|
||||
late final _archimedes_get_image_data =
|
||||
_archimedes_get_image_dataPtr.asFunction<
|
||||
int Function(ffi.Pointer<Frame>, ffi.Pointer<ffi.UnsignedChar>)>();
|
||||
}
|
||||
|
||||
final class _Frame extends ffi.Struct {
|
||||
external ffi.Pointer<FILE> stream;
|
||||
|
||||
@ffi.Int()
|
||||
external int w;
|
||||
|
||||
@ffi.Int()
|
||||
external int h;
|
||||
|
||||
@ffi.Int()
|
||||
external int depth;
|
||||
}
|
||||
|
||||
typedef FILE = _IO_FILE;
|
||||
|
||||
final class _IO_FILE extends ffi.Struct {
|
||||
@ffi.Int()
|
||||
external int _flags;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_read_ptr;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_read_end;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_read_base;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_write_base;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_write_ptr;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_write_end;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_buf_base;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_buf_end;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_save_base;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_backup_base;
|
||||
|
||||
external ffi.Pointer<ffi.Char> _IO_save_end;
|
||||
|
||||
external ffi.Pointer<_IO_marker> _markers;
|
||||
|
||||
external ffi.Pointer<_IO_FILE> _chain;
|
||||
|
||||
@ffi.Int()
|
||||
external int _fileno;
|
||||
|
||||
@ffi.Int()
|
||||
external int _flags2;
|
||||
|
||||
@__off_t()
|
||||
external int _old_offset;
|
||||
|
||||
@ffi.UnsignedShort()
|
||||
external int _cur_column;
|
||||
|
||||
@ffi.SignedChar()
|
||||
external int _vtable_offset;
|
||||
|
||||
@ffi.Array.multi([1])
|
||||
external ffi.Array<ffi.Char> _shortbuf;
|
||||
|
||||
external ffi.Pointer<_IO_lock_t> _lock;
|
||||
|
||||
@__off64_t()
|
||||
external int _offset;
|
||||
|
||||
external ffi.Pointer<_IO_codecvt> _codecvt;
|
||||
|
||||
external ffi.Pointer<_IO_wide_data> _wide_data;
|
||||
|
||||
external ffi.Pointer<_IO_FILE> _freeres_list;
|
||||
|
||||
external ffi.Pointer<ffi.Void> _freeres_buf;
|
||||
|
||||
@ffi.Size()
|
||||
external int __pad5;
|
||||
|
||||
@ffi.Int()
|
||||
external int _mode;
|
||||
|
||||
@ffi.Array.multi([20])
|
||||
external ffi.Array<ffi.Char> _unused2;
|
||||
}
|
||||
|
||||
final class _IO_marker extends ffi.Opaque {}
|
||||
|
||||
typedef __off_t = ffi.Long;
|
||||
typedef _IO_lock_t = ffi.Void;
|
||||
typedef __off64_t = ffi.Long;
|
||||
|
||||
final class _IO_codecvt extends ffi.Opaque {}
|
||||
|
||||
final class _IO_wide_data extends ffi.Opaque {}
|
||||
|
||||
typedef Frame = _Frame;
|
||||
|
Reference in New Issue
Block a user