Description
Currently, when writing FFI plugins there is no good way to interact with the engine. One options is to write a Flutter plugin, that either stores reference to the engine statically to be used with FFI, or uses platform channels to let the isolate access engine address and then possibly use it through FFI calls. Both of these are problematic, because a) storing reference to engine statically assumes that there's only one engine running b) using platform channels requires asynchronous call to get engine address.
Neither of these workarounds would help us if we want to use FFI inside our embedders, which is something we might want to do in future, i.e. for #150460 or multi-window.
Better solution would be to provide engine handle to the root isolate at startup. Dart code can then pass this value to FFI calls which could then obtain the Flutter engine.
How exactly the engine is obtained depends on the platform.
- iOS, macOS -
[FlutterEngine engineForHandle:handle]
. - Windows -
FlutterDesktopEngineForHandle(handle)
- Linux -
fl_engine_for_handle(handle)
. - Android -
io.flutter.embedding.engine.FlutterEngine.forHandle(handle)
(static method).
On dart side this could be accessed from PlatformDispatcher
, i.e. PlatformDispatcher.instance.engineHandle
. The value would be null
for web and custom embedders that don't set the value.
The value would be set through embedder API by the engine and available from isolate start.