Skip to content

[Impeller] Off-frame rendering advances RenderTargetCache's frame clock, evicting onscreen saveLayer targets #194307

Open

Description

@gaaclarke

I noticed this behavior too the other day when looking into #190613 but forgot to file an issue. Here's gemini's writeup.

Summary

The keep-alive in RenderTargetCache is counted in calls to End(). One onscreen frame is supposed to be one tick. In practice, every impeller::Canvas calls Start()/End() on the ContentContext's single shared cache. That includes the canvases used for Picture.toImageSync / toImage, Scene.toImage, and screenshots. So every snapshot counts as a frame.

With the default keep_alive_frame_count = 4, if more than 4 snapshots run between two onscreen frames, the onscreen frame's cached saveLayer render targets are evicted. The next frame has to allocate them again. Apps that make several snapshots per frame (multi-pass effects, toImageSync chains) and also use saveLayer onscreen pay for new texture allocations every frame, even though their render targets never change.

Where the cache ticks

  • Canvas::SetupRenderPass calls renderer_.GetRenderTargetCache()->Start().
  • Canvas::EndReplay calls renderer_.GetRenderTargetCache()->End().
  • Both run for every Canvas, whatever it renders to. Callers include:
    • RenderToTarget (onscreen surfaces: gpu_surface_metal_impeller.mm, gpu_surface_vulkan_impeller.cc, gpu_surface_gl_impeller.cc, embedder_external_view_embedder.cc)
    • DisplayListToTexture (snapshot_controller_impeller.cc for toImage/toImageSync, Rasterizer::ScreenshotLayerTreeAsImage)

RenderTargetCache::End():

for (RenderTargetData& td : render_target_data_) {
  if (td.used_this_frame) {
    retain.push_back(td);
  } else if (td.keep_alive_frame_count > 0) {
    td.keep_alive_frame_count--;   // decremented once per Canvas, not per frame
    retain.push_back(td);
  }
}

Expected

Cached render targets should survive keep_alive_frame_count onscreen frames, no matter how many snapshots run in between.

Actual

Each snapshot Canvas that doesn't use a cached entry decrements that entry's keep-alive. Here S is the number of snapshots between two onscreen frames:

Snapshots between frames Effect on onscreen saveLayer targets
S ≤ 4 Kept (the keep-alive resets to 4 whenever an entry is used)
S ≥ 5 Evicted before the next frame, then reallocated

The snapshots get no benefit in return: they can only reuse an entry whose size and config match exactly.

Steps to reproduce

The Dart sample from #193941 almost reproduces this. That sample's layer and snapshot modes need to be combined, so that one frame has both onscreen saveLayers and ≥5 toImageSync calls:

@override
void paint(Canvas canvas, Size size) {
  // >4 snapshots per frame (the results don't even need to be drawn).
  for (int i = 0; i < 8; i++) {
    final ui.PictureRecorder r = ui.PictureRecorder();
    Canvas(r).drawRect(const Rect.fromLTWH(0, 0, 64, 64), Paint());
    final ui.Picture p = r.endRecording();
    p.toImageSync(64, 64).dispose();
    p.dispose();
  }
  // Onscreen saveLayers whose render targets should come from the cache.
  for (int i = 0; i < 8; i++) {
    canvas.saveLayer(Offset.zero & size, Paint());
    canvas.drawRect(Rect.fromLTWH(i * 4.0, 0, 40, 40),
        Paint()..color = const Color(0x80FF0000));
  }
  for (int i = 0; i < 8; i++) {
    canvas.restore();
  }
}

Profile the raster thread. On iOS, look for -[AGXTexture initWithDevice:…] / IOGPUResourceCreate under the onscreen frame's saveLayer allocations. They show up every frame with 8 snapshots and go away when the snapshot loop drops to ≤4 iterations.

Suggested engine-level test

In impeller/display_list (e.g. aiks_dl_unittests.cc or a new test), using one AiksContext:

  1. Call RenderToTarget with a display list containing a saveLayer. Record RenderTargetCache::CachedTextureCount() and the allocation count (e.g. a counting Allocator wrapper).
  2. Call DisplayListToTexture 5 times with a display list of a different size.
  3. Call RenderToTarget with the first display list again.
  4. Expected: no new allocations for the saveLayer target in step 3. Today: the target is reallocated.

Possible fixes

The frame clock should be driven only by onscreen frame boundaries. Off-frame canvases still need to reset used_this_frame so they can reuse entries among themselves. For example, the 32 passes of a toImageSync chain should share one set of transient MSAA/depth-stencil textures if those ever come from the cache (see #193941).

Options:

  1. Split the API. Keep Start() for clearing used_this_frame. Move the keep-alive decrement and eviction into a separate AdvanceFrame() (or a parameter on End(bool advance_frame)), called only for onscreen rendering (is_onscreen_ in Canvas, or by the surfaces / Rasterizer once per frame).
  2. Count in onscreen frames. Store a "last used frame number" per entry and evict on current_frame - last_used > keep_alive, where current_frame only advances on onscreen frames.

Warning

Simply skipping Start()/End() for offscreen canvases is not a correct fix. Without Start(), the first snapshot's entries stay marked used_this_frame until the next onscreen frame. Every later snapshot in that frame would then allocate a new entry, growing the cache instead of reusing it.

Related

Notes / open questions

  • Platform views: On iOS, each overlay layer is rendered through its own RenderToTarget call, so a frame with N overlays probably ticks the cache N+1 times. Not verified.
  • Other per-frame caches: DisplayListToTexture also calls TextShadowCache::MarkFrameStart/MarkFrameEnd for each snapshot, so it may age off-frame in the same way. Worth checking with the same fix.
  • Unverified: This is from reading the code on master @ 0e4dea4. I haven't profiled it.

Activity

  1. added
    team-engine
    P2
    e: impeller
    triaged-engine
    and removed
    team-engine
    P2
    e: impeller
    on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2e: impellerteam-enginetriaged-engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions