Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
be17e5b
feat(graphite): trim the recorders of threads outside the render pool
wcandillon Nov 24, 2026
5d53d1e
feat(graphite): add Skia.freeGpuResources() in place of the JS-thread…
wcandillon Nov 24, 2026
b76f2b4
feat(graphite): rename freeGpuResources to purgeThreadGpuCache
wcandillon Nov 24, 2026
631f6ef
feat(graphite): one recorder per offscreen surface, add Skia.purgeGpu…
wcandillon Nov 24, 2026
779a043
Merge branch 'main' into claude/wonderful-bohr-bktsjj
wcandillon Nov 24, 2026
778ca6b
fix(graphite): address the review of offscreen surfaces and GPU purges
wcandillon Nov 24, 2026
8390841
Merge branch 'main' into claude/wonderful-bohr-bktsjj
wcandillon Nov 24, 2026
0900205
refactor(graphite): surface recorder and GPU purge out of DawnContext
wcandillon Nov 24, 2026
509fef2
test(graphite): e2e for purgeGpuResources, narrow its documentation
wcandillon Nov 25, 2026
dc2b481
chore(graphite): drop the ReduceMemoryUsage Dawn patch
wcandillon Nov 25, 2026
0ecca4a
refactor(graphite): split trimThreadRecorders, report memory events t…
wcandillon Nov 25, 2026
7e98fb5
feat(graphite): purge when the app goes to the background on Android
wcandillon Nov 25, 2026
ff00a8d
docs(graphite): keep main's sentence in submitRecording until main is…
wcandillon Nov 25, 2026
32a8480
Merge branch 'main' into claude/wonderful-bohr-bktsjj
wcandillon Nov 25, 2026
61abc14
docs(graphite): submitRecording no longer serves the surfaces of a th…
wcandillon Nov 25, 2026
84ded89
docs(graphite): do not document Skia.purgeGpuResources() on the docs …
wcandillon Nov 25, 2026
8aed4a3
refactor(graphite): group the trim functions of DawnContext ahead of …
wcandillon Nov 25, 2026
d3e16d4
Merge branch 'main' into claude/wonderful-bohr-bktsjj
wcandillon Nov 25, 2026
17fb67e
fix(graphite): flushAsync() on a surface that owns its recorder
wcandillon Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/docs/docs/animations/textures.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ const Demo = () => {
};
```

Flush the surface before drawing its image into another surface too: each surface submits its own drawing only, so flushing the second does not flush the first, and the order of the flushes is the order on the GPU.

Never draw the image onto its own surface: use a second surface for a feedback loop.

:::info
Expand All @@ -196,4 +198,13 @@ Read the snapshot back with `makeRasterImage()` before drawing it in a canvas. T

:::

## GPU memory of offscreen surfaces

Each offscreen surface has a GPU cache of its own, for the scratch textures and atlases its drawing needs, so it can be drawn into on another thread than the one that made it.
A surface must only be used by one thread at a time.

Skia frees what the cache has not used for a few seconds as you draw into the surface. The rest goes with the surface: call `dispose()` once you are done with it. Keep a snapshot (`makeImageSnapshot()`) if you still need its content.

Disposed surfaces leave their memory to Skia, for the next ones.

Textures can also be shared with WebGPU: see [WebGPU](/docs/webgpu).
2 changes: 1 addition & 1 deletion apps/docs/docs/webgpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const drawFrame = async () => {
};
```

The promise covers the draws pending on the thread, as the GPU surfaces of a thread are submitted together.
The promise covers the draws of this surface: each surface submits its own drawing.
While the app is in the background, it settles on return.

On Android, `Skia.gpu.foreground` is whether the host activity is resumed; on the web, whether the document is visible.
Expand Down
14 changes: 14 additions & 0 deletions packages/skia/android/cpp/jni/JniPlatformContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "JniPresentThread.h"
#include "RNDawnContext.h"
#include "RNSkGpuPurge.h"
#include "RNSkPresentThread.h"

namespace RNSkia {
Expand All @@ -37,6 +38,10 @@ void JniPlatformContext::registerNatives() {
JniPlatformContext::runMainThreadTasks),
makeNativeMethod("setForegroundNative",
JniPlatformContext::setForegroundNative),
makeNativeMethod("onLowMemoryNative",
JniPlatformContext::onLowMemoryNative),
makeNativeMethod("onTrimMemoryNative",
JniPlatformContext::onTrimMemoryNative),
makeNativeMethod("setCacheDirectoryNative",
JniPlatformContext::setCacheDirectoryNative),
makeNativeMethod("setPixelDensityNative",
Expand Down Expand Up @@ -97,6 +102,15 @@ void JniPlatformContext::setForegroundNative(jni::alias_ref<jclass>,
DawnContext::setForeground(foreground == JNI_TRUE);
}

void JniPlatformContext::onLowMemoryNative(jni::alias_ref<jclass>) {
RNSkGpuPurge::onLowMemory();
}

void JniPlatformContext::onTrimMemoryNative(jni::alias_ref<jclass>,
jint level) {
RNSkGpuPurge::onTrimMemory(level);
}

void JniPlatformContext::setCacheDirectoryNative(
jni::alias_ref<jclass>, jni::alias_ref<jni::JString> directory) {
if (directory) {
Expand Down
5 changes: 5 additions & 0 deletions packages/skia/android/cpp/jni/include/JniPlatformContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class JniPlatformContext : public jni::HybridClass<JniPlatformContext> {
// and outlives it.
static void setForegroundNative(jni::alias_ref<jclass>, jboolean foreground);

// What the system says about memory, as it says it: RNSkGpuPurge decides
// what calls for a purge.
static void onLowMemoryNative(jni::alias_ref<jclass>);
static void onTrimMemoryNative(jni::alias_ref<jclass>, jint level);

static void setCacheDirectoryNative(jni::alias_ref<jclass>,
jni::alias_ref<jni::JString> directory);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.reactnative.skia;

import android.content.ComponentCallbacks;
import android.content.ComponentCallbacks2;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Looper;
Expand Down Expand Up @@ -35,15 +35,23 @@ public class PlatformContext implements LifecycleEventListener {
private static ParcelFileDescriptor sMainThreadWakeup;

// The display density can change while the app runs (display size
// setting, foldables): the native side follows it.
private final ComponentCallbacks mComponentCallbacks = new ComponentCallbacks() {
// setting, foldables): the native side follows it. What the system says
// about memory goes to the native side as it comes, which decides when
// the GPU memory nothing uses goes back to the system (RNSkGpuPurge).
private final ComponentCallbacks2 mComponentCallbacks = new ComponentCallbacks2() {
@Override
public void onConfigurationChanged(Configuration newConfig) {
updatePixelDensity();
}

@Override
public void onLowMemory() {
onLowMemoryNative();
}

@Override
public void onTrimMemory(int level) {
onTrimMemoryNative(level);
}
};

Expand Down Expand Up @@ -172,6 +180,12 @@ public void run() {
// Whether the app is in the foreground (DawnContext::setForeground).
private static native void setForegroundNative(boolean foreground);

// What the system says about memory (RNSkGpuPurge::onLowMemory,
// RNSkGpuPurge::onTrimMemory).
private static native void onLowMemoryNative();

private static native void onTrimMemoryNative(int level);

// Where Dawn's blob cache keeps its entries (DawnContext::setCacheDirectory).
private static native void setCacheDirectoryNative(String directory);
}
7 changes: 7 additions & 0 deletions packages/skia/apple/RNSkApplePlatformContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <utility>

#include "RNDawnContext.h"
#include "RNSkGpuPurge.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
Expand Down Expand Up @@ -183,6 +184,12 @@
usingBlock:^(NSNotification *) {
DawnContext::setForeground(true);
}];
[center addObserverForName:UIApplicationDidReceiveMemoryWarningNotification
object:nil
queue:nil
usingBlock:^(NSNotification *) {
RNSkGpuPurge::onMemoryWarning();
}];
});
#endif // !TARGET_OS_OSX
}
Expand Down
9 changes: 9 additions & 0 deletions packages/skia/cpp/api/JsiSkApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "rnskia/RNSkRuntimeScheduler.h"

#include "rnskia/RNDawnContext.h"
#include "rnskia/RNSkGpuPurge.h"

#include "JsiSkAnimatedImage.h"
#include "JsiSkAnimatedImageFactory.h"
Expand Down Expand Up @@ -168,6 +169,12 @@ class JsiSkApi : public JsiSkNativeObject<JsiSkApi> {
JSI_HOST_FUNCTION(Color) {
return JsiSkColor::createCtor()(runtime, thisValue, arguments, count);
}
JSI_HOST_FUNCTION(purgeGpuResources) {
// The recorders of this thread, which only it may touch, then Dawn.
DawnContext::purgeThreadRecorders();
RNSkGpuPurge::request();
return jsi::Value::undefined();
}
JSI_HOST_FUNCTION(getNativeDevice) {
// Raw WGPUDevice pointer of the Graphite device, as a BigInt. Consumed by
// react-native-webgpu's importDevice(), which AddRefs it; the pointer
Expand Down Expand Up @@ -267,6 +274,8 @@ class JsiSkApi : public JsiSkNativeObject<JsiSkApi> {
}

static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installHostMethod(runtime, prototype, "purgeGpuResources",
&JsiSkApi::purgeGpuResources);
installHostMethod(runtime, prototype, "getNativeDevice",
&JsiSkApi::getNativeDevice);
installGetterWithRuntime(runtime, prototype, "gpu", &JsiSkApi::getGpu);
Expand Down
45 changes: 29 additions & 16 deletions packages/skia/cpp/api/JsiSkCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "CustomBlendModes.h"
#include "JsiSkConverters.h"
#include "JsiSkDispatcher.h"
#include "JsiSkFont.h"
#include "JsiSkImage.h"
#include "JsiSkImageInfo.h"
Expand All @@ -29,6 +28,7 @@

#include "rnskia/RNDawnContext.h"
#include "rnskia/RNSkAtlas.h"
#include "rnskia/RNSkSurfaceRecorder.h"

#include <jsi/jsi.h>

Expand All @@ -50,6 +50,19 @@ namespace RNSkia {

namespace jsi = facebook::jsi;

/**
* A surface with the recorder it was made on, destroyed in that order: the
* surface deregisters itself from its recorder.
*/
struct SurfaceAndRecorder {
SurfaceAndRecorder(std::shared_ptr<RNSkSurfaceRecorder> recorder,
sk_sp<SkSurface> surface)
: recorder(std::move(recorder)), surface(std::move(surface)) {}
// Destroyed last.
std::shared_ptr<RNSkSurfaceRecorder> recorder;
sk_sp<SkSurface> surface;
};

class JsiSkCanvas : public JsiSkNativeObject<JsiSkCanvas> {
public:
static constexpr const char *CLASS_NAME = "Canvas";
Expand Down Expand Up @@ -514,8 +527,8 @@ class JsiSkCanvas : public JsiSkNativeObject<JsiSkCanvas> {
}
if (_surface) {
auto snapshot = _surface->makeImageSnapshot();
if (auto *recorder = _surface->recorder()) {
DawnContext::getInstance().submitRecording(recorder);
if (_recorder) {
_recorder->flush();
}
return JsiSkImage::readPixelsAsync(
runtime, getContext(), std::move(snapshot), *info, srcX, srcY);
Expand Down Expand Up @@ -612,35 +625,35 @@ class JsiSkCanvas : public JsiSkNativeObject<JsiSkCanvas> {
throw std::runtime_error("Attempted to use a canvas whose surface was "
"disposed or whose recording has finished.");
}
if (_recorder) {
_recorder->markDirty();
}
return _canvas;
}

// Associates the canvas with its owning surface, keeping it alive as long
// as the canvas. This lets readPixels fall back to a surface snapshot on
// Graphite, which has no synchronous canvas readback. The dispatcher is the
// one of the thread the surface was created on.
// as the canvas, along with the recorder of the surface. This lets readPixels
// fall back to a surface snapshot on Graphite, which has no synchronous
// canvas readback.
void setSurface(sk_sp<SkSurface> surface,
std::shared_ptr<Dispatcher> dispatcher) {
std::shared_ptr<RNSkSurfaceRecorder> recorder) {
_surface = std::move(surface);
_dispatcher = std::move(dispatcher);
_recorder = std::move(recorder);
}

// Called by the owner of the SkCanvas (JsiSkSurface, JsiSkPictureRecorder,
// JsiSkGraphiteContext) once that canvas is gone, and by the destructor:
// forgets the pointer and lets go of the surface. The canvas may hold the
// last reference to its surface, and the GC may finalize it on any thread:
// a Graphite surface has to be destroyed on the thread of its recorder (see
// Dispatcher).
// forgets the pointer and lets go of the surface and its recorder.
void detach() {
_canvas = nullptr;
if (_dispatcher) {
_dispatcher->release(std::move(_surface));
}
// Declared so that the surface goes before its recorder.
auto released =
SurfaceAndRecorder(std::move(_recorder), std::move(_surface));
}

private:
SkCanvas *_canvas = nullptr;
sk_sp<SkSurface> _surface;
std::shared_ptr<Dispatcher> _dispatcher;
std::shared_ptr<RNSkSurfaceRecorder> _recorder;
};
} // namespace RNSkia
88 changes: 0 additions & 88 deletions packages/skia/cpp/api/JsiSkDispatcher.h

This file was deleted.

Loading
Loading