1

We are developing a "image editor" app using Flutter. Thus, we need to (1) show images on the UI, and (2) manipulate images using some algorithms in C/C++.

Flutter does have a FFI between dart and c/c++, but I do not know how to pass big arrays like images (e.g. can be megabytes) efficiently?

Thanks very much!

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
ch271828n
  • 15,854
  • 5
  • 53
  • 88

2 Answers2

2

After experiments, now I have some code (in the production code!) passing around array of 20MB size between C and Dart. It only takes several milliseconds (just a rough estimation, but surely it does not take seconds).

Thus, my answer to my question is: Just do it. Memory copies seems to be quite cheap, and do not worry about it :)


EDIT in 2022:

Flutter does have have "zero copy" array transfer when going from native to Flutter side, via DartCObject. See the "ZeroCopyBuffer" feature in my library https://github.com/fzyzcjy/flutter_rust_bridge for an example of usage.

ch271828n
  • 15,854
  • 5
  • 53
  • 88
1

You can use asTypedList method, which works with alot of dart Pointer classes such as Uint8Pointer...

Example Usage:

Int32List l = myNativeCall().asTypedList(myListLength); //myNativeCall() returns a Pointer<Int32>

Note that the native memory should stay accessible as long as list l is going to be used.

You may want also to use NativeFinalizer when using this method to free the native memory when the corresponding dart object is garbage collected, to avoid memory leaks.

HII
  • 3,420
  • 1
  • 14
  • 35