N coding
Flutter : FFI 본문
What is flutter FFI?
플러터 FFI란?
https://summer-milkshake-01b.notion.site/Flutter-FFI-c91c46b8ca8f4138af4a072acc4d089c
FFI = Foreign function interface
https://en.wikipedia.org/wiki/Foreign_function_interface
한 언어로 작성된 프로그램이 다른 언어로 쓰인 함수나 서비스를 사용할 수 있게 하는 것
주된 기능
- 한 언어(host language = FFI를 정의한 쪽)의 semantic, calling convention을 다른 언어(guest language)의 semantic, convention과 일치시키는 것
- runtime 환경 and/or applicaiton binary interface를 고려해야한다
방법
- Wrapper library 사용
- 적절한 glue code랑 함께 자동으로 guest lang 함수를 wrap해주는 툴을 사용
- Cross lang으로 사용될 수 있는 host lang의 기능을 제한
- Guest lang 함수를 특정 방식으로 구현하거나 제한함
고려 사항
- 한 언어는 GC를 지원하고 다른 언어는 아닌 경우, GC를 하는 언어가 다른 언어의 개체 메모리를 막 해제하지 않도록 주의
- Complicated or non-trivial objects or datatypes may be difficult to map from one environment to another.
- It may not be possible for both languages to maintain references to the same instance of a mutable object, due to the mapping issue above.
- One or both languages may be running on a virtual machine (VM); moreover, if both are, these will probably be different VMs.
- Cross-language inheritance and other differences, such as between type systems or between object-composition models, may be especially difficult.
dart:ffi
https://flutter.dev/docs/development/platform-integration/c-interop
https://api.dart.dev/dev/2.14.0-321.0.dev/dart-ffi/dart-ffi-library.html
dart:ffi 라이브러리를 사용해서 C 언어를 호환하여 사용할 수 있다.
native code에 바인딩 하려면 확인
- natvie code 가 로드되어있는지
- sysbol이 Dart에 visible한지
Dynamic vs. Static Linking
native library는 동적 혹은 정적으로 연결가능
Static
- 앱의 실행 파일 이미지에 내장되어있으며 시작될 떄 로드
- DynamicLibrary.executable 또는 `DynamicLibrary.process`` 를 통해 로드 가능
Dynamic
- 앱 내 별도의 파일 또는 폴더에 배포되며 필요할 때 로드 됨
- [DynamicLibrary.open](<http://dynamiclibrary.open>) 을 통해 로드 가능
HOW TO
- Create a plugin
- Add C/C++ source
- The FFI library can only bind against C symbols, so in C++ these symbols must be marked extern C. You should also add attributes to indicate that the symbols are referenced from Dart, to prevent the linker from discarding the symbols during link-time optimization.
- Load the code using Dart : ffi
- import 'dart:ffi'; // For FFI import 'dart:io'; // For Platform.isX final DynamicLibrary nativeAddLib = Platform.isAndroid ? DynamicLibrary.open("libnative_add.so") : DynamicLibrary.process(); final int Function(int x, int y) nativeAdd = nativeAddLib .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add") .asFunction();
Calling Native Libraries in Flutter with Dart FFI
Call tizen api
url_launcher_tizen 플러그인에서 사용함
https://github.com/flutter-tizen/plugins/blob/master/packages/url_launcher/lib/src/app_control.dart
Method Channel vs. FFI
https://www.raywenderlich.com/21512310-calling-native-libraries-in-flutter-with-dart-ffi
device나 OS specific한 기능을 위해서는 Method Channel을 사용
Method Channel은 platform api를 사용하게 해주는 것
- ex. Camera, Notification, Integration with other app
third-party, platform-agnostic (OS나 프로세서 조합에 상관없이 수행가능한 기술) 라이브러리를 사용하는 app을 작성하고 있으면 FFI 사용
- ex. 머신 러닝, 렌더링 엔진, 암호화 라이브러리, 신호 처리
https://linuxtut.com/en/564e1c3725a4be48e40a/
Under the conditions measured this time, if data conversion between Dart and C is required, There was no significant difference between Method Channel and FFI.
On the other hand, if you don't need data conversion, use asTypedList for FFI. You can reduce the number of data copies while accessing from both sides of Dart / C. Of course, the larger the data size, the greater the effect, and under the measurement conditions this time, the FFI was about 100 times faster.
Here is a tour repo for dart:ffi: https://github.com/Sunbreak/native_interop.tour
- Async/Sync
- For MethodChannel, both Dart -> Native and Native -> Dart are asynchronous
- For dart:ffi, Dart -> Native and Native -> Dart could be synchronous (except native call from non-mutator thread of Isolate)
- Memory
- For MethodChannel, each interop requires serilization/deseriliazation
- For dart:ffi, you'd easily write C-like memory-efficient operation
- Performance
dart:ffi synchronous interop has a good advantage on non-frequent small data
https://blog.naver.com/nywoo19/222434770054
'Dart&Flutter' 카테고리의 다른 글
Flutter : How to develop flutter on tizen? (0) | 2022.01.20 |
---|---|
Dart : async/await/Future (0) | 2022.01.20 |
Flutter : Flutter onDrawerSlide 구현하기 (0) | 2022.01.20 |
Dart : what is extenstion ? extension keyword (0) | 2022.01.20 |
Dart : enum with values (enum-like class) (0) | 2022.01.20 |