Repository navigation
Description
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::SetupRenderPasscallsrenderer_.GetRenderTargetCache()->Start().Canvas::EndReplaycallsrenderer_.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.ccfortoImage/toImageSync,Rasterizer::ScreenshotLayerTreeAsImage)
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:
- Call
RenderToTargetwith a display list containing asaveLayer. RecordRenderTargetCache::CachedTextureCount()and the allocation count (e.g. a countingAllocatorwrapper). - Call
DisplayListToTexture5 times with a display list of a different size. - Call
RenderToTargetwith the first display list again. - Expected: no new allocations for the
saveLayertarget 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:
- Split the API. Keep
Start()for clearingused_this_frame. Move the keep-alive decrement and eviction into a separateAdvanceFrame()(or a parameter onEnd(bool advance_frame)), called only for onscreen rendering (is_onscreen_inCanvas, or by the surfaces /Rasterizeronce per frame). - Count in onscreen frames. Store a "last used frame number" per entry and evict on
current_frame - last_used > keep_alive, wherecurrent_frameonly 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
- [Impeller] Picture.toImageSync allocates a new GPU texture on every call; chained offscreen passes spend ~30% of the raster thread creating textures #193941:
Picture.toImageSyncallocates a new texture on every call (this bug makes multi-snapshot frames even more expensive) - [Impeller] Make the render target cache configurable #190613: render target cache configuration
- Adds memory budget to RenderTargetCache #193027: memory budget for
RenderTargetCache - [Impeller] Reuse and resize offscreen RenderTargetCache textures in place on OpenGLES #194122: reuse/resize offscreen
RenderTargetCachetextures on OpenGLES
Notes / open questions
- Platform views: On iOS, each overlay layer is rendered through its own
RenderToTargetcall, so a frame with N overlays probably ticks the cache N+1 times. Not verified. - Other per-frame caches:
DisplayListToTexturealso callsTextShadowCache::MarkFrameStart/MarkFrameEndfor 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
[Impeller] Reuse render targets on text shadow cache misses (flutter#…