As you know, flutter is a multi-platform ui engine for mobile, desktop and web, the flutter engine architecture is like this.
On the bottom lies the embedder which creates the threads and surface for the engine.
So take the iOS platform embedder for an example, here how the embedder creates the underlying support for flutter.
What embedder does:
create the Shell object defined in src/flutter/shell/common/shell.cc
This is the object that is at the core of flutter engine which handles ui rendering, platform messaging and flutter engine creation.
Let’s look at the comment section for this class,
Perhaps the single most important class in the Flutter engine repository.
When embedders create a Flutter application, they are referring to the
creation of an instance of a shell.
Creation and destruction of the shell is
synchronous and the embedder only holds a unique pointer to the shell. The shell does not create the threads its primary components run on. Instead, it is the embedder’s responsibility to create threads and give the shell task runners for those threads.
This basically tells us that the creation of the flutter engine means the creation of the Shell object by the embedder.
So let’s begin digging into the shell creation process.
There’s two things the embedder has to provide,
- a surface for drawing
- an engine for dart code execution.
1.The initialization process
This part is where dart:ui draws the app. The embedder has to provide a surface for the skia 2d engine to draw the flutter app.
on iOS platform, the surface is a FlutterView class, which is held by the FlutterViewController class, which itself is held by the AppDelegate of the App.
when the app launches, the flutter ViewController’s designated initializer is called by the storyboard,
- (instancetype)initWithProject:(nullable FlutterDartProject*)project nibName:(nullable NSString*)nibName bundle:(nullable NSBundle*)nibBundle
in this method the engine is created and
[_engine createShell:nil liraryURI:nil]; is called for shell creation.
this method in turn calls the c++ method
this c++ method handles the task runner creation,
in convention the platform and gpu task runner are the same.
Then in FlutterViewController’s
viewWillAppear: method, flutter engine will be set to launch, which performs the following steps,
- check configuration valid
- get entry point?
- check isolate run status
- if isolate running add settings’ callbacks to isolate
- get service id and handle platform service id messge
I’m still figuring out the calling stack and details of the running process so,
article to be continued …