Performance, under the hood · Puzzle 5 · 6 of 10

Who puts pixels on screen

Your app never touches the display. It fills a block of memory with pixels and hands it to someone else, who combines it with every other window on screen. Here's that hand-off, the path every frame takes.

The problem

On your screen right now are the status bar, your app, maybe a keyboard, and the navigation bar. Each comes from a different process. Yet the display shows one picture, 60 or 120 times a second, without tearing. Who puts it together, and what exactly does your app hand over?

See it

The RenderThread sends drawing commands to the GPU, which fills a buffer with pixels; the buffer is queued for SurfaceFlinger, which takes the latest buffer from the app, the status bar and the navigation bar and composites them into one picture for the display at vsync RenderThread GPU your buffer status barnav barSurfaceFlinger displayat vsync: one picture
  1. Your app's RenderThread sends drawing commands to the GPU, the graphics processor.

    Step 1 of 4
  2. The GPU writes the frame's pixels into a buffer, memory that holds one picture. Your app queues it.

    Step 2 of 4
  3. SurfaceFlinger takes the latest buffer from every visible window and composites them. Predict: what if your buffer isn't finished in time?

    Step 3 of 4
  4. At vsync the display shows the combined picture. No finished buffer from your app: its old picture is shown again.

    Step 4 of 4

The building block

The display redraws its picture at a fixed rate, 60 or 120 times a second. Just before each refresh it sends vsync, a signal that says "a new picture is about to be shown". Everything below happens in rhythm with it.

In the Android course you saw a frame's work: the main thread measures, lays out and records what to draw, and the RenderThread takes it from there. The RenderThread sends those drawing commands to the GPU, the graphics processor: a separate part of the chip built to fill millions of pixels at once. Android's docs put it plainly: drawing on a view's canvas uses the GPU.

The GPU writes the pixels into a buffer, a block of memory that holds one picture. Your app doesn't own the display, so it queues the finished buffer and moves on to the next frame with a fresh one.

On the other side of that queue is SurfaceFlinger, a system service. Between display refreshes, it takes the latest buffer from every visible window (your app, the status bar, the keyboard) and composites them: combines them into the one picture the display shows. It only accepts buffers between refreshes, so the screen never changes halfway through drawing.

So a frame is on time when its buffer, pixels finished by the GPU, is queued before the refresh it's meant for. Late in any step, main thread, RenderThread or GPU, and the display shows the old picture again. That's the jank the next puzzle measures.

The analogy

A newspaper's layout desk. Each reporter (an app) types their article onto their own page (a buffer), with a fast typesetter (the GPU). Before the press runs (vsync), the editor (SurfaceFlinger) takes the latest page from every reporter and pastes them into one front page. An article not ready in time? Yesterday's version runs again.

Check yourself

Your app's main thread and RenderThread finished early, but the GPU is still filling the buffer at the refresh. What does the display show?

Not quite. The pixels are written by the GPU; until it's done, there's no new buffer.

Right. No finished buffer, no new picture.

Not quite. SurfaceFlinger only takes finished buffers between refreshes, so there's no half picture.

The status bar and your app are drawn by different processes. Who combines them?

Right. One service puts every window together.

Not quite. The GPU fills buffers it's given commands for; combining them is SurfaceFlinger's job.

Not quite. Each window fills its own buffer; your app never draws the status bar.

What's the difference between vsync and a buffer?

Not quite. That's the two swapped. Re-read that part ↑

Right. The beat vs the picture.

Sources

What you now know

The display redraws at a fixed rate and sends vsync before each refresh. Your app's RenderThread gives drawing commands to the GPU, which fills a buffer with the frame's pixels. The finished buffer is queued for SurfaceFlinger, which composites the latest buffer of every visible window into what the display shows. A frame counts only once its buffer is ready when SurfaceFlinger needs it.