1. Architecture & Rendering

Rendering Pipeline & Frame Lifecycle

Explore the 4-stage Flutter rendering pipeline: Animate, Build, Layout, Paint, and Composite. Understand how VSYNC signals, Tickers, and single-pass layout algorithms guarantee smooth 60fps and 120fps display performance.

1. The Frame Lifecycle & VSYNC Signal

To achieve smooth 60 FPS (frames per second) or 120 FPS (ProMotion / high refresh rate displays), Flutter must complete all UI computations within a strict frame budget:

  • 60 Hz Display: 1000ms / 60 ≈ 16.6ms per frame.
  • 120 Hz Display: 1000ms / 120 ≈ 8.3ms per frame.
VSYNC Hardware Interrupt Trigger 2. Build: Dirty Elements re-execute build() 3. Layout: Constraints down, Sizes up 4. Paint: RenderObjects record canvas commands 5. Composite: Layer tree sent to GPU for rasterization Display buffer presented on physical screen 1. Animate: Tickers advance state Platform OS / VSYNC Clock SchedulerBinding Build Phase (Widget Tree) Layout Phase (RenderObjects) Paint Phase (Layers & Canvases) Impeller / GPU Rasterizer

2. The 4-Stage Rendering Pipeline

When state changes or an animation advances, Flutter executes four strict sequential phases:

┌──────────────┐     ┌──────────────┐     ┌──────────────┐     ┌──────────────┐
│ 1. ANIMATE   │ ──> │   2. BUILD   │ ──> │  3. LAYOUT   │ ──> │   4. PAINT   │ ──> [ 5. COMPOSITE ]
│ Tickers tick │     │ build() runs │     │  Sizes calc  │     │ Canvas draw  │     GPU merges layers
└──────────────┘     └──────────────┘     └──────────────┘     └──────────────┘

1. Animate Phase

  • Triggered by Ticker objects tied to the device's hardware VSYNC refresh rate.
  • AnimationController interpolates Tween values and calls registered listeners.

2. Build Phase

  • All Element instances marked as dirty (via setState(), InheritedWidget updates, or hot reload) re-invoke their associated Widget.build() methods.
  • Produces the updated declarative widget tree and reconciles changes against the persistent Element tree.

3. Layout Phase ($O(N)$ Single-Pass)

  • The framework walks the RenderObject hierarchy.
  • Constraints go down: Parents pass min/max width and height constraints down to children.
  • Sizes go up: Children determine their exact size within those constraints and report their dimensions back to the parent.
  • Flutter strictly enforces single-pass layout ($O(N)$ linear complexity). Unlike traditional web browsers that may perform costly multi-pass recursive layouts, a RenderObject in Flutter is visited only once during standard layout.

4. Paint Phase

  • RenderObjects record drawing operations onto a PictureRecorder (e.g., canvas.drawRect(), canvas.drawParagraph()).
  • Drawing calls are not immediately rasterized to physical pixels; instead, they generate a tree of display list commands grouped into Layers.

5. Compositing Phase

  • The Layer tree is packaged and transferred across the C++ boundary to the Flutter Engine.
  • The GPU backend (Impeller) rasterizes the layers into display memory.

3. The Single-Pass Layout Protocol in Action

In Flutter, layout negotiation between parent and child follows a strict contract defined on RenderBox:

// Conceptual layout execution inside Flutter's RenderBox
@override
void performLayout() {
  // 1. Parent inspects incoming constraints (BoxConstraints)
  // 2. Parent lays out child with modified constraints
  if (child != null) {
    child!.layout(
      BoxConstraints(
        minWidth: 0.0,
        maxWidth: constraints.maxWidth,
        minHeight: 0.0,
        maxHeight: constraints.maxHeight,
      ),
      parentUsesSize: true, // Tells parent to track child's computed size
    );

    // 3. Parent sets its own size based on child's size
    size = constraints.constrain(Size(child!.size.width + 20, child!.size.height + 20));

    // 4. Parent positions child using BoxParentData
    final BoxParentData childParentData = child!.parentData! as BoxParentData;
    childParentData.offset = const Offset(10.0, 10.0);
  } else {
    size = constraints.smallest;
  }
}
Constraint flow:
Parent BoxConstraints(minW: 0, maxW: 400, minH: 0, maxH: 800)
    │
    ▼ (Passed down)
Child determines size -> Size(250, 100)
    │
    ▲ (Returned up)
Parent receives Size(250, 100) -> Positions child at Offset(75, 350)

4. Repaint Boundaries: Isolating Render Subtrees

When a widget frequently animates (e.g., a progress spinner, pulse animation, or scrolling list), painting the entire screen on every frame wastes valuable GPU time. A RepaintBoundary creates an isolated visual layer that prevents repaint cascading.

// Without RepaintBoundary: Animating the spinner repaints the entire complex dashboard!
Column(
  children: [
    CircularProgressIndicator(),
    ExpensiveToPaintChart(), // ⚠️ Repainted every 16ms unnecessarily
  ],
);

// With RepaintBoundary: Only the spinner's isolated layer repaints
Column(
  children: [
    const RepaintBoundary(
      child: CircularProgressIndicator(), // ✅ Isolated onto its own GPU layer
    ),
    const RepaintBoundary(
      child: ExpensiveToPaintChart(),     // ✅ Cached layer; zero CPU paint calls
    ),
  ],
);

When to Use RepaintBoundary

ScenarioRecommended ActionPerformance Impact
High-frequency animation widgetWrap in RepaintBoundaryIsolates frequent repaints to a single small texture.
Complex static canvas or vector graphicWrap in RepaintBoundaryRasterized once and cached on GPU memory.
Trivial text labels / simple buttonsDo NOT wrapExtra layers introduce memory overhead and compositing cost.

❓ Knowledge Check

Knowledge Check

What is the primary advantage of Flutter's single-pass layout algorithm compared to traditional web layout engines?

Knowledge Check

What does a RepaintBoundary widget do to improve rendering performance during animations?

On this page