Skip to content

feat(🗿): one recorder per offscreen surface, add Skia.purgeGpuResources() - #4520

Merged
wcandillon merged 19 commits into
mainfrom
claude/wonderful-bohr-bktsjj
Nov 25, 2026
Merged

wcandillon merged 19 commits into
mainfrom
claude/wonderful-bohr-bktsjj

Conversation

@wcandillon

@wcandillon wcandillon commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Description

One recorder per offscreen surface. A Graphite recorder has a single owner, but an offscreen surface used the recorder of the thread that made it. A surface made on the JS thread and drawn by a UI worklet shared its recorder with whatever the JS thread recorded or purged, and crashed in ResourceCache. Each surface now owns its recorder (budget scaled to its size, 32 to 256 MB); JsiSkDispatcher is gone.

Skia.purgeGpuResources() replaces purgeThreadGpuCache(). It trims the caller's recorders, then asks Dawn on a background thread to give its pooled heaps back to the system. It also runs on iOS memory warnings, and on Android onLowMemory and onTrimMemory from TRIM_MEMORY_BACKGROUND (Android 14+ never calls onLowMemory).

Trim after submits. Threads outside the render pool free what went unused for 2 s.

drawAsImage and useTexture dispose their surface after the snapshot.

Release note

Drawing a.asImage() into b now requires a.flush() first (as the asImage() docs say). It used to work without when both surfaces were made on the same thread.

Agent-written content

Measured on a Samsung S25 Edge unless noted:

  • Crash: JS-made surface drawn on the UI runtime while JS purges or draws another surface: 12/12 crashed before (trim off included, so likely on main too; main itself was not built), 0 after.
  • Cost: create/draw/flush/dispose 127-134 to 209-230 µs; 50 live surfaces 60 to 206 MB of GL memory (about 2.9 MB each). No per-surface leak over 40 cycles.
  • Purge (native code at 0900205a4, 3 rounds, surfaces disposed first): GL memory 366 to 157 MB with purgeGpuResources() and 366 to 149-153 MB with a simulated onTrimMemory; 366 MB without a purge, 364 MB in the background only. PSS 950 to 739 MB with the purge, 950 MB without.
  • iOS memory warning (simulator, ASan, lldb): 3/3 at 631f6efd7, confirmed again by one trace at 0900205a4: UIKit's warning reaches ReduceMemoryUsage off the main thread.
  • E2E (at 0900205a4): Samsung 462 pass, 3 fail (known Paragraphs); iOS simulator 462 pass, 2 fail (known). The purge spec, added after, passes 3/3 on both.
  • Jest: 952 pass.

Not verified: memory on iOS (Dawn's Metal allocator reports 0), onLowMemory firing, onTrimMemory sent by the system rather than adb, frame times during a purge on the final sources, device loss during a purge, Mali. Disposing a surface while a worklet draws into it stays unsupported.

The JS thread and worklet runtimes record offscreen surfaces on recorders
of their own, which nothing trimmed: their scratch textures stayed cached
up to Graphite's budget for the life of the thread. They now trim what went
unused for RNSkThreadPool::kIdleDelay after their submits, at most once per
delay, and the JS thread does so once more after releasing a surface, when
no submit may follow.
… trim

Freeing what a thread caches once it is done drawing is the caller's call:
it knows when an export ends, and it works on worklet runtimes too, which
the delayed JS-thread trim could not reach. The trim after submits stays,
so that the cache does not grow while drawing.
@wcandillon wcandillon changed the title Add GPU resource trimming for non-pool threads feat(graphite): trim the recorders of threads outside the render pool, add Skia.freeGpuResources() Nov 24, 2026
The name says what the call touches (the calling thread's cache, never
what is in use), and the docs spell out what it frees, what it leaves,
and when it is worth calling.
@wcandillon wcandillon changed the title feat(graphite): trim the recorders of threads outside the render pool, add Skia.freeGpuResources() feat(graphite): trim the recorders of threads outside the render pool, add Skia.purgeThreadGpuCache() Nov 24, 2026
@wcandillon wcandillon changed the title feat(graphite): trim the recorders of threads outside the render pool, add Skia.purgeThreadGpuCache() feat(🗿): trim the recorders of threads outside the render pool, add Skia.purgeThreadGpuCache() Nov 24, 2026
…Resources()

A Graphite recorder has a single owner, but an offscreen surface used the
recorder of the thread that made it: a surface made on the JS thread and drawn
by a UI worklet shared its recorder with whatever the JS thread recorded or
purged, and crashed in ResourceCache. Each offscreen surface now owns its
recorder, with a budget scaled to its size, and JsiSkDispatcher is gone.

Skia.purgeThreadGpuCache() becomes Skia.purgeGpuResources(): it trims the
caller's recorders, then asks Dawn in the background to give its pooled heaps
back to the system. It also runs on iOS memory warnings, and on Android
onLowMemory and onTrimMemory from TRIM_MEMORY_BACKGROUND.

Drawing a.asImage() into b now requires a.flush() first.
@wcandillon wcandillon changed the title feat(🗿): trim the recorders of threads outside the render pool, add Skia.purgeThreadGpuCache() feat(🗿): one recorder per offscreen surface, add Skia.purgeGpuResources() Nov 24, 2026
- dispose() submits what was drawn into the surface and not flushed
- a purge checks again for a device JS destroyed meanwhile; Dawn patch for
  the window left (ships with the next binaries)
- a purge deferred in the background is no longer missed on return
- document flushing a surface before drawing its asImage() elsewhere
RNDawnContext.h keeps small primitives only: makeRecorder(budget,
&generation), submitRecording(recorder, generation, sync), one pass of
reduceMemoryUsage(), peekInstance() and waitForForeground().

- RNSkSurfaceRecorder.h: the recorder an offscreen surface owns, with
  flush(), flushIfDirty() and markDirty().
- RNSkGpuPurge.h: Skia.purgeGpuResources() and the platform's memory
  warnings, on a thread of its own.

On iOS, a purge asked for in the background now waits for the foreground
on a condition variable, instead of being asked for again by
setForeground().
The pinned Dawn (1716e88cc9) checks for a lost device itself.
…o RNSkGpuPurge

- trimThreadRecorders(unusedFor, everything) was two operations behind a
  boolean. trimThreadRecorders(unusedFor) is the routine, by age;
  purgeThreadRecorders() frees all the recorders of a thread can let go of
  (an idle render thread, Skia.purgeGpuResources()).
- The platform reports what the system said (onMemoryWarning, onLowMemory,
  onTrimMemory) and RNSkGpuPurge decides what calls for a purge, as GpuPurge
  does in react-native-webgpu. The threshold is no longer in Java.
- The pass count and the delays of a purge, the trim level, and the age a
  busy render thread trims at are named constants.
- The surface recorder calls its throttle a trim, as the thread recorders
  do.

No change in behavior.
On a Galaxy S25 Edge (Android 17) the system only sends
TRIM_MEMORY_UI_HIDDEN when the app leaves the foreground.
TRIM_MEMORY_BACKGROUND never came, with the app cached and frozen for a
minute (measured in wcandillon/react-native-webgpu#649), so the purge never
ran on its own. From TRIM_MEMORY_UI_HIDDEN, going home with 100 disposed
512x512 surfaces gives 260 MB back within 3 s.

Chrome, Flutter on iOS and Android's own renderer also free their GPU
caches when the app is hidden.
@wcandillon

Copy link
Copy Markdown
Owner Author

Agent-written content: memory measurements on 7e98fb5

Two commits since the description was written, the counterpart of wcandillon/react-native-webgpu#649:

  • 0ecca4a splits trimThreadRecorders(unusedFor, everything) into trimThreadRecorders(unusedFor) and purgeThreadRecorders(), and has the platform report what the system said to RNSkGpuPurge (onMemoryWarning, onLowMemory, onTrimMemory). No change in behavior.
  • 7e98fb5 purges from TRIM_MEMORY_UI_HIDDEN on Android instead of TRIM_MEMORY_BACKGROUND. On the Samsung below the system only ever sends level 20, each time the app leaves the foreground; level 40 never came, with the app cached and frozen for a minute (measured in feat(🧹): add purgeGpuResources(), and purge when the system is short of memory react-native-webgpu#649). The trigger line of the description is out of date on that point.

Samsung S25 Edge (Android 17, Adreno 830), debug build

Example app on the e2e screen. Offscreen surfaces are made, drawn into (a blurred circle), flushed, then disposed of. Total PSS from dumpsys meminfo, in MB, sampled from the host. Baseline is 735.

Foreground

after dispose() 3 s later 6 s later 10 s later
512² × 100 surfaces, control 948 947 947 947
512² × 100 surfaces, Skia.purgeGpuResources() 948 737 737
1024² × 40 surfaces, control 901 901 902 902
1024² × 40 surfaces, Skia.purgeGpuResources() 902 737 737

Each control arm was purged afterwards and came back to 736.

Background, nothing called from JS (512² × 100 surfaces disposed of, then the home button)

after dispose() 3 s after home 10 s after home
7e98fb5 (TRIM_MEMORY_UI_HIDDEN) 949 687 687

iPhone 14 Pro (iOS 27.0.1), debug build

Physical footprint of the app in MB, recorded from the host while the arms ran (xctrace, Activity Monitor template, one sample a second). Baseline is 478 to 484.

allocated after dispose() 10 s later 6 s after Skia.purgeGpuResources()
512² × 100 surfaces, control 595 489 486 484
512² × 100 surfaces, purge 606 485 478
1024² × 40 surfaces, control 688 553 553 480
1024² × 40 surfaces, purge 668 535 488

In the control arms the purge is the cleanup at the end of the arm. On Metal, dispose() gives most of the memory back at once. With the larger surfaces 50 to 70 MB stays until a purge.

iOS simulator (iOS 26.5, debug build with ASan)

Surface memory does not show in the footprint of a simulator process (flat at 649 to 654 MB through every arm), so there is nothing to read there.

Memory warning (lldb, _performMemoryWarning): RNSkGpuPurge::onMemoryWarning() is reached, then one run: two rounds of passes (DawnContext::reduceMemoryUsage() and dawn::native::ReduceMemoryUsage twice each), and DawnContext::purgeThreadRecorders() on the four idle render threads.

E2E on 7e98fb5

  • Samsung: 464 pass, 7 skipped, 1 fails (Paragraphs, "Should build the first example from the documentation"), which fails the same way on dc2b481 without these two commits.
  • iOS simulator: 464 pass, 7 skipped, 1 fails (the known ParagraphGlyphs diff).

Not measured: Mali, frame-time cost. waitForForeground() in the purge is unchanged.

… merged

main rewords the end of the sentence this branch had reflowed (#4531), and
the two changes conflict. With the original lines back, GitHub can merge
main into the branch; the commit after the merge puts this branch's wording
back.
…read

Each offscreen surface has its own recorder on this branch, so the clause
that came back for the merge of main goes again. The end of the sentence is
main's (#4531).
…site

It is meant for one rare case, and the purge now runs on its own when the
app is hidden or the system is short of memory. The function stays, and its
doc comment says when to call it and when not to.
…merging main

kThreadTrimDelay and trimThreadIfDue() move next to trimThreadRecorders()
and purgeThreadRecorders(): main adds submitRecordingAsync() (#4532) where
they were.

Two lines also change place for the time of the merge, so that GitHub can
make it without a conflict: JsiSkSurface.h gets RNSkSurfaceRecorder.h
through JsiSkCanvas.h, and the imports of Surfaces.spec.tsx are in another
order. The commit after the merge puts them back.
Surface.flushAsync() (#4532) came with the merge of main written for the
recorder of a thread, which a surface no longer uses on this branch, and
for the dispatcher this branch removed, so the merge commit does not build
on its own:

- RNSkSurfaceRecorder::flushAsync() flushes the surface's own recorder, as
  flush() does, and trims after it.
- DawnContext::submitRecordingAsync() takes the generation that recorder
  was made for, as submitRecording() does. Without it the draws of every
  surface would be reported lost.
- Its documentation says the promise covers the draws of the surface: each
  surface submits its own here.
@wcandillon
wcandillon merged commit 1fad470 into main Nov 25, 2026
12 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 3.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant