react-native-webgpu 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/CMakeLists.txt +1 -0
- package/android/cpp/cpp-adapter.cpp +19 -96
- package/android/src/main/java/com/webgpu/BackingViewKind.java +5 -22
- package/android/src/main/java/com/webgpu/WebGPUAPI.java +0 -2
- package/android/src/main/java/com/webgpu/WebGPUModule.java +31 -0
- package/android/src/main/java/com/webgpu/WebGPUView.java +17 -34
- package/apple/WebGPUModule.mm +22 -0
- package/cpp/rnwgpu/GpuPurge.cpp +86 -0
- package/cpp/rnwgpu/GpuPurge.h +66 -0
- package/cpp/rnwgpu/SurfaceRegistry.h +0 -610
- package/cpp/rnwgpu/api/GPUDevice.cpp +59 -32
- package/cpp/rnwgpu/api/GPUDevice.h +49 -4
- package/cpp/rnwgpu/api/RNWebGPU.h +6 -0
- package/lib/commonjs/Canvas.js +8 -26
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/main/index.js +12 -0
- package/lib/commonjs/main/index.js.map +1 -1
- package/lib/commonjs/main/index.web.js +11 -0
- package/lib/commonjs/main/index.web.js.map +1 -1
- package/lib/commonjs/mock.js +4 -1
- package/lib/commonjs/mock.js.map +1 -1
- package/lib/commonjs/purgeGpuResources.js +29 -0
- package/lib/commonjs/purgeGpuResources.js.map +1 -0
- package/lib/module/Canvas.js +8 -26
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/main/index.js +1 -0
- package/lib/module/main/index.js.map +1 -1
- package/lib/module/main/index.web.js +1 -0
- package/lib/module/main/index.web.js.map +1 -1
- package/lib/module/mock.js +1 -0
- package/lib/module/mock.js.map +1 -1
- package/lib/module/purgeGpuResources.js +22 -0
- package/lib/module/purgeGpuResources.js.map +1 -0
- package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/mock.d.ts +1 -0
- package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/purgeGpuResources.d.ts +18 -0
- package/lib/typescript/lib/commonjs/purgeGpuResources.d.ts.map +1 -0
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/main/index.d.ts +1 -0
- package/lib/typescript/lib/module/main/index.web.d.ts +1 -0
- package/lib/typescript/lib/module/mock.d.ts +1 -0
- package/lib/typescript/lib/module/mock.d.ts.map +1 -1
- package/lib/typescript/lib/module/purgeGpuResources.d.ts +2 -0
- package/lib/typescript/lib/module/purgeGpuResources.d.ts.map +1 -0
- package/lib/typescript/src/Canvas.d.ts +2 -12
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.d.ts +1 -0
- package/lib/typescript/src/main/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.web.d.ts +1 -0
- package/lib/typescript/src/main/index.web.d.ts.map +1 -1
- package/lib/typescript/src/mock.d.ts +2 -1
- package/lib/typescript/src/mock.d.ts.map +1 -1
- package/lib/typescript/src/purgeGpuResources.d.ts +17 -0
- package/lib/typescript/src/purgeGpuResources.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/Canvas.tsx +12 -50
- package/src/WebGPUViewNativeComponent.ts +1 -1
- package/src/WebGPUViewNativeComponent.web.ts +1 -1
- package/src/index.tsx +1 -0
- package/src/main/index.tsx +1 -0
- package/src/main/index.web.tsx +1 -0
- package/src/mock.ts +2 -1
- package/src/purgeGpuResources.ts +21 -0
- package/android/src/main/java/com/webgpu/WebGPUHardwareBufferView.java +0 -278
package/android/CMakeLists.txt
CHANGED
|
@@ -56,6 +56,7 @@ add_library(${PACKAGE_NAME} SHARED
|
|
|
56
56
|
../cpp/rnwgpu/api/GPUComputePipeline.cpp
|
|
57
57
|
../cpp/rnwgpu/api/GPUCanvasContext.cpp
|
|
58
58
|
../cpp/rnwgpu/DawnBlobCache.cpp
|
|
59
|
+
../cpp/rnwgpu/GpuPurge.cpp
|
|
59
60
|
../cpp/rnwgpu/NativeBufferViewRegistry.cpp
|
|
60
61
|
../cpp/rnwgpu/AnimationFrames.cpp
|
|
61
62
|
../cpp/rnwgpu/RNWebGPUManager.cpp
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
#include "DawnBlobCache.h"
|
|
26
26
|
#include "GPUCanvasContext.h"
|
|
27
27
|
#include "GPUDevice.h"
|
|
28
|
+
#include "GpuPurge.h"
|
|
28
29
|
#include "NativeBufferViewRegistry.h"
|
|
29
30
|
#include "RNWebGPUManager.h"
|
|
30
31
|
#include "WebGPURuntime.h"
|
|
@@ -70,6 +71,21 @@ Java_com_webgpu_WebGPUModule_flushPipelineCaches(JNIEnv * /* env */,
|
|
|
70
71
|
rnwgpu::GPUDevice::flushPipelineCaches();
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
// WebGPUModule's ComponentCallbacks2 (UI thread): what the system says about
|
|
75
|
+
// memory, as it says it. GpuPurge decides what calls for a purge.
|
|
76
|
+
extern "C" JNIEXPORT void JNICALL
|
|
77
|
+
Java_com_webgpu_WebGPUModule_onLowMemoryNative(JNIEnv * /* env */,
|
|
78
|
+
jclass /* clazz */) {
|
|
79
|
+
rnwgpu::GpuPurge::onLowMemory();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
extern "C" JNIEXPORT void JNICALL
|
|
83
|
+
Java_com_webgpu_WebGPUModule_onTrimMemoryNative(JNIEnv * /* env */,
|
|
84
|
+
jclass /* clazz */,
|
|
85
|
+
jint level) {
|
|
86
|
+
rnwgpu::GpuPurge::onTrimMemory(level);
|
|
87
|
+
}
|
|
88
|
+
|
|
73
89
|
// WebGPUModule.onHostResume and onHostPause (UI thread): animation frames
|
|
74
90
|
// follow the host activity, resumed or not.
|
|
75
91
|
extern "C" JNIEXPORT void JNICALL
|
|
@@ -198,9 +214,9 @@ extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onSurfaceChanged(
|
|
|
198
214
|
namespace {
|
|
199
215
|
|
|
200
216
|
// Weak handle on a Java view and one of its no-argument methods, for native
|
|
201
|
-
// threads to call into it:
|
|
202
|
-
//
|
|
203
|
-
//
|
|
217
|
+
// threads to call into it: native wakes a WebGPUNativeBufferView when new
|
|
218
|
+
// content is ready, the presenting thread tells a SurfaceView it is animating
|
|
219
|
+
// (AnimationReporter). Holding the view weakly means a view that RN
|
|
204
220
|
// has dropped is not kept alive by a SurfaceInfo that outlives it (the
|
|
205
221
|
// registry keeps the SurfaceInfo until onDropViewInstance).
|
|
206
222
|
struct WeakViewCall {
|
|
@@ -416,99 +432,6 @@ extern "C" JNIEXPORT void JNICALL Java_com_webgpu_WebGPUView_onViewDestroyed(
|
|
|
416
432
|
registry.removeSurfaceInfo(contextId);
|
|
417
433
|
}
|
|
418
434
|
|
|
419
|
-
// --- WebGPUHardwareBufferView (AHB-pool presentation)
|
|
420
|
-
// ---------------------------------
|
|
421
|
-
|
|
422
|
-
// Turn on pool mode for this context. dpW/dpH is the canvas-client (dp) size;
|
|
423
|
-
// the native pool buffers are sized from the canvas drawing buffer lazily.
|
|
424
|
-
extern "C" JNIEXPORT void JNICALL
|
|
425
|
-
Java_com_webgpu_WebGPUHardwareBufferView_nEnablePool(JNIEnv *env, jobject thiz,
|
|
426
|
-
jint contextId, jint dpW,
|
|
427
|
-
jint dpH) {
|
|
428
|
-
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
|
|
429
|
-
auto info = registry.getSurfaceInfoOrCreate(
|
|
430
|
-
contextId, rnwgpu::WebGPURuntime::shared().instance(),
|
|
431
|
-
static_cast<int>(dpW), static_cast<int>(dpH));
|
|
432
|
-
auto waker = std::make_shared<WeakViewCall>(env, thiz, "onNativeFrameReady");
|
|
433
|
-
info->setPoolFrameReadyCallback([waker]() { (*waker)(); });
|
|
434
|
-
info->enablePool(static_cast<int>(dpW), static_cast<int>(dpH));
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
// Keep the canvas-client (dp) size in sync on resize.
|
|
438
|
-
extern "C" JNIEXPORT void JNICALL
|
|
439
|
-
Java_com_webgpu_WebGPUHardwareBufferView_nSetClientSize(JNIEnv *env,
|
|
440
|
-
jobject thiz,
|
|
441
|
-
jint contextId,
|
|
442
|
-
jint dpW, jint dpH) {
|
|
443
|
-
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
|
|
444
|
-
auto info = registry.getSurfaceInfo(contextId);
|
|
445
|
-
if (info != nullptr) {
|
|
446
|
-
info->setPoolClientSize(static_cast<int>(dpW), static_cast<int>(dpH));
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// The HardwareBuffer backing a (generation, slot), for the view to wrap in a
|
|
451
|
-
// Bitmap. Returns null for a retired generation.
|
|
452
|
-
extern "C" JNIEXPORT jobject JNICALL
|
|
453
|
-
Java_com_webgpu_WebGPUHardwareBufferView_nGetHardwareBuffer(
|
|
454
|
-
JNIEnv *env, jobject thiz, jint contextId, jint generation, jint slot) {
|
|
455
|
-
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
|
|
456
|
-
auto info = registry.getSurfaceInfo(contextId);
|
|
457
|
-
if (info == nullptr) {
|
|
458
|
-
return nullptr;
|
|
459
|
-
}
|
|
460
|
-
void *ahb = info->poolBufferForDisplay(static_cast<uint32_t>(generation),
|
|
461
|
-
static_cast<int>(slot));
|
|
462
|
-
if (ahb == nullptr) {
|
|
463
|
-
return nullptr;
|
|
464
|
-
}
|
|
465
|
-
// toHardwareBuffer acquires its own ref for the returned jobject; the pool
|
|
466
|
-
// keeps the underlying buffer alive independently.
|
|
467
|
-
return AHardwareBuffer_toHardwareBuffer(env,
|
|
468
|
-
static_cast<AHardwareBuffer *>(ahb));
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// Latest signaled frame ready to display, encoded (generation << 32 | slot), or
|
|
472
|
-
// -1 when nothing is new. Called on the UI thread after onNativeFrameReady.
|
|
473
|
-
extern "C" JNIEXPORT jlong JNICALL
|
|
474
|
-
Java_com_webgpu_WebGPUHardwareBufferView_nPollReady(JNIEnv *env, jobject thiz,
|
|
475
|
-
jint contextId) {
|
|
476
|
-
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
|
|
477
|
-
auto info = registry.getSurfaceInfo(contextId);
|
|
478
|
-
if (info == nullptr) {
|
|
479
|
-
return -1;
|
|
480
|
-
}
|
|
481
|
-
return info->poolPollReady();
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
// The view is done displaying (and holding) a slot; return it to the pool.
|
|
485
|
-
extern "C" JNIEXPORT void JNICALL
|
|
486
|
-
Java_com_webgpu_WebGPUHardwareBufferView_nReleaseSlot(JNIEnv *env, jobject thiz,
|
|
487
|
-
jint contextId,
|
|
488
|
-
jint generation,
|
|
489
|
-
jint slot) {
|
|
490
|
-
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
|
|
491
|
-
auto info = registry.getSurfaceInfo(contextId);
|
|
492
|
-
if (info != nullptr) {
|
|
493
|
-
info->poolReleaseSlot(static_cast<uint32_t>(generation),
|
|
494
|
-
static_cast<int>(slot));
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
// View detached / hidden: keep the canvas alive by falling back to an offscreen
|
|
499
|
-
// texture (mirrors switchToOffscreenSurface for the surface path).
|
|
500
|
-
extern "C" JNIEXPORT void JNICALL
|
|
501
|
-
Java_com_webgpu_WebGPUHardwareBufferView_nSwitchToOffscreen(JNIEnv *env,
|
|
502
|
-
jobject thiz,
|
|
503
|
-
jint contextId) {
|
|
504
|
-
auto ®istry = rnwgpu::SurfaceRegistry::getInstance();
|
|
505
|
-
auto info = registry.getSurfaceInfo(contextId);
|
|
506
|
-
if (info != nullptr) {
|
|
507
|
-
info->setPoolFrameReadyCallback(nullptr);
|
|
508
|
-
info->switchToOffscreen();
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
|
|
512
435
|
// --- WebGPUNativeBufferView
|
|
513
436
|
// ------------------------------------------------------
|
|
514
437
|
|
|
@@ -4,35 +4,18 @@ import androidx.annotation.Nullable;
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* The Android view a WebGPUView draws into, see WebGPUView.updateView(), and
|
|
7
|
-
* the
|
|
8
|
-
*
|
|
7
|
+
* the rule the prop resolves by. The JS side (Canvas.tsx) mirrors it to warn
|
|
8
|
+
* in development.
|
|
9
9
|
*/
|
|
10
10
|
enum BackingViewKind {
|
|
11
11
|
SURFACE_VIEW,
|
|
12
|
-
TEXTURE_VIEW
|
|
13
|
-
HARDWARE_BUFFER_VIEW;
|
|
12
|
+
TEXTURE_VIEW;
|
|
14
13
|
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
* HardwareBufferView (a plain View drawing AHardwareBuffers inline) needs
|
|
18
|
-
* Android 10: below it, the request falls back to a TextureView, which
|
|
19
|
-
* composites the same way, and the JS side warns about it in development.
|
|
20
|
-
*/
|
|
21
|
-
static BackingViewKind fromBackingView(@Nullable String backingView, boolean hardwareBufferSupported) {
|
|
14
|
+
/** The kind the androidBackingView prop names; a TextureView by default. */
|
|
15
|
+
static BackingViewKind fromBackingView(@Nullable String backingView) {
|
|
22
16
|
if ("SurfaceView".equals(backingView)) {
|
|
23
17
|
return SURFACE_VIEW;
|
|
24
18
|
}
|
|
25
|
-
if ("HardwareBufferView".equals(backingView) && hardwareBufferSupported) {
|
|
26
|
-
return HARDWARE_BUFFER_VIEW;
|
|
27
|
-
}
|
|
28
19
|
return TEXTURE_VIEW;
|
|
29
20
|
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Whether the canvas composites as opaque: what the prop asks for, except
|
|
33
|
-
* that a HardwareBufferView always composites with alpha.
|
|
34
|
-
*/
|
|
35
|
-
static boolean resolveOpaque(BackingViewKind kind, boolean opaque) {
|
|
36
|
-
return opaque && kind != HARDWARE_BUFFER_VIEW;
|
|
37
|
-
}
|
|
38
21
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
package com.webgpu;
|
|
2
2
|
|
|
3
|
+
import android.content.ComponentCallbacks2;
|
|
4
|
+
import android.content.res.Configuration;
|
|
3
5
|
import android.graphics.Bitmap;
|
|
4
6
|
import android.hardware.HardwareBuffer;
|
|
5
7
|
import android.os.Build;
|
|
@@ -34,6 +36,25 @@ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEve
|
|
|
34
36
|
System.loadLibrary("react-native-webgpu"); // Load the C++ library
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
// What the system says about memory goes to the native side as it comes,
|
|
40
|
+
// which decides when the GPU memory nothing uses goes back to the system
|
|
41
|
+
// (see GpuPurge).
|
|
42
|
+
private final ComponentCallbacks2 mComponentCallbacks =
|
|
43
|
+
new ComponentCallbacks2() {
|
|
44
|
+
@Override
|
|
45
|
+
public void onConfigurationChanged(Configuration newConfig) {}
|
|
46
|
+
|
|
47
|
+
@Override
|
|
48
|
+
public void onLowMemory() {
|
|
49
|
+
onLowMemoryNative();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@Override
|
|
53
|
+
public void onTrimMemory(int level) {
|
|
54
|
+
onTrimMemoryNative(level);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
37
58
|
public WebGPUModule(ReactApplicationContext reactContext) {
|
|
38
59
|
super(reactContext);
|
|
39
60
|
// Initialize the C++ module
|
|
@@ -41,11 +62,15 @@ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEve
|
|
|
41
62
|
// Animation frames follow the host activity: resumed, or not.
|
|
42
63
|
setForegroundNative(reactContext.getLifecycleState() == LifecycleState.RESUMED);
|
|
43
64
|
reactContext.addLifecycleEventListener(this);
|
|
65
|
+
reactContext.getApplicationContext().registerComponentCallbacks(mComponentCallbacks);
|
|
44
66
|
}
|
|
45
67
|
|
|
46
68
|
@Override
|
|
47
69
|
public void invalidate() {
|
|
48
70
|
getReactApplicationContext().removeLifecycleEventListener(this);
|
|
71
|
+
getReactApplicationContext()
|
|
72
|
+
.getApplicationContext()
|
|
73
|
+
.unregisterComponentCallbacks(mComponentCallbacks);
|
|
49
74
|
super.invalidate();
|
|
50
75
|
}
|
|
51
76
|
|
|
@@ -72,6 +97,12 @@ public class WebGPUModule extends NativeWebGPUModuleSpec implements LifecycleEve
|
|
|
72
97
|
@DoNotStrip
|
|
73
98
|
private native void flushPipelineCaches();
|
|
74
99
|
|
|
100
|
+
@DoNotStrip
|
|
101
|
+
private static native void onLowMemoryNative();
|
|
102
|
+
|
|
103
|
+
@DoNotStrip
|
|
104
|
+
private static native void onTrimMemoryNative(int level);
|
|
105
|
+
|
|
75
106
|
/**
|
|
76
107
|
* Whether the app is in the foreground: the host activity is resumed (see
|
|
77
108
|
* AnimationFrameDispatcher::setForeground). An AChoreographer sends its
|
|
@@ -13,9 +13,8 @@ import com.facebook.react.uimanager.ThemedReactContext;
|
|
|
13
13
|
import com.facebook.react.views.view.ReactViewGroup;
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* The Android view behind <Canvas>. It is backed by a TextureView
|
|
17
|
-
* SurfaceView
|
|
18
|
-
* and created once, from the props of the first transaction, for the life of
|
|
16
|
+
* The Android view behind <Canvas>. It is backed by a TextureView or a
|
|
17
|
+
* SurfaceView, named by the androidBackingView prop and created once, from the props of the first transaction, for the life of
|
|
19
18
|
* the view (see updateView()).
|
|
20
19
|
*/
|
|
21
20
|
public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
@@ -36,8 +35,8 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
36
35
|
private View mView = null;
|
|
37
36
|
@Nullable
|
|
38
37
|
private BackingViewKind mKind = null;
|
|
39
|
-
// Only a SurfaceView votes: a TextureView
|
|
40
|
-
//
|
|
38
|
+
// Only a SurfaceView votes: a TextureView is composited into the window,
|
|
39
|
+
// which votes for it.
|
|
41
40
|
private final SurfaceFrameRateVote mFrameRateVote = new SurfaceFrameRateVote(
|
|
42
41
|
this, idleAfterNanos -> releaseFrameRateVoteIfIdle(mContextId, idleAfterNanos));
|
|
43
42
|
|
|
@@ -46,9 +45,9 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
46
45
|
// ReactViewGroup disables clipChildren (RN overflow is handled with canvas
|
|
47
46
|
// clipping instead). Android copies the parent's clipChildren into each
|
|
48
47
|
// child's RenderNode.clipToBounds, and HWUI treats a damaged node without
|
|
49
|
-
// it as unbounded damage (RenderNode::damageSelf).
|
|
50
|
-
//
|
|
51
|
-
//
|
|
48
|
+
// it as unbounded damage (RenderNode::damageSelf). A TextureView child
|
|
49
|
+
// invalidates on every frame, so without this every non-opaque canvas
|
|
50
|
+
// frame repaints the whole window. Our only child
|
|
52
51
|
// exactly fills this view, so clipping it changes nothing visually.
|
|
53
52
|
setClipChildren(true);
|
|
54
53
|
}
|
|
@@ -76,8 +75,7 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
76
75
|
warnIgnoredOnceMounted("android.backingView");
|
|
77
76
|
return;
|
|
78
77
|
}
|
|
79
|
-
|
|
80
|
-
mBackingView = BackingViewKind.fromBackingView(value, hardwareBufferSupported);
|
|
78
|
+
mBackingView = BackingViewKind.fromBackingView(value);
|
|
81
79
|
}
|
|
82
80
|
|
|
83
81
|
public void setOpaque(boolean value) {
|
|
@@ -139,32 +137,22 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
139
137
|
// not carry. A canvas that needs another backing view is remounted instead,
|
|
140
138
|
// and is off screen for a frame or two until the new view draws its first
|
|
141
139
|
// frame. Later transactions apply the opacity in place (the setters decide
|
|
142
|
-
// what else is taken)
|
|
143
|
-
// alpha, so there it is a no-op.
|
|
140
|
+
// what else is taken).
|
|
144
141
|
public void updateView() {
|
|
145
142
|
if (mKind != null) {
|
|
146
|
-
boolean opaque = BackingViewKind.resolveOpaque(mKind, mOpaque);
|
|
147
143
|
if (mKind == BackingViewKind.TEXTURE_VIEW) {
|
|
148
|
-
((WebGPUTextureView) mView).setOpaque(
|
|
149
|
-
} else
|
|
150
|
-
((WebGPUSurfaceView) mView).setOpaque(
|
|
144
|
+
((WebGPUTextureView) mView).setOpaque(mOpaque);
|
|
145
|
+
} else {
|
|
146
|
+
((WebGPUSurfaceView) mView).setOpaque(mOpaque);
|
|
151
147
|
}
|
|
152
148
|
return;
|
|
153
149
|
}
|
|
154
|
-
|
|
155
|
-
mKind = kind;
|
|
156
|
-
boolean opaque = BackingViewKind.resolveOpaque(kind, mOpaque);
|
|
150
|
+
mKind = mBackingView;
|
|
157
151
|
Context ctx = getContext();
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
case TEXTURE_VIEW:
|
|
163
|
-
mView = new WebGPUTextureView(ctx, this, opaque);
|
|
164
|
-
break;
|
|
165
|
-
default:
|
|
166
|
-
mView = new WebGPUSurfaceView(ctx, this, mZOrderOnTop, mZOrderMediaOverlay, opaque);
|
|
167
|
-
break;
|
|
152
|
+
if (mKind == BackingViewKind.TEXTURE_VIEW) {
|
|
153
|
+
mView = new WebGPUTextureView(ctx, this, mOpaque);
|
|
154
|
+
} else {
|
|
155
|
+
mView = new WebGPUSurfaceView(ctx, this, mZOrderOnTop, mZOrderMediaOverlay, mOpaque);
|
|
168
156
|
}
|
|
169
157
|
addView(mView);
|
|
170
158
|
// ReactViewGroup.requestLayout() is a deliberate no-op, so addView outside
|
|
@@ -234,11 +222,6 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
|
|
|
234
222
|
}
|
|
235
223
|
}
|
|
236
224
|
|
|
237
|
-
@Override
|
|
238
|
-
public int getContextId() {
|
|
239
|
-
return mContextId;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
225
|
/**
|
|
243
226
|
* Called from WebGPUViewManager.onDropViewInstance when React removes this
|
|
244
227
|
* view: the view dies with its Canvas, so it retires the registry entry.
|
package/apple/WebGPUModule.mm
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
#include "ApplePlatformContext.h"
|
|
3
3
|
#include "DawnBlobCache.h"
|
|
4
4
|
#import "GPUCanvasContext.h"
|
|
5
|
+
#include "GpuPurge.h"
|
|
5
6
|
#import "RNWGUIKit.h"
|
|
6
7
|
|
|
7
8
|
#import <React/RCTBridge+Private.h>
|
|
@@ -22,6 +23,24 @@ namespace react = facebook::react;
|
|
|
22
23
|
- (void *)runtime;
|
|
23
24
|
@end
|
|
24
25
|
|
|
26
|
+
#if !TARGET_OS_OSX
|
|
27
|
+
// A memory warning goes to the native side as it comes, which decides when the
|
|
28
|
+
// GPU memory nothing uses goes back to the system (see GpuPurge). Once for the
|
|
29
|
+
// process: devices outlive a React reload.
|
|
30
|
+
static void observeMemoryWarnings() {
|
|
31
|
+
static dispatch_once_t once;
|
|
32
|
+
dispatch_once(&once, ^{
|
|
33
|
+
[[NSNotificationCenter defaultCenter]
|
|
34
|
+
addObserverForName:UIApplicationDidReceiveMemoryWarningNotification
|
|
35
|
+
object:nil
|
|
36
|
+
queue:nil
|
|
37
|
+
usingBlock:^(NSNotification *) {
|
|
38
|
+
rnwgpu::GpuPurge::onMemoryWarning();
|
|
39
|
+
}];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
#endif
|
|
43
|
+
|
|
25
44
|
@implementation WebGPUModule
|
|
26
45
|
|
|
27
46
|
RCT_EXPORT_MODULE(WebGPUModule)
|
|
@@ -149,6 +168,9 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
|
|
|
149
168
|
rnwgpu::DawnBlobCache::setDirectory(
|
|
150
169
|
[caches stringByAppendingPathComponent:@"webgpu-dawn"].UTF8String);
|
|
151
170
|
}
|
|
171
|
+
#if !TARGET_OS_OSX
|
|
172
|
+
observeMemoryWarnings();
|
|
173
|
+
#endif
|
|
152
174
|
webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
|
|
153
175
|
platformContext);
|
|
154
176
|
return @true;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#include "GpuPurge.h"
|
|
2
|
+
|
|
3
|
+
#include <mutex>
|
|
4
|
+
#include <system_error>
|
|
5
|
+
#include <thread>
|
|
6
|
+
|
|
7
|
+
#include "GPUDevice.h"
|
|
8
|
+
|
|
9
|
+
namespace rnwgpu {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
struct State {
|
|
14
|
+
std::mutex mutex;
|
|
15
|
+
// Whether the purge thread is alive.
|
|
16
|
+
bool running = false;
|
|
17
|
+
// Whether a request came in since the thread last started a run.
|
|
18
|
+
bool requested = false;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Never destroyed: the purge thread may still use it while the process exits.
|
|
22
|
+
State &getState() noexcept {
|
|
23
|
+
static auto *state = new State();
|
|
24
|
+
return *state;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
} // namespace
|
|
28
|
+
|
|
29
|
+
void GpuPurge::request() noexcept {
|
|
30
|
+
auto &state = getState();
|
|
31
|
+
{
|
|
32
|
+
std::lock_guard<std::mutex> lock(state.mutex);
|
|
33
|
+
state.requested = true;
|
|
34
|
+
if (state.running) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
state.running = true;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
std::thread([] { runRequests(); }).detach();
|
|
41
|
+
} catch (const std::system_error &) {
|
|
42
|
+
// No thread to be had: nothing is purged.
|
|
43
|
+
std::lock_guard<std::mutex> lock(state.mutex);
|
|
44
|
+
state.running = false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
void GpuPurge::onMemoryWarning() noexcept { request(); }
|
|
49
|
+
|
|
50
|
+
void GpuPurge::onLowMemory() noexcept { request(); }
|
|
51
|
+
|
|
52
|
+
void GpuPurge::onTrimMemory(int level) noexcept {
|
|
53
|
+
if (level >= kTrimMemoryUiHidden) {
|
|
54
|
+
request();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
void GpuPurge::runRequests() noexcept {
|
|
59
|
+
auto &state = getState();
|
|
60
|
+
for (;;) {
|
|
61
|
+
{
|
|
62
|
+
std::lock_guard<std::mutex> lock(state.mutex);
|
|
63
|
+
if (!state.requested) {
|
|
64
|
+
state.running = false;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
state.requested = false;
|
|
68
|
+
}
|
|
69
|
+
run();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
void GpuPurge::run() {
|
|
74
|
+
for (int pass = 0; pass < kMaxPasses; pass++) {
|
|
75
|
+
bool pending = GPUDevice::reduceMemoryUsage();
|
|
76
|
+
if (pass < kRuntimeThreadPasses) {
|
|
77
|
+
pending |= GPUDevice::reduceMemoryUsageOnRuntimeThreads();
|
|
78
|
+
}
|
|
79
|
+
if (!pending) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
std::this_thread::sleep_for(kPassInterval);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
} // namespace rnwgpu
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <chrono>
|
|
4
|
+
|
|
5
|
+
namespace rnwgpu {
|
|
6
|
+
|
|
7
|
+
// purgeGpuResources(), which the system also brings about when it is short of
|
|
8
|
+
// memory and, on Android, when the app goes to the background: Dawn gives the
|
|
9
|
+
// GPU memory it keeps for reuse (the memory of destroyed textures and buffers,
|
|
10
|
+
// staging memory) back to the system, for every live device (see
|
|
11
|
+
// GPUDevice::reduceMemoryUsage). Nothing alive is affected.
|
|
12
|
+
//
|
|
13
|
+
// The platform modules only report what the system said (onMemoryWarning,
|
|
14
|
+
// onLowMemory, onTrimMemory): whether it calls for a purge is decided here.
|
|
15
|
+
//
|
|
16
|
+
// request() returns at once. The work runs on a thread of its own, started by
|
|
17
|
+
// a request and gone once done; a request made while it runs is served by one
|
|
18
|
+
// more run of the same thread, however many came in.
|
|
19
|
+
class GpuPurge {
|
|
20
|
+
public:
|
|
21
|
+
// purgeGpuResources() from JS. Any thread.
|
|
22
|
+
static void request() noexcept;
|
|
23
|
+
|
|
24
|
+
// iOS, UIApplicationDidReceiveMemoryWarningNotification: the system is short
|
|
25
|
+
// of memory, and ends the apps that hold the most of it first. Any thread.
|
|
26
|
+
static void onMemoryWarning() noexcept;
|
|
27
|
+
|
|
28
|
+
// Android, ComponentCallbacks.onLowMemory(): the whole system is short of
|
|
29
|
+
// memory. Any thread.
|
|
30
|
+
static void onLowMemory() noexcept;
|
|
31
|
+
|
|
32
|
+
// Android, ComponentCallbacks2.onTrimMemory(): purges from
|
|
33
|
+
// kTrimMemoryUiHidden, which is each time the app goes to the background.
|
|
34
|
+
// Any thread.
|
|
35
|
+
static void onTrimMemory(int level) noexcept;
|
|
36
|
+
|
|
37
|
+
private:
|
|
38
|
+
// One run asks Dawn again for as long as it says deletions wait for
|
|
39
|
+
// submitted work to complete, this many times at most and this far apart:
|
|
40
|
+
// about a second in all.
|
|
41
|
+
static constexpr int kMaxPasses = 10;
|
|
42
|
+
static constexpr std::chrono::milliseconds kPassInterval{100};
|
|
43
|
+
|
|
44
|
+
// The passes of a run that also ask the devices only the thread of their
|
|
45
|
+
// runtime may use (see GPUDevice::reduceMemoryUsageOnRuntimeThreads). What
|
|
46
|
+
// Dawn answers there is not waited for, hence a fixed number: the first lets
|
|
47
|
+
// the pending deletions go through, the next free what they leave.
|
|
48
|
+
static constexpr int kRuntimeThreadPasses = 3;
|
|
49
|
+
|
|
50
|
+
// ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: the app is no longer visible. It
|
|
51
|
+
// renders nothing there, and the system ends the processes in the background
|
|
52
|
+
// that hold the most memory first. The system sends it each time the app
|
|
53
|
+
// leaves the foreground, and it may be all it sends: TRIM_MEMORY_BACKGROUND
|
|
54
|
+
// did not come to an app left in the background until it was cached and
|
|
55
|
+
// frozen (a Samsung on Android 17). The levels below this one are for an app
|
|
56
|
+
// in the foreground, and are no longer sent since Android 14.
|
|
57
|
+
static constexpr int kTrimMemoryUiHidden = 20;
|
|
58
|
+
|
|
59
|
+
// The purge thread: runs until no request is left.
|
|
60
|
+
static void runRequests() noexcept;
|
|
61
|
+
|
|
62
|
+
// One run: the passes over the live devices.
|
|
63
|
+
static void run();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
} // namespace rnwgpu
|