Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Pass Uint8List to Pointer<Void> #27

Closed
tzvc opened this issue Nov 13, 2019 · 7 comments
Closed

Pass Uint8List to Pointer<Void> #27

tzvc opened this issue Nov 13, 2019 · 7 comments
Assignees

Comments

@tzvc
Copy link

tzvc commented Nov 13, 2019

Hi there,

I have a C function with the Following signature

bool sendFrame(int width, int height, int channelNumber, void *data, bool clone);

data here being a raw image.

My ffi function signature is:

  final int Function(int, int, int, Pointer<Void>, int) rPPGSendFrame = rPPGLib
      .lookup<
          NativeFunction<
              Int32 Function(
                  Int32, Int32, Int32, Pointer<Void>, Int32)>>("sendFrame")
      .asFunction();

(the bools have to be converted to ints because the bool type is not supported yet (right?))

I'm trying to call this method from dart with a Uint8List coming from a frame of the camera stream https://pub.dev/documentation/camera/latest/camera/Plane-class.html but I'm not sure how to convert/allocate my Uint8List to void *

Any idea?

Cheers!

@dcharkes
Copy link
Contributor

dcharkes commented Nov 13, 2019

Hey @theochampion,

The Uint8List lives in the Dart heap, which is garbage collected, and the objects might be moved around by the garbage collector. So, you'll have to convert it into a pointer into the C heap.

  Uint8List list;
  final pointer = allocate<Uint8>(count: list.length);
  for(int i = 0, ...){
    pointer[i] = list[i];
  }
  final voidStar = pointer.cast<Void>();
  // function call
  free(pointer);

@dcharkes
Copy link
Contributor

@theochampion, does this work for you?

I'll close this issue, please reopen if need be.

@muck27
Copy link

muck27 commented Dec 6, 2021

@dcharkes I am trying to get image data copied as a pointer and pass it to a opencv c++ function using dart ffi. Could you give me a hint of how to make it work?

TIA

@dcharkes
Copy link
Contributor

dcharkes commented Dec 6, 2021

@muck27 could you give more details.

P.S. Feel free to open a new issue rather than reusing an old one if you have a different problem.

@renjuashokan
Copy link

renjuashokan commented Jul 4, 2022

Hi @dcharkes,
is your above reply up-to-date?
Because list.length() is changed to list.length

Also from which package allocate is coming from?

@dcharkes
Copy link
Contributor

dcharkes commented Jul 4, 2022

Also from which package allocate is coming from?

This package. package:ffi.

Because list.length() is changed to list.length

Must have been a typo on my side. It is indeed a property: https://api.dart.dev/stable/2.17.5/dart-core/List/length.html

@dcharkes
Copy link
Contributor

dcharkes commented Jun 1, 2023

Note for people coming across this issue:

  1. If you can get the memory in C to begin with (instead of a Uint8List in the Dart heap), that would avoid copying all together.
  2. Exposing a mem-copy API from Uint8List to Pointers is tracked in [vm/ffi] Provide memcopy function dart-lang/sdk#43968.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants