Sub topic 1
How Flutter Produces a Frame
Flutter displays an app by producing a sequence of images called frames. The operating system asks for a new frame at each screen refresh. Flutter then builds the widget tree, calculates positions, draws visual elements, and sends the result to the screen.
This chapter assumes you can build and run a Flutter app, understand build() methods, and use basic Flutter command-line commands.
Prerequisites
You should have:
- A working Flutter SDK and DevTools
- A physical device or emulator
- Basic knowledge of widgets and
StatefulWidget - A working understanding of
build(), layout, and painting
The frame pipeline
A frame passes through several stages. Flutter's framework and engine divide much of this work between a UI thread and a raster thread.
The UI thread runs Dart code. It handles application logic, widget building, layout, and much of the creation of drawing instructions. The raster thread turns those instructions into pixels. The GPU then presents those pixels on the display.
The widget tree is a description of what the interface should contain. During the build stage, Flutter calls widget build() methods and creates or updates elements. During layout, render objects calculate sizes and positions. During painting, those render objects record commands such as drawing text, images, shapes, and decorations.
The raster thread consumes the recorded commands. It may upload images, apply clips and shadows, and communicate with the GPU. A frame is complete only when both the UI-side work and the raster-side work finish in time.
Vsync and the frame budget
A display refreshes at a fixed rate, such as 60, 90, or 120 times per second. Vsync, short for vertical synchronization, is the signal that identifies when the next frame should be ready.
At 60 Hz, one refresh interval is about 16.67 milliseconds:
| Refresh rate | Approximate frame interval |
|---|---|
| 60 Hz | 16.67 ms |
| 90 Hz | 11.11 ms |
| 120 Hz | 8.33 ms |
The available time is a budget, not a guaranteed amount of time for Dart code. UI-thread work and raster-thread work must both fit into the interval. If either side misses the deadline, the display keeps showing the previous frame or presents a later frame. The result is visible as a pause or uneven motion. This is called jank.
What happens during an update
Suppose a tap changes a counter. The event reaches Dart code on the UI thread. Calling setState() marks part of the application as needing an update. Flutter schedules another frame, then rebuilds the affected widget subtree.
Flutter may reuse existing elements and render objects. It does not automatically rebuild every widget in the application for every change. However, an unnecessarily broad rebuild can still create expensive work, especially when build() performs computation or creates a large subtree.
After rebuilding, Flutter performs layout and painting for objects that need them. The engine packages the resulting layer information for the raster thread. The raster thread then processes layers, images, effects, and other drawing operations before the GPU presents the result.
Why the pipeline matters for profiling
The pipeline gives you a useful starting question: which stage missed its deadline?
- Slow Dart logic, rebuilding, layout, or painting usually indicates UI-thread pressure.
- Large images, expensive effects, complex clipping, or heavy drawing can indicate raster-thread pressure.
- Some problems affect both sides, so inspect the complete frame before changing code.
Profiling is therefore a process of connecting a visible slow frame to the stage that caused it. Later, the Performance view can show the frame duration and the work recorded on each thread.
Exercise
At 60 Hz, a frame takes 20 ms on the UI thread but 4 ms on the raster thread. Does it meet the frame budget?
Show answer
No. The UI thread exceeds the approximately 16.67 ms interval, so the frame can miss its display deadline and produce jank.