Frontend / GPU compositing

GPU compositing

Browsers can keep parts of a page as separate pictures and let the graphics chip slide them around. See how layers form, why that is smooth, and what it costs.

Advanced7 steps~6 min
Page layer — the picture of everything else
Card layer — an element with its own picture
GPU — stacks the layers for every frame
// THE SHORT VERSION

In 30 seconds

A browser can paint an element into its own picture, called a layer, and hand it to the graphics processor (GPU). The GPU then builds each frame by stacking the layers: this is compositing.

Moving or fading a layer changes only where and how its picture is drawn, so nothing is laid out or painted again. That is why animating transform and opacity stays smooth.

Layers are not free. Each one is stored as raw pixels, 4 bytes apiece, so promoting too many elements can use up graphics memory and slow the page down.

GPU compositing, step by step

STEPS01 / 07

Simplified: browsers split big layers into tiles, can skip storing solid-colour ones, and choose what to promote with rules that vary by engine and version. MB here means one million bytes.

STEP 01 / 07 One page, one picture
THE MATH
1 layer = 1 painted picture move the card: paint again
  1. Step 1: One page, one picture. By default a browser paints a page the way you would paint a wall: everything ends up in one flat picture. The red card is just pixels in that picture, mixed in with the text around it. To move the card, the browser has to paint that part of the picture again, on every single frame.
  2. Step 2: A layer of its own. The browser can lift an element out of the page picture and paint it into a separate picture of its own, called a layer. Each layer is handed to the graphics processor, the GPU, which keeps it in memory as a texture. On screen nothing looks different: the page is now two pictures, one resting on the other.
  3. Step 3: The GPU stacks the layers. Putting the layers together is called compositing. For every frame the GPU draws each stored picture in order, at its position and with its opacity: the page first, then the card on top. This is the work graphics chips are built for. Nothing is laid out or painted again; the pictures are only placed.
  4. Step 4: Why transform is smooth. Animate left and every frame needs layout, paint and compositing, queued behind whatever JavaScript is running. Animate transform instead and the card’s picture never changes: the GPU just draws it somewhere else. Opacity is the same trick, blending the picture more or less strongly. With no layout or paint, both stay smooth.
  5. Step 5: What earns a layer. Browsers decide which elements get a layer, by rules that differ a little between engines. Common triggers: a will-change hint, a 3D transform, a running animation of transform or opacity, video and canvas, and often fixed positioning. An element that overlaps a layer may be promoted too, so that it can be drawn on top.
  6. Step 6: Every layer costs memory. A layer is stored as raw pixels, and each pixel takes 4 bytes: red, green, blue and transparency. A layer of 1000 × 500 pixels therefore needs 2 MB of graphics memory, however simple it looks. On a high-density 2× screen the same element covers four times as many pixels, so it needs 8 MB.
  7. Step 7: Too many layers. So layers are a trade, not a free speed-up. Promote a hundred cards of that size and they hold 200 MB of graphics memory, and the GPU has a hundred pictures to place on every frame. On a phone that can make a page slower, or get it killed. Promote only what is about to animate, and drop the hint when it stops.
VALUES SO FAR
layers1default
layers····promoted
frame····GPU
left····slow
cheap····composite
cheap····composite
memory····per layer
× 100····cost

Spotted a mistake on this page?