C++ library drawing on a Flutter Canvas?

401 views
Skip to first unread message

Odev

unread,
May 1, 2020, 4:24:01 AM5/1/20
to Dart FFI
Hi,

I have a library written in C++ for drawing on a canvas. It leaves the implementation of the canvas up to the user, depending on which platform the library is meant to be used on; it just expects various functions like canvas.setColor() and canvas.drawLine() to be available. I was wondering if it'd be at all possible to use this library in conjunction with a Flutter Canvas (which supports all those functions), via FFI?

Of course I'd be happy to write some kind of wrappers in Flutter/Dart and C/C++, but my understanding is that even with this, this might be tricky. Is there some reasonable way to work around this kind of issue? Is the easiest way to implement a dummy canvas in C++ that caches all the instructions in some data type, and then pull this from Flutter using FFI, and draw the instructions? This could get rather tedious; is there some best practice for encoding this kind of thing?

Kind regards,
Odev

Daco Harkes 🔵

unread,
May 13, 2020, 8:58:26 AM5/13/20
to Odev, Dart FFI
Hi Odev,

Nice to meet you!

Assuming you're talking about the Dart API Canvas in Flutter: You can use callbacks from C back in to Dart to invoke the relevant Dart functions on Canvas.

If you want to do the callbacks from an arbitrary thread (instead of the main Dart thread), you need to use native ports. How that works is documented in these 2 samples.

If you have more than one Canvas object, currently the workaround is to have a Map<int,Canvas> in Dart and pass the integer through C. (I'm working on something that allows you to pass objects through the FFI, but this is not ready yet.)

Kind regards,

--
You received this message because you are subscribed to the Google Groups "Dart FFI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dart-ffi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-ffi/b695ba4d-1fcd-461b-bbea-d02dbc3c5c47%40googlegroups.com.


--
Daco Harkes

Odev

unread,
May 15, 2020, 10:09:54 AM5/15/20
to Dart FFI
Many thanks for your reply, Daco! I am indeed talking about the Dart API Canvas in Flutter.

Are there any examples that demonstrate using simple callbacks from C back into Dart, with both the C code and the Dart code? In the (I presume simpler) case where everything's happening in the same thread, that is. (I'm afraid I don't understand yet how the threading will work when calling an external library, even if it is all being built from source.)

I found sample_ffi_functions_callbacks.dart which I assume is one half of this, just missing the C code. Is that a good place to start?

Great work with FFI!

Thanks!
Odev
To unsubscribe from this group and stop receiving emails from it, send an email to dart...@googlegroups.com.


--
Daco Harkes

Daco Harkes 🔵

unread,
May 15, 2020, 10:34:14 AM5/15/20
to Odev, Dart FFI
Hi Odev,

Yes, those samples are synchronous. The C code is here: ffi_test_functions.cc.

Have a nice day!

To unsubscribe from this group and stop receiving emails from it, send an email to dart-ffi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-ffi/906fab64-f5d2-442f-9211-7d79a08f5f35%40googlegroups.com.


--
Daco Harkes
Reply all
Reply to author
Forward
0 new messages