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
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
package com.webgpu;
|
|
2
|
-
|
|
3
|
-
import android.annotation.SuppressLint;
|
|
4
|
-
import android.content.Context;
|
|
5
|
-
import android.graphics.Bitmap;
|
|
6
|
-
import android.graphics.Canvas;
|
|
7
|
-
import android.graphics.Paint;
|
|
8
|
-
import android.graphics.Rect;
|
|
9
|
-
import android.hardware.HardwareBuffer;
|
|
10
|
-
import android.os.Build;
|
|
11
|
-
import android.os.Handler;
|
|
12
|
-
import android.os.Looper;
|
|
13
|
-
import android.view.View;
|
|
14
|
-
|
|
15
|
-
import androidx.annotation.Keep;
|
|
16
|
-
import androidx.annotation.NonNull;
|
|
17
|
-
import androidx.annotation.RequiresApi;
|
|
18
|
-
|
|
19
|
-
import java.util.ArrayDeque;
|
|
20
|
-
import java.util.HashMap;
|
|
21
|
-
import java.util.Iterator;
|
|
22
|
-
import java.util.Map;
|
|
23
|
-
import java.util.concurrent.atomic.AtomicBoolean;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A "normal RN view" backend for the WebGPU canvas.
|
|
27
|
-
*
|
|
28
|
-
* <p>WebGPU renders into a native pool of AHardwareBuffers (sized from the canvas drawing buffer,
|
|
29
|
-
* like the swapchain) that Dawn imports as SharedTextureMemory; see SurfaceRegistry.h. Each finished
|
|
30
|
-
* buffer is drawn inline here via {@link Bitmap#wrapHardwareBuffer} + {@link Canvas#drawBitmap}, so
|
|
31
|
-
* this is a plain {@link View}: parent transforms, clipping, alpha, z-order and animations all
|
|
32
|
-
* apply, with no GL interop and no extra copy.
|
|
33
|
-
*
|
|
34
|
-
* <p>This view is a pure consumer: it enables pool mode, and whenever the native fence waiter
|
|
35
|
-
* signals that a frame is ready it asks native for the latest ready (gen, slot), wraps that buffer
|
|
36
|
-
* in a cached Bitmap, and draws it scaled to the current bounds. Acquire is rigorous (native blocks
|
|
37
|
-
* on Dawn's render-complete fence before a frame is ready); release is heuristic via a held-ring of
|
|
38
|
-
* {@link #HELD_MAX} frames.
|
|
39
|
-
*/
|
|
40
|
-
@RequiresApi(api = Build.VERSION_CODES.Q)
|
|
41
|
-
@SuppressLint("ViewConstructor")
|
|
42
|
-
public class WebGPUHardwareBufferView extends View {
|
|
43
|
-
|
|
44
|
-
private static final int HELD_MAX = 2; // displayed frames kept before release (release safety)
|
|
45
|
-
|
|
46
|
-
private final WebGPUAPI mApi;
|
|
47
|
-
private final Paint mPaint;
|
|
48
|
-
private final Rect mSrc = new Rect();
|
|
49
|
-
private final Rect mDst = new Rect();
|
|
50
|
-
|
|
51
|
-
// token (generation << 32 | slot) -> wrapped hardware Bitmap (one per slot per generation).
|
|
52
|
-
private final Map<Long, Bitmap> mBitmaps = new HashMap<>();
|
|
53
|
-
// Held-ring of displayed frame tokens (newest last). The newest is what onDraw shows.
|
|
54
|
-
private final ArrayDeque<Long> mHeld = new ArrayDeque<>();
|
|
55
|
-
|
|
56
|
-
// Native -> UI thread wake-up. The fence waiter calls onNativeFrameReady() off the UI thread;
|
|
57
|
-
// we coalesce those into at most one pending consume() on the main looper.
|
|
58
|
-
private final Handler mMainHandler = new Handler(Looper.getMainLooper());
|
|
59
|
-
private final AtomicBoolean mConsumePosted = new AtomicBoolean(false);
|
|
60
|
-
private final Runnable mConsume = this::consume;
|
|
61
|
-
|
|
62
|
-
private Bitmap mDisplayed;
|
|
63
|
-
private int mDisplayedW;
|
|
64
|
-
private int mDisplayedH;
|
|
65
|
-
private int mCurrentGen;
|
|
66
|
-
|
|
67
|
-
private boolean mAttached;
|
|
68
|
-
private boolean mEnabled;
|
|
69
|
-
|
|
70
|
-
public WebGPUHardwareBufferView(Context context, WebGPUAPI api) {
|
|
71
|
-
super(context);
|
|
72
|
-
mApi = api;
|
|
73
|
-
mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
|
|
74
|
-
setWillNotDraw(false);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private int contextId() {
|
|
78
|
-
return mApi.getContextId();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private static int genOf(long token) {
|
|
82
|
-
return (int) (token >>> 32);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private static int slotOf(long token) {
|
|
86
|
-
return (int) (token & 0xffffffffL);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// --- Sizing ----------------------------------------------------------------
|
|
90
|
-
|
|
91
|
-
private void enablePool(int w, int h) {
|
|
92
|
-
final float density = getResources().getDisplayMetrics().density;
|
|
93
|
-
nEnablePool(contextId(), Math.max(1, Math.round(w / density)),
|
|
94
|
-
Math.max(1, Math.round(h / density)));
|
|
95
|
-
mEnabled = true;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
@Override
|
|
99
|
-
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
|
100
|
-
super.onSizeChanged(w, h, oldw, oldh);
|
|
101
|
-
if (w <= 0 || h <= 0) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (!mEnabled) {
|
|
105
|
-
enablePool(w, h);
|
|
106
|
-
} else {
|
|
107
|
-
// Native reallocates the pool from the canvas drawing buffer; we only keep the dp
|
|
108
|
-
// canvas-client size in sync so JS computes the right canvas.width/height.
|
|
109
|
-
final float density = getResources().getDisplayMetrics().density;
|
|
110
|
-
nSetClientSize(contextId(), Math.max(1, Math.round(w / density)),
|
|
111
|
-
Math.max(1, Math.round(h / density)));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// --- Consume (UI thread, woken by the native fence waiter) -----------------
|
|
116
|
-
|
|
117
|
-
/** Called from the native fence-waiter thread (see cpp-adapter.cpp) when a frame is ready. */
|
|
118
|
-
@Keep
|
|
119
|
-
private void onNativeFrameReady() {
|
|
120
|
-
if (mConsumePosted.compareAndSet(false, true)) {
|
|
121
|
-
mMainHandler.post(mConsume);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
private void consume() {
|
|
126
|
-
mConsumePosted.set(false);
|
|
127
|
-
if (!mAttached) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
long token = nPollReady(contextId());
|
|
131
|
-
if (token >= 0) {
|
|
132
|
-
onFrameReady(token);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
private void onFrameReady(long token) {
|
|
137
|
-
final int gen = genOf(token);
|
|
138
|
-
final int slot = slotOf(token);
|
|
139
|
-
|
|
140
|
-
if (gen > mCurrentGen) {
|
|
141
|
-
mCurrentGen = gen;
|
|
142
|
-
recycleStaleGenerations();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
Bitmap bmp = mBitmaps.get(token);
|
|
146
|
-
if (bmp == null) {
|
|
147
|
-
HardwareBuffer hb = nGetHardwareBuffer(contextId(), gen, slot);
|
|
148
|
-
if (hb == null) {
|
|
149
|
-
return; // generation already retired; drop this stale frame
|
|
150
|
-
}
|
|
151
|
-
// wrapHardwareBuffer takes its own ref, so the wrapper can be closed immediately.
|
|
152
|
-
bmp = Bitmap.wrapHardwareBuffer(hb, null);
|
|
153
|
-
hb.close();
|
|
154
|
-
if (bmp == null) {
|
|
155
|
-
// Cannot display this frame; hand the slot straight back so the pool
|
|
156
|
-
// does not drain (native marked it Displayed when we polled it).
|
|
157
|
-
nReleaseSlot(contextId(), gen, slot);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
mBitmaps.put(token, bmp);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
mDisplayed = bmp;
|
|
164
|
-
mDisplayedW = bmp.getWidth();
|
|
165
|
-
mDisplayedH = bmp.getHeight();
|
|
166
|
-
|
|
167
|
-
mHeld.addLast(token);
|
|
168
|
-
while (mHeld.size() > HELD_MAX) {
|
|
169
|
-
long old = mHeld.pollFirst();
|
|
170
|
-
if (genOf(old) == mCurrentGen) {
|
|
171
|
-
// Live generation reuses slots: hand it back once HWUI has had time to sample it.
|
|
172
|
-
nReleaseSlot(contextId(), mCurrentGen, slotOf(old));
|
|
173
|
-
} else {
|
|
174
|
-
// Old generation: its slot is never re-rendered, so just drop the Bitmap.
|
|
175
|
-
recycleToken(old);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
invalidate();
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private void recycleStaleGenerations() {
|
|
182
|
-
// Keep the current and previous generation (cross-fade); recycle older cached Bitmaps that are
|
|
183
|
-
// no longer held.
|
|
184
|
-
Iterator<Map.Entry<Long, Bitmap>> it = mBitmaps.entrySet().iterator();
|
|
185
|
-
while (it.hasNext()) {
|
|
186
|
-
Map.Entry<Long, Bitmap> e = it.next();
|
|
187
|
-
if (genOf(e.getKey()) < mCurrentGen - 1 && !mHeld.contains(e.getKey())) {
|
|
188
|
-
Bitmap b = e.getValue();
|
|
189
|
-
if (b != mDisplayed && !b.isRecycled()) {
|
|
190
|
-
b.recycle();
|
|
191
|
-
}
|
|
192
|
-
it.remove();
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private void recycleToken(long token) {
|
|
198
|
-
Bitmap b = mBitmaps.remove(token);
|
|
199
|
-
if (b != null && b != mDisplayed && !b.isRecycled()) {
|
|
200
|
-
b.recycle();
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
@Override
|
|
205
|
-
protected void onDraw(Canvas canvas) {
|
|
206
|
-
super.onDraw(canvas);
|
|
207
|
-
Bitmap bmp = mDisplayed;
|
|
208
|
-
if (bmp == null || bmp.isRecycled()) {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
// Always scale the last good frame to the current bounds, so a buffer that lags the view size
|
|
212
|
-
// (mid-resize) is shown stretched rather than blank.
|
|
213
|
-
mSrc.set(0, 0, mDisplayedW, mDisplayedH);
|
|
214
|
-
mDst.set(0, 0, getWidth(), getHeight());
|
|
215
|
-
canvas.drawBitmap(bmp, mSrc, mDst, mPaint);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// --- Lifecycle -------------------------------------------------------------
|
|
219
|
-
|
|
220
|
-
@Override
|
|
221
|
-
protected void onAttachedToWindow() {
|
|
222
|
-
super.onAttachedToWindow();
|
|
223
|
-
mAttached = true;
|
|
224
|
-
if (!mEnabled && getWidth() > 0 && getHeight() > 0) {
|
|
225
|
-
enablePool(getWidth(), getHeight());
|
|
226
|
-
}
|
|
227
|
-
// Pick up anything that became ready while we were detached.
|
|
228
|
-
onNativeFrameReady();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
@Override
|
|
232
|
-
protected void onDetachedFromWindow() {
|
|
233
|
-
super.onDetachedFromWindow();
|
|
234
|
-
mAttached = false;
|
|
235
|
-
mEnabled = false;
|
|
236
|
-
mMainHandler.removeCallbacks(mConsume);
|
|
237
|
-
mConsumePosted.set(false);
|
|
238
|
-
|
|
239
|
-
// Keep the canvas alive offscreen, then drop all GPU resources. The
|
|
240
|
-
// SurfaceInfo stays registered so a transient detach/re-attach keeps
|
|
241
|
-
// working; WebGPUViewManager.onDropViewInstance removes it when RN drops
|
|
242
|
-
// the view for good.
|
|
243
|
-
nSwitchToOffscreen(contextId());
|
|
244
|
-
|
|
245
|
-
mDisplayed = null;
|
|
246
|
-
mHeld.clear();
|
|
247
|
-
for (Bitmap b : mBitmaps.values()) {
|
|
248
|
-
if (!b.isRecycled()) {
|
|
249
|
-
b.recycle();
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
mBitmaps.clear();
|
|
253
|
-
mCurrentGen = 0;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
@Override
|
|
257
|
-
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
|
|
258
|
-
super.onVisibilityChanged(changedView, visibility);
|
|
259
|
-
if (visibility == VISIBLE && mAttached && !mEnabled
|
|
260
|
-
&& getWidth() > 0 && getHeight() > 0) {
|
|
261
|
-
enablePool(getWidth(), getHeight());
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// --- Native (cpp-adapter.cpp) ----------------------------------------------
|
|
266
|
-
|
|
267
|
-
private native void nEnablePool(int contextId, int dpW, int dpH);
|
|
268
|
-
|
|
269
|
-
private native void nSetClientSize(int contextId, int dpW, int dpH);
|
|
270
|
-
|
|
271
|
-
private native long nPollReady(int contextId);
|
|
272
|
-
|
|
273
|
-
private native HardwareBuffer nGetHardwareBuffer(int contextId, int generation, int slot);
|
|
274
|
-
|
|
275
|
-
private native void nReleaseSlot(int contextId, int generation, int slot);
|
|
276
|
-
|
|
277
|
-
private native void nSwitchToOffscreen(int contextId);
|
|
278
|
-
}
|