---
title: "Frame timing and IOSurface"
description: "How offscreen frames arrive, who owns their IOSurface, and when they reach the screen."
---

```swift
page.offscreenFrameRate = 30   // 1 to 240; Low Power Mode can lower it
page.setNeedsDisplay()         // repaint the whole view with the next frame
```

Gallery: [`OffscreenOverlayDemo`](https://github.com/Rajaniraiyn/CefSwift/blob/main/Examples/Sources/Gallery/Demos/OffscreenOverlayDemo.swift), [`AnimationDemo`](https://github.com/Rajaniraiyn/CefSwift/blob/main/Examples/Sources/Gallery/Demos/AnimationDemo.swift).

## Frame timing

Offscreen pages are created without external begin frames, so
Chromium's compositor times frames from its own timer at the page's
frame rate (`windowless_frame_rate`), and each frame arrives through
`on_accelerated_paint` as an `IOSurface`.

Driving frames from the view's display link instead
(`external_begin_frame_enabled` and `send_external_begin_frame` on every
refresh) was measured on a 60 Hz display; the Mac had no 120 Hz display.
CEF keeps one external begin frame in flight and ignores a request until
the last one is acknowledged, and a frame's deadline is the frame
interval the frame rate gives. At a frame rate of 30, CEF's default, the
acknowledgement comes after the next refresh, so every other request is
dropped: pages run 60 animation frames a second but 30 reach the screen.
At a frame rate of 60 or more, every refresh paints. A second request
4 ms after each refresh runs animation frames twice per refresh. An
offscreen page that animates, and one that follows the mouse, measured
over 3 s, twice each:

| Frame timing | Painted frames a second | Spread of paint intervals | Mouse move to painted frame, median / 90th percentile | CPU |
|---|---|---|---|---|
| Chromium's timer, rate 60 | 59.7 | 1.4–2.0 ms | 12–17 / 17–23 ms | 34–40% |
| Display link, rate 30 | 30 | 5.7–6.4 ms | 20–24 / 35–37 ms | 35–36% |
| Display link, rate 60 | 59.7 | 1.6–2.4 ms | 16–23 / 35–37 ms | 40–42% |
| Display link, rate 120 or 240 | 59.3–59.7 | 1.6–2.4 ms | 17–25 / 29–38 ms | 39–43% |

The display link matches the timer's rate and pacing and adds latency: a
mouse move waits for the next refresh, then for the main thread to send
the request. CefKit keeps Chromium's timer. `Benchmarks.frames()` in
SelfTest (`-benchmark frames`) measures both on another display.

## Frame ownership

CEF's header says an accelerated frame's `IOSurface` may only be used
during `on_accelerated_paint` and must be copied into a texture the client
owns. The Chromium 156.0.8078.88 source, with CEF 156.0.28 (`57389f0`),
shows that nothing the browser process can do keeps Chromium from
rendering into a delivered surface:

- CEF's video consumer (`libcef/browser/osr/video_consumer_osr.cc`,
  `OnFrameCaptured`) holds the capturer's callbacks in a
  `ScopedVideoFrameDone`, whose destructor calls `Done()`, and calls
  `OnAcceleratedPaint` synchronously in between. `Done()` goes out as the
  callback returns; the C API exposes no way to delay it.
- viz's `FrameSinkVideoCapturerImpl` returns the frame to its pool on
  `Done()` (`NotifyFrameReleased`) and keeps only the latest frame
  (`marked_frame_`) for resurrection. The pool for ARGB frames on macOS is
  `MappableSharedImageVideoFramePool`, over
  `media::RenderableMappableSharedImageVideoFramePool`: its
  `OnVideoFrameDestroyed` keeps at most two idle buffers
  (`kMaxAvailableFrames = 2`) and `MaybeCreateVideoFrame` hands out the
  oldest first (`front()`, `pop_front()`), checking only size and colour
  space.
- No code on that path reads `IOSurfaceIsInUse` or a use count. The one
  reader, the shared-image overlay check
  (`IOSurfaceImageBacking::OverlayRepresentation::IsInUseByWindowServer`
  in `iosurface_image_backing.mm`), returns false for images with
  `SHARED_IMAGE_USAGE_MACOS_VIDEO_TOOLBOX`, which the capture pool sets
  on every image. Holding the `IOSurfaceRef`, `IOSurfaceIncrementUseCount`
  or showing it as a layer's contents keeps the kernel object alive but
  not out of the pool.
- The page's `IOSurfaceRef` is the GPU process's surface
  (`IOSurfaceLookupFromMachPort` on the receiving side of the mojo
  handle), and `DidCopyFrame` blits each capture straight into the pooled
  image. A delivered surface is rendered into again by the capture two or
  three frames later, or the next one when the frame was not marked:
  8 to 25 ms at 120 frames a second. `Stop()`, which runs whenever the
  page is hidden, and `Start()` replace the pool.

So the surface is copied, and the copy is limited to what changed.
`CefOffscreenPageView` copies each frame on the GPU with a Metal blit
into an `IOSurface` of its own (`CefOffscreenCompositor`) and waits for
the copy before the callback returns. That surface becomes the layer's
contents. A surface is written again only when it is not the layer's
contents and `IOSurfaceIsInUse` is false: the window server holds a
replaced surface until it has composited the next refresh, about 11 ms on
a 60 Hz display, as long as the contents change has no implicit
animation.

Each frame comes with its damage, viz's `capture_update_rect`
accumulated since the previous capture, which CEF passes as the one
dirty rectangle, and `capture_counter`, its number. Each owned surface
keeps the part of it that is out of date: the union of the damage of
every frame since the surface was written. The blit copies only that.
A gap in the frame numbers, a new size or a first frame copies the whole
surface. The pool holds up to four surfaces of the frame's size, and
three when a surface is larger than 32 MB (a 5120×3200 frame is 64 MB).
A frame that finds none free is dropped, and the view asks Chromium for a
new one a refresh later. When the view stops being seen, only the surface
on screen stays. A pixel format Metal cannot wrap is copied on the CPU
under the surfaces' locks.

`SelfTest`'s `compositor` checks read the frame number a page paints into
its background from each surface as it becomes the layer's contents, and
again every 2 ms while it stays there. Presenting Chromium's surfaces
directly, one surface changed while it was the layer's contents in a
2-second resize run (1 of 237 frames); with the copies, none did in any
run. A second page changes one block of a 4×4 grid per frame, block
`f % 16` showing `f % 64`, so a presented surface is whole only if its
blocks hold 16 consecutive frame numbers; the checks read every block of
each surface the same way, at 1× and 2× and while resizing, and found no
surface that was not whole with damage-limited copies. The table below
measures whole-frame copies; [design notes](/performance/offscreen-frames#offscreen-frames-copied-and-only-where-they-changed)
compare them with damage-limited ones. `-benchmark compositor` measures
the cost on an M3 MacBook Air
(60 Hz display, offscreen frame rate 120, 3 s of animation, at least two
runs each). Surfaces of 1280×800 pixels are a 1280×800 view at 1× (a
640×400 view on this 2× display); 2560×1600 pixels are a 1280×800 view at
2× or a 2560×1600 view at 1×; 5120×3200 pixels are a 2560×1600 view at
2×:

| Surface | Presenting | Frames presented a second | App CPU | Mouse move to presented frame, median / 90th percentile | App memory while animating |
|---|---|---|---|---|---|
| 1280×800 | Chromium's surface | 119.4–119.7 | 8–19% | 15–18 / 22–24 ms | +1–6 MB |
| 1280×800 | Copy | 57–120 | 12–25% | 17–26 / 26–31 ms | +13–64 MB |
| 2560×1600 | Chromium's surface | 119.4–119.9 | 7–20% | 12–15 / 24–36 ms | +0–1 MB |
| 2560×1600 | Copy | 58–102 | 13–16% | 20–28 / 31–41 ms | +49–89 MB |
| 5120×3200 | Chromium's surface | 119.7–120.0 | 6–17% | 15–20 / 20–26 ms | +0 MB |
| 5120×3200 | Copy | 31–93 | 12–18% | 15–29 / 24–41 ms | +220–297 MB |

The display shows at most 60 of those frames a second. A 5120×3200 copy
takes 1.5–2.4 ms of GPU time and holds the main thread for 2.6–4.2 ms;
smaller copies hold it for 1.6–3.4 ms, mostly waiting for completion.
That wait holds Chromium's main thread, so Chromium delivers fewer frames.
Chromium's surfaces count toward the GPU process, the copies toward the
app.
