Repository navigation
feat(🗿): one recorder per offscreen surface, add Skia.purgeGpuResources() - #4520
Conversation
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.
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.
…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.
- 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.
Agent-written content: memory measurements on 7e98fb5Two commits since the description was written, the counterpart of wcandillon/react-native-webgpu#649:
Samsung S25 Edge (Android 17, Adreno 830), debug buildExample app on the e2e screen. Offscreen surfaces are made, drawn into (a blurred circle), flushed, then disposed of. Total PSS from Foreground
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)
iPhone 14 Pro (iOS 27.0.1), debug buildPhysical footprint of the app in MB, recorded from the host while the arms ran (
In the control arms the purge is the cleanup at the end of the arm. On Metal, 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, E2E on 7e98fb5
Not measured: Mali, frame-time cost. |
… 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.
|
🎉 This PR is included in version 3.13.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
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);JsiSkDispatcheris gone.Skia.purgeGpuResources()replacespurgeThreadGpuCache(). 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 AndroidonLowMemoryandonTrimMemoryfromTRIM_MEMORY_BACKGROUND(Android 14+ never callsonLowMemory).Trim after submits. Threads outside the render pool free what went unused for 2 s.
drawAsImageanduseTexturedispose their surface after the snapshot.Release note
Drawing
a.asImage()intobnow requiresa.flush()first (as theasImage()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:
maintoo;mainitself was not built), 0 after.0900205a4, 3 rounds, surfaces disposed first): GL memory 366 to 157 MB withpurgeGpuResources()and 366 to 149-153 MB with a simulatedonTrimMemory; 366 MB without a purge, 364 MB in the background only. PSS 950 to 739 MB with the purge, 950 MB without.631f6efd7, confirmed again by one trace at0900205a4: UIKit's warning reachesReduceMemoryUsageoff the main thread.0900205a4): Samsung 462 pass, 3 fail (knownParagraphs); iOS simulator 462 pass, 2 fail (known). The purge spec, added after, passes 3/3 on both.Not verified: memory on iOS (Dawn's Metal allocator reports 0),
onLowMemoryfiring,onTrimMemorysent 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.