What is Flutter Impeller?
https://blog.nonstopio.com/flutter-impellers-d18d47a809d9
Credits: wikiHow
Flutter 3.10 introduces Impeller in iOS apps by default. So these iOS apps will have less jank and better consistent performance. And for Android, Impeller is in the development phase. But we should know what Impeller is.
Impeller is a new renderer within Flutter’s engine. Until now, Flutter has been using Skia. Skia has built-in rendering features which can be used for various devices. But Skia is not always optimized solution for Flutter’s needs.
Before going further, let’s see
The basic Scenario of Impeller is that (check this issue)-
If we had a static set of shaders, we could asynchronously compile them on program startup, so that by the time the application runs an animation we can use this static set and not stutter while we prepare a new one.
Medium term we could also determine what specialized shaders we would want to use in each scene, and compile them in the background so that the next time, if all the specialized shaders we would want to use are ready, we could just use them. That way we get the long-term sustained performance benefits of specialized shaders and the short-term smooth start performance benefits of a static shader set.
Yes, our main goal is to eliminate jank or any stuttering that’s happening inside your app. But first, we should know what is Renderer.
Renderer software helps to translate the code into the pixels that are visible on the screen.
Whenever we build any widget into the Flutter that goes into some steps like Widget Tree -> Element Tree -> Render Object Tree.
But what happened after that? We will go step by step.
- Display List — Render objects contain the instruction for how to actually layout and how that widget should be painted, in the form of instructions, and those instructions are given to the engine as a simple command, which is called Display List. Now an engine will decide whether the widget should be rendered either using Impeller or Skia.
2. Render Pipelines — Render Pipelines that can be used to render everything given in the Display List.
3. Shader — A Shader is nothing but a small piece of code that gets executed on the graphics device.
4. Rasterization — Shaders take the vertices (say for ex. Flutter Logo) and it will move them on the screen where exactly we want to draw it. This process will iterate through all the given triangles of this Flutter Logo(Ex. Fig.1). For each vertex into say triangle of any image, we figure out the specific pixels that are inside it, and this is called rasterization.
Fig. 1 (Ref)
But wait...
Those Shaders and Render Pipeline need compilation to get instructed by the GPU so that GPU can execute it.
AND THIS COMPILATION PROCESS IS VERY EXPENSIVE.
And that’s how Impeller comes into the picture. Let’s discuss this issue in detail.
Shader Compilation Jank Issue —We know that Shaders are the low-level code given to GPU to draw UI on screen.
Skia compiles and generates these shaders (at runtime) when running app for the first time. The generation of shaders requires quite a bit of time(approx. 200ms or more depending on the situation) and this causes the frame, which can’t be rendered within 16ms to jank. This jank is called shader-compilation-jank.
Impeller resolved this issue by pre-compiling a smaller, simpler set of shaders at build time.
First, we will see how exactly Impeller Architecture works,
Now let’s take this Display List and draw it using Impeller.
Impeller Architecture
1. Aiks — The Display List operations are dispatched to Aiks. Aiks is the topmost layer of Impeller and it mainly contains the canvas drawing API.
2. Entity — Aiks takes the higher level commands from the DisplayList and translates them into the simpler self-contained drawing operations called Entities.
3. Contents of Entity — These are the Content Objects of the Entity, and it contains the actual GPU instructions need to draw the Entity.
4. Hardware Abstraction Layer [HAL] — Impeller needs some kind of translation layer by which it can communicate with the GPU, which called as Hardware Abstraction Layer. Render Pipelines contain the Shaders, and each Content of Entity uses the HAL to draw itself by giving instructions to GPU to render these Shaders. This HAL talks to the graphics driver through various standard graphics APIs like Metal on iOS and Vulcan on Android. And then this is how the resulting texture gets displayed on the screen.
Impeller generates and compiles all necessary Shaders (ahead of time) when we build the Flutter Engine. Therefore apps running on Impeller will have a predefined set of Shaders they need, and the shaders can be used without introducing jank into animations. This operation can be done by using Impellerlc Shader Compiler.
Impellerlc Shader Compiler — Impeller has a set of handwritten shaders compiled in advance. Impeller precompiles a smaller, simpler set of shaders at Engine build time so they don’t compile at runtime.
After a preview period since January 2023, Impeller is now on-by-default on iOS in a stable branch. Android up next.
Impeller Architecture
Here, I have taken this example and tried to run it from the IDE (Android Studio).
Pre-Impeller build
Can you see that, the app got stuck at a very early stage of the app run.
Now as per this, to see what direction Android support will take, experiment with Impeller in the 3.7 or later stable release, I enabled Impeller for Android and tried to run the same app from scratch. And got the below observations.
Post-Impeller build
Here, the animation runs smoothly after enabling Impeller.
Hope you enjoyed this article!
Here to make the community stronger by sharing our knowledge. Follow me and my team to stay updated on the latest and greatest in the web & mobile tech world.
Ref:
'프로그래밍 > App' 카테고리의 다른 글
[Flutter] Flutter vs React Native (0) | 2023.08.11 |
---|---|
Otter: Transcribe Voice Notes. STT 소리를 텍스트로 (0) | 2023.08.07 |
What’s new in Flutter 3.10 (0) | 2023.08.03 |
Flutter Web in 2023 — is it good for you? (0) | 2023.07.14 |
Threads, an Instagram app (0) | 2023.07.07 |