react-native-webgpu 0.16.0 → 0.16.1

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.
Files changed (38) hide show
  1. package/android/build.gradle +11 -1
  2. package/android/src/main/java/com/webgpu/WebGPUView.java +84 -34
  3. package/cpp/jsi/NativeObject.h +45 -5
  4. package/cpp/rnwgpu/RNWebGPUManager.cpp +49 -43
  5. package/cpp/rnwgpu/RNWebGPUManager.h +10 -0
  6. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +1 -0
  7. package/lib/commonjs/Canvas.js +51 -0
  8. package/lib/commonjs/Canvas.js.map +1 -1
  9. package/lib/commonjs/external/reanimated/registerWebGPUForReanimated.js.map +1 -1
  10. package/lib/commonjs/install.js +26 -7
  11. package/lib/commonjs/install.js.map +1 -1
  12. package/lib/module/Canvas.js +51 -0
  13. package/lib/module/Canvas.js.map +1 -1
  14. package/lib/module/external/reanimated/registerWebGPUForReanimated.js.map +1 -1
  15. package/lib/module/install.js +26 -7
  16. package/lib/module/install.js.map +1 -1
  17. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
  18. package/lib/typescript/lib/commonjs/install.d.ts +10 -7
  19. package/lib/typescript/lib/commonjs/install.d.ts.map +1 -1
  20. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  21. package/lib/typescript/lib/module/install.d.ts.map +1 -1
  22. package/lib/typescript/src/Canvas.d.ts +18 -4
  23. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  24. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts +1 -0
  25. package/lib/typescript/src/external/reanimated/registerWebGPUForReanimated.d.ts.map +1 -1
  26. package/lib/typescript/src/install.d.ts +10 -7
  27. package/lib/typescript/src/install.d.ts.map +1 -1
  28. package/lib/typescript/src/types.d.ts +1 -1
  29. package/package.json +6 -5
  30. package/plugin/build/index.js +2 -1
  31. package/plugin/build/setMinSdkVersion.d.ts +9 -0
  32. package/plugin/build/setMinSdkVersion.js +25 -0
  33. package/plugin/build/withMinSdkVersion.d.ts +11 -0
  34. package/plugin/build/withMinSdkVersion.js +20 -0
  35. package/src/Canvas.tsx +71 -4
  36. package/src/external/reanimated/registerWebGPUForReanimated.ts +4 -0
  37. package/src/install.ts +26 -7
  38. package/src/types.ts +1 -1
@@ -75,6 +75,16 @@ def getExtOrIntegerDefault(name) {
75
75
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Wgpu_" + name]).toInteger()
76
76
  }
77
77
 
78
+ // androidMinSdkVersion in package.json is the lowest API level the native code
79
+ // builds for: it calls the AHardwareBuffer API, from Android 8 (API 26). An
80
+ // app's lower minSdkVersion does not lower it, so such an app gets the
81
+ // manifest merger's error, with its suggested fix, instead of the compiler's.
82
+ def resolveMinSdkVersion() {
83
+ def floor = new groovy.json.JsonSlurper().parse(file("../package.json")).androidMinSdkVersion as Integer
84
+ def app = rootProject.ext.has("minSdkVersion") ? rootProject.ext.get("minSdkVersion").toString().toInteger() : floor
85
+ return Math.max(app, floor)
86
+ }
87
+
78
88
  def supportsNamespace() {
79
89
  def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
80
90
  def major = parsed[0].toInteger()
@@ -88,7 +98,7 @@ android {
88
98
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
89
99
 
90
100
  defaultConfig {
91
- minSdkVersion getExtOrIntegerDefault("minSdkVersion")
101
+ minSdkVersion resolveMinSdkVersion()
92
102
  targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
93
103
  buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", "true"
94
104
  externalNativeBuild {
@@ -2,6 +2,7 @@ package com.webgpu;
2
2
 
3
3
  import android.content.Context;
4
4
  import android.os.Build;
5
+ import android.util.Log;
5
6
  import android.view.Surface;
6
7
  import android.view.View;
7
8
 
@@ -10,6 +11,7 @@ import com.facebook.react.uimanager.ThemedReactContext;
10
11
  import com.facebook.react.views.view.ReactViewGroup;
11
12
 
12
13
  public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
14
+ private static final String TAG = "WebGPUView";
13
15
 
14
16
  // Backing view kinds, see updateView().
15
17
  private static final int KIND_SURFACE_VIEW = 0;
@@ -17,13 +19,18 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
17
19
  private static final int KIND_HARDWARE_BUFFER_VIEW = 2;
18
20
 
19
21
  private int mContextId;
22
+ // Props. opaque is applied in place, and so is zOrderOnTop where Android
23
+ // allows it (see setZOrderOnTop); surfaceType is read once, when the
24
+ // backing view is created (see updateView()).
20
25
  private boolean mOpaque = true;
21
26
  private String mSurfaceType = "auto";
22
27
  private boolean mZOrderOnTop = false;
23
- private int mAppliedKind = -1;
24
- private boolean mAppliedZOrderOnTop;
25
28
  private WebGPUModule mModule;
29
+ // The backing view, created by the first prop transaction and kept, with
30
+ // the kind it was created with. The kind is -1 until then: it is what
31
+ // tells a mounted canvas apart.
26
32
  private View mView = null;
33
+ private int mKind = -1;
27
34
  // Only a SurfaceView votes: a TextureView or HardwareBufferView is
28
35
  // composited into the window, which votes for it.
29
36
  private final SurfaceFrameRateVote mFrameRateVote = new SurfaceFrameRateVote(
@@ -55,12 +62,52 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
55
62
  mOpaque = value;
56
63
  }
57
64
 
65
+ // The backing view is chosen from the props of the first transaction and
66
+ // kept (see updateView()). React calls a setter again only when its prop
67
+ // changed, so a call on a mounted canvas is a change: the setters below
68
+ // apply it in place where the view can take it, else they ignore it and
69
+ // say so (a canvas that needs another backing view is remounted, with a
70
+ // new key). The JS side warns about the same changes in development (see
71
+ // Canvas.tsx), mirroring these rules.
72
+
58
73
  public void setSurfaceType(String value) {
74
+ if (mKind != -1) {
75
+ warnIgnoredOnceMounted("android.surfaceType");
76
+ return;
77
+ }
59
78
  mSurfaceType = value;
60
79
  }
61
80
 
62
81
  public void setZOrderOnTop(boolean value) {
82
+ if (mKind == -1) {
83
+ mZOrderOnTop = value;
84
+ return;
85
+ }
86
+ // Only a SurfaceView has a z-order of its own: elsewhere the prop never
87
+ // applied, and the change is moot.
88
+ if (mKind != KIND_SURFACE_VIEW) {
89
+ return;
90
+ }
91
+ if (!zOrderChangesInPlace()) {
92
+ warnIgnoredOnceMounted("android.zOrderOnTop");
93
+ return;
94
+ }
63
95
  mZOrderOnTop = value;
96
+ ((WebGPUSurfaceView) mView).setZOrderOnTop(value);
97
+ }
98
+
99
+ private void warnIgnoredOnceMounted(String prop) {
100
+ Log.w(TAG, "Ignoring a change of " + prop + " on a mounted canvas: the backing view"
101
+ + " is chosen when the canvas mounts. Remount the canvas (give it a new key)"
102
+ + " to change it.");
103
+ }
104
+
105
+ // SurfaceView.setZOrderOnTop() moves a live surface since Android 11, for
106
+ // an app targeting it; before that, the surface reads it when it is
107
+ // created (see updateView()).
108
+ private boolean zOrderChangesInPlace() {
109
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
110
+ && getContext().getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R;
64
111
  }
65
112
 
66
113
  // Resolve the backing view from the props. "auto" picks SurfaceView for an
@@ -84,41 +131,44 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
84
131
  }
85
132
 
86
133
  // Apply the complete prop transaction once, after contextId and all rendering
87
- // options have arrived. Only a change of backing view (or of zOrderOnTop,
88
- // which a SurfaceView must know before it attaches) replaces the child;
89
- // opacity is applied in place.
134
+ // options have arrived. The first one creates the backing view, which is
135
+ // then kept: a SurfaceView must know its z-order before it attaches (before
136
+ // Android 11), and replacing a backing view on screen hands the canvas from
137
+ // one view to another, which no release of Android does without a frame of
138
+ // nothing, or of black, in between (the two views are composited by
139
+ // different frames). A canvas that needs another backing view is remounted
140
+ // instead. Later transactions apply the opacity in place (the setters decide
141
+ // what else is taken); WebGPUHardwareBufferView always composites with
142
+ // alpha, so there it is a no-op.
90
143
  public void updateView() {
144
+ if (mKind != -1) {
145
+ if (mKind == KIND_TEXTURE_VIEW) {
146
+ ((WebGPUTextureView) mView).setOpaque(mOpaque);
147
+ } else if (mKind == KIND_SURFACE_VIEW) {
148
+ ((WebGPUSurfaceView) mView).setOpaque(mOpaque);
149
+ }
150
+ return;
151
+ }
91
152
  int kind = resolveKind();
92
153
  boolean zOrderOnTop = kind == KIND_SURFACE_VIEW && mZOrderOnTop;
93
- if (mView == null || kind != mAppliedKind || zOrderOnTop != mAppliedZOrderOnTop) {
94
- if (mView != null) {
95
- removeView(mView);
96
- }
97
- mAppliedKind = kind;
98
- mAppliedZOrderOnTop = zOrderOnTop;
99
- Context ctx = getContext();
100
- switch (kind) {
101
- case KIND_HARDWARE_BUFFER_VIEW:
102
- mView = new WebGPUHardwareBufferView(ctx, this);
103
- break;
104
- case KIND_TEXTURE_VIEW:
105
- mView = new WebGPUTextureView(ctx, this, mOpaque);
106
- break;
107
- default:
108
- mView = new WebGPUSurfaceView(ctx, this, zOrderOnTop, mOpaque);
109
- break;
110
- }
111
- addView(mView);
112
- // ReactViewGroup.requestLayout() is a deliberate no-op, so addView outside
113
- // the layout pass never lays the child out; do it by hand or the new view
114
- // stays 0x0 and never gets a surface.
115
- layoutChild();
116
- } else if (kind == KIND_TEXTURE_VIEW) {
117
- ((WebGPUTextureView) mView).setOpaque(mOpaque);
118
- } else if (kind == KIND_SURFACE_VIEW) {
119
- ((WebGPUSurfaceView) mView).setOpaque(mOpaque);
154
+ mKind = kind;
155
+ Context ctx = getContext();
156
+ switch (kind) {
157
+ case KIND_HARDWARE_BUFFER_VIEW:
158
+ mView = new WebGPUHardwareBufferView(ctx, this);
159
+ break;
160
+ case KIND_TEXTURE_VIEW:
161
+ mView = new WebGPUTextureView(ctx, this, mOpaque);
162
+ break;
163
+ default:
164
+ mView = new WebGPUSurfaceView(ctx, this, zOrderOnTop, mOpaque);
165
+ break;
120
166
  }
121
- // WebGPUHardwareBufferView always composites with alpha; opacity is a no-op.
167
+ addView(mView);
168
+ // ReactViewGroup.requestLayout() is a deliberate no-op, so addView outside
169
+ // the layout pass never lays the child out; do it by hand or the new view
170
+ // stays 0x0 and never gets a surface.
171
+ layoutChild();
122
172
  }
123
173
 
124
174
  private void layoutChild() {
@@ -138,7 +188,7 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
138
188
  float density = getResources().getDisplayMetrics().density;
139
189
  float width = getWidth() / density;
140
190
  float height = getHeight() / density;
141
- boolean votesFrameRate = mAppliedKind == KIND_SURFACE_VIEW;
191
+ boolean votesFrameRate = mKind == KIND_SURFACE_VIEW;
142
192
  if (votesFrameRate) {
143
193
  mFrameRateVote.attach(surface);
144
194
  }
@@ -179,6 +179,14 @@ private:
179
179
  * getters and Proxy traps of unrelated objects never run.
180
180
  * - box(value): wraps the object into a BoxedWebGPUObject, branded with its
181
181
  * class name, for unbox() on the target runtime.
182
+ * - installConstructors(): installs the constructor of every WebGPU class on
183
+ * the calling runtime, skipping the ones already there, so `instanceof`
184
+ * works against any class on that runtime, including classes no object of
185
+ * which was ever created there or crossed into it (unboxing installs only
186
+ * the class of the object that crosses). installWebGPU() calls it. The
187
+ * class list belongs to the API layer (RNWebGPUManager::installConstructors)
188
+ * and is handed in at construction: every API class includes this header,
189
+ * which therefore cannot include them back.
182
190
  *
183
191
  * One instance is installed on the main runtime as
184
192
  * globalThis.__webgpuWorkletHelpers and captured by the serializer's
@@ -189,8 +197,26 @@ private:
189
197
  */
190
198
  class WebGPUWorkletHelpers : public jsi::HostObject {
191
199
  public:
200
+ using InstallConstructors = void (*)(jsi::Runtime &);
201
+
202
+ explicit WebGPUWorkletHelpers(InstallConstructors installConstructors)
203
+ : _installConstructors(installConstructors) {}
204
+
192
205
  jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override {
193
206
  auto propName = name.utf8(runtime);
207
+ if (propName == "installConstructors") {
208
+ auto install = _installConstructors;
209
+ return jsi::Function::createFromHostFunction(
210
+ runtime, name, 0,
211
+ [install](jsi::Runtime &rt, const jsi::Value & /*thisVal*/,
212
+ const jsi::Value * /*args*/,
213
+ size_t /*count*/) -> jsi::Value {
214
+ if (install != nullptr) {
215
+ install(rt);
216
+ }
217
+ return jsi::Value::undefined();
218
+ });
219
+ }
194
220
  if (propName == "isNativeObject") {
195
221
  return jsi::Function::createFromHostFunction(
196
222
  runtime, name, 1,
@@ -230,11 +256,15 @@ public:
230
256
  std::vector<jsi::PropNameID>
231
257
  getPropertyNames(jsi::Runtime &runtime) override {
232
258
  std::vector<jsi::PropNameID> names;
233
- names.reserve(2);
259
+ names.reserve(3);
234
260
  names.push_back(jsi::PropNameID::forUtf8(runtime, "isNativeObject"));
235
261
  names.push_back(jsi::PropNameID::forUtf8(runtime, "box"));
262
+ names.push_back(jsi::PropNameID::forUtf8(runtime, "installConstructors"));
236
263
  return names;
237
264
  }
265
+
266
+ private:
267
+ InstallConstructors _installConstructors;
238
268
  };
239
269
 
240
270
  /**
@@ -378,6 +408,9 @@ public:
378
408
  * The constructor throws if called directly (these objects are only
379
409
  * created internally by the native code).
380
410
  *
411
+ * Idempotent per runtime: once the global exists there, later calls return
412
+ * without touching it, so the constructor object keeps its identity.
413
+ *
381
414
  * Also registers this class with NativeObjectRegistry so that
382
415
  * BoxedWebGPUObject::unbox() can rebuild its objects on secondary runtimes.
383
416
  */
@@ -394,10 +427,9 @@ public:
394
427
  if (instance == nullptr) {
395
428
  throw jsi::JSError(rt, "Invalid boxed native object state");
396
429
  }
397
- // The constructor makes `instanceof` work on this runtime too.
398
- if (!rt.global().hasProperty(rt, Derived::CLASS_NAME)) {
399
- Derived::installConstructor(rt);
400
- }
430
+ // The constructor makes `instanceof` work on this runtime too
431
+ // (a no-op once it is installed there).
432
+ Derived::installConstructor(rt);
401
433
  // Unboxing hands back the wrapper cached for the target runtime,
402
434
  // creating it on first use. Keep the runtime the object was
403
435
  // originally created on: async native code (GPUDevice error/lost
@@ -415,6 +447,14 @@ public:
415
447
 
416
448
  installPrototype(runtime);
417
449
 
450
+ // Once per runtime. installWebGPU() runs this at the top of every worklet
451
+ // invocation, and reinstalling would replace the global with a new
452
+ // function object each time (`const C = GPUBuffer; C === GPUBuffer`
453
+ // would be false).
454
+ if (runtime.global().hasProperty(runtime, Derived::CLASS_NAME)) {
455
+ return;
456
+ }
457
+
418
458
  auto *prototype = getCachedPrototype(runtime);
419
459
  if (prototype == nullptr) {
420
460
  return;
@@ -87,45 +87,7 @@ RNWebGPUManager::RNWebGPUManager(
87
87
  RNWebGPU::create(*_jsRuntime, rnWebGPU));
88
88
 
89
89
  // Install constructors for instanceof support
90
- GPU::installConstructor(*_jsRuntime);
91
- GPUAdapter::installConstructor(*_jsRuntime);
92
- GPUAdapterInfo::installConstructor(*_jsRuntime);
93
- GPUBindGroup::installConstructor(*_jsRuntime);
94
- GPUBindGroupLayout::installConstructor(*_jsRuntime);
95
- GPUBuffer::installConstructor(*_jsRuntime);
96
- GPUCanvasContext::installConstructor(*_jsRuntime);
97
- GPUCommandBuffer::installConstructor(*_jsRuntime);
98
- GPUCommandEncoder::installConstructor(*_jsRuntime);
99
- GPUCompilationInfo::installConstructor(*_jsRuntime);
100
- GPUCompilationMessage::installConstructor(*_jsRuntime);
101
- GPUComputePassEncoder::installConstructor(*_jsRuntime);
102
- GPUComputePipeline::installConstructor(*_jsRuntime);
103
- GPUDevice::installConstructor(*_jsRuntime);
104
- GPUDeviceLostInfo::installConstructor(*_jsRuntime);
105
- GPUError::installConstructor(*_jsRuntime);
106
- GPUExternalTexture::installConstructor(*_jsRuntime);
107
- GPUNativeBufferExternalTexture::installConstructor(*_jsRuntime);
108
- GPUNativeBufferTexture::installConstructor(*_jsRuntime);
109
- GPUInternalError::installConstructor(*_jsRuntime);
110
- GPUOutOfMemoryError::installConstructor(*_jsRuntime);
111
- GPUValidationError::installConstructor(*_jsRuntime);
112
- GPUUncapturedErrorEvent::installConstructor(*_jsRuntime);
113
- GPUPipelineLayout::installConstructor(*_jsRuntime);
114
- GPUQuerySet::installConstructor(*_jsRuntime);
115
- GPUQueue::installConstructor(*_jsRuntime);
116
- GPURenderBundle::installConstructor(*_jsRuntime);
117
- GPURenderBundleEncoder::installConstructor(*_jsRuntime);
118
- GPURenderPassEncoder::installConstructor(*_jsRuntime);
119
- GPURenderPipeline::installConstructor(*_jsRuntime);
120
- GPUSampler::installConstructor(*_jsRuntime);
121
- GPUSharedFence::installConstructor(*_jsRuntime);
122
- GPUSharedTextureMemory::installConstructor(*_jsRuntime);
123
- GPUShaderModule::installConstructor(*_jsRuntime);
124
- GPUSupportedLimits::installConstructor(*_jsRuntime);
125
- GPUTexture::installConstructor(*_jsRuntime);
126
- GPUTextureView::installConstructor(*_jsRuntime);
127
- VideoFrame::installConstructor(*_jsRuntime);
128
- VideoPlayer::installConstructor(*_jsRuntime);
90
+ installConstructors(*_jsRuntime);
129
91
 
130
92
  // Install constant objects as plain JS objects with own properties
131
93
  _jsRuntime->global().setProperty(*_jsRuntime, "GPUBufferUsage",
@@ -144,14 +106,58 @@ RNWebGPUManager::RNWebGPUManager(
144
106
  installWebGPUWorkletHelpers(*_jsRuntime);
145
107
  }
146
108
 
109
+ void RNWebGPUManager::installConstructors(jsi::Runtime &runtime) {
110
+ GPU::installConstructor(runtime);
111
+ GPUAdapter::installConstructor(runtime);
112
+ GPUAdapterInfo::installConstructor(runtime);
113
+ GPUBindGroup::installConstructor(runtime);
114
+ GPUBindGroupLayout::installConstructor(runtime);
115
+ GPUBuffer::installConstructor(runtime);
116
+ GPUCanvasContext::installConstructor(runtime);
117
+ GPUCommandBuffer::installConstructor(runtime);
118
+ GPUCommandEncoder::installConstructor(runtime);
119
+ GPUCompilationInfo::installConstructor(runtime);
120
+ GPUCompilationMessage::installConstructor(runtime);
121
+ GPUComputePassEncoder::installConstructor(runtime);
122
+ GPUComputePipeline::installConstructor(runtime);
123
+ GPUDevice::installConstructor(runtime);
124
+ GPUDeviceLostInfo::installConstructor(runtime);
125
+ GPUError::installConstructor(runtime);
126
+ GPUExternalTexture::installConstructor(runtime);
127
+ GPUNativeBufferExternalTexture::installConstructor(runtime);
128
+ GPUNativeBufferTexture::installConstructor(runtime);
129
+ GPUInternalError::installConstructor(runtime);
130
+ GPUOutOfMemoryError::installConstructor(runtime);
131
+ GPUValidationError::installConstructor(runtime);
132
+ GPUUncapturedErrorEvent::installConstructor(runtime);
133
+ GPUPipelineLayout::installConstructor(runtime);
134
+ GPUQuerySet::installConstructor(runtime);
135
+ GPUQueue::installConstructor(runtime);
136
+ GPURenderBundle::installConstructor(runtime);
137
+ GPURenderBundleEncoder::installConstructor(runtime);
138
+ GPURenderPassEncoder::installConstructor(runtime);
139
+ GPURenderPipeline::installConstructor(runtime);
140
+ GPUSampler::installConstructor(runtime);
141
+ GPUSharedFence::installConstructor(runtime);
142
+ GPUSharedTextureMemory::installConstructor(runtime);
143
+ GPUShaderModule::installConstructor(runtime);
144
+ GPUSupportedLimits::installConstructor(runtime);
145
+ GPUTexture::installConstructor(runtime);
146
+ GPUTextureView::installConstructor(runtime);
147
+ VideoFrame::installConstructor(runtime);
148
+ VideoPlayer::installConstructor(runtime);
149
+ }
150
+
147
151
  void RNWebGPUManager::installWebGPUWorkletHelpers(jsi::Runtime &runtime) {
148
- // The serializer worklets capture this object by closure; worklets passes a
149
- // HostObject to every runtime by reference, so installing it here, on the
150
- // main runtime, is enough (see WebGPUWorkletHelpers).
152
+ // The serializer worklets and installWebGPU() capture this object by
153
+ // closure; worklets passes a HostObject to every runtime by reference, so
154
+ // installing it here, on the main runtime, is enough (see
155
+ // WebGPUWorkletHelpers).
151
156
  runtime.global().setProperty(
152
157
  runtime, "__webgpuWorkletHelpers",
153
158
  jsi::Object::createFromHostObject(
154
- runtime, std::make_shared<WebGPUWorkletHelpers>()));
159
+ runtime, std::make_shared<WebGPUWorkletHelpers>(
160
+ &RNWebGPUManager::installConstructors)));
155
161
  }
156
162
 
157
163
  void RNWebGPUManager::flushPendingSurfaceTransition(
@@ -35,6 +35,16 @@ public:
35
35
  */
36
36
  static void installWebGPUWorkletHelpers(jsi::Runtime &runtime);
37
37
 
38
+ /**
39
+ * Installs the constructor of every WebGPU API class on `runtime` (see
40
+ * NativeObject::installConstructor), skipping the ones already there. Runs
41
+ * on the main runtime at startup and, through
42
+ * __webgpuWorkletHelpers.installConstructors(), on every runtime that calls
43
+ * installWebGPU(). RNWebGPU itself is not part of it: that global is the
44
+ * module instance, main runtime only.
45
+ */
46
+ static void installConstructors(jsi::Runtime &runtime);
47
+
38
48
  /**
39
49
  * Applies a surface attach latched by the platform UI thread (see
40
50
  * SurfaceInfo::applyPendingAttach) from the JS thread. Surface attaches are
@@ -2,6 +2,7 @@
2
2
 
3
3
  #include <memory>
4
4
  #include <string>
5
+ #include <utility>
5
6
 
6
7
  #include <jsi/jsi.h>
7
8
 
@@ -14,10 +14,57 @@ let CONTEXT_COUNTER = 1;
14
14
  function generateContextId() {
15
15
  return CONTEXT_COUNTER++;
16
16
  }
17
+
18
+ /**
19
+ * Android-only rendering options. They select the backing view, which is
20
+ * chosen when the canvas mounts and kept: a later change is ignored, with a
21
+ * warning in development. Remount the canvas, with a `key`, to change it.
22
+ */
23
+
17
24
  // Anything else reaching the native component would hit the generated
18
25
  // string-enum parser, which aborts on unknown values.
19
26
  const resolveSurfaceType = surfaceType => surfaceType === "SurfaceView" || surfaceType === "TextureView" || surfaceType === "HardwareBufferView" ? surfaceType : "auto";
20
27
 
28
+ // The props the Android backing view is chosen from when the canvas mounts.
29
+
30
+ // The backing view the props select, see WebGPUView.resolveKind().
31
+ const resolveKind = ({
32
+ opaque,
33
+ android
34
+ }) => {
35
+ const surfaceType = resolveSurfaceType(android?.surfaceType);
36
+ if (surfaceType !== "auto") {
37
+ return surfaceType;
38
+ }
39
+ return opaque ? "SurfaceView" : "TextureView";
40
+ };
41
+ const warnIgnored = prop => console.warn(`Ignoring a change of ${prop} on a mounted canvas: the backing view is ` + "chosen when the canvas mounts. Remount the canvas (give it a new key) " + "to change it.");
42
+
43
+ // Warns, in development on Android, about a change of these props that the
44
+ // native view ignores: the backing view is chosen from them when the canvas
45
+ // mounts (see WebGPUView.java, whose setters these rules mirror). The native
46
+ // side only says so in logcat, which is not where a JS developer looks.
47
+ const useBackingViewWarning = props => {
48
+ const mount = (0, _react.useRef)(props);
49
+ const prev = (0, _react.useRef)(props);
50
+ (0, _react.useEffect)(() => {
51
+ if (__DEV__ && _reactNative.Platform.OS === "android") {
52
+ const {
53
+ current: before
54
+ } = prev;
55
+ if (resolveSurfaceType(before.android?.surfaceType) !== resolveSurfaceType(props.android?.surfaceType)) {
56
+ warnIgnored("android.surfaceType");
57
+ }
58
+ // Only a SurfaceView has a z-order of its own, which Android 11 and
59
+ // later changes in place.
60
+ if (!!before.android?.zOrderOnTop !== !!props.android?.zOrderOnTop && resolveKind(mount.current) === "SurfaceView" && Number(_reactNative.Platform.Version) < 30) {
61
+ warnIgnored("android.zOrderOnTop");
62
+ }
63
+ }
64
+ prev.current = props;
65
+ });
66
+ };
67
+
21
68
  // A view measured relative to itself gets its layout size, without the
22
69
  // transforms getBoundingClientRect() includes; measureLayout() calls back
23
70
  // synchronously on the new architecture. On the web react-native-web measures
@@ -55,6 +102,10 @@ const Canvas = ({
55
102
  }) => {
56
103
  const viewRef = (0, _react.useRef)(null);
57
104
  const [contextId, _] = (0, _react.useState)(() => generateContextId());
105
+ useBackingViewWarning({
106
+ opaque,
107
+ android
108
+ });
58
109
  // Retire the native registry entry for this contextId on unmount. When a
59
110
  // native surface is still attached, this is a no-op and the native view's
60
111
  // own teardown retires the entry instead — which keeps StrictMode's
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_WebGPUViewNativeComponent","_interopRequireDefault","e","__esModule","default","t","WeakMap","r","n","o","i","f","__proto__","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","_extends","assign","bind","arguments","length","apply","CONTEXT_COUNTER","generateContextId","resolveSurfaceType","surfaceType","measureLayout","view","Platform","OS","clientWidth","clientHeight","width","height","size","_left","_top","Canvas","opaque","android","ref","props","viewRef","useRef","contextId","_","useState","useEffect","RNWebGPU","destroyContext","useImperativeHandle","getContextId","getNativeSurface","getContext","contextName","Error","current","MakeWebGPUCanvasContext","createElement","View","collapsable","style","flex","androidSurfaceType","androidZOrderOnTop","zOrderOnTop","exports"],"sourceRoot":"../../src","sources":["Canvas.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAEA,IAAAC,YAAA,GAAAD,OAAA;AAGA,IAAAE,0BAAA,GAAAC,sBAAA,CAAAH,OAAA;AAA2D,SAAAG,uBAAAC,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAAA,SAAAL,wBAAAK,CAAA,EAAAG,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAT,uBAAA,YAAAA,CAAAK,CAAA,EAAAG,CAAA,SAAAA,CAAA,IAAAH,CAAA,IAAAA,CAAA,CAAAC,UAAA,SAAAD,CAAA,MAAAO,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAR,OAAA,EAAAF,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAS,CAAA,MAAAF,CAAA,GAAAJ,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAE,CAAA,CAAAI,GAAA,CAAAX,CAAA,UAAAO,CAAA,CAAAK,GAAA,CAAAZ,CAAA,GAAAO,CAAA,CAAAM,GAAA,CAAAb,CAAA,EAAAS,CAAA,gBAAAN,CAAA,IAAAH,CAAA,gBAAAG,CAAA,OAAAW,cAAA,CAAAC,IAAA,CAAAf,CAAA,EAAAG,CAAA,OAAAK,CAAA,IAAAD,CAAA,GAAAS,MAAA,CAAAC,cAAA,KAAAD,MAAA,CAAAE,wBAAA,CAAAlB,CAAA,EAAAG,CAAA,OAAAK,CAAA,CAAAI,GAAA,IAAAJ,CAAA,CAAAK,GAAA,IAAAN,CA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OAAA,CAAA2D,aAAA,CAAChE,YAAA,CAAAiE,IAAI,EAAA3C,QAAA;IAAC4C,WAAW,EAAE,KAAM;IAACpB,GAAG,EAAEE;EAAQ,GAAKD,KAAK,gBAC/ClD,MAAA,CAAAQ,OAAA,CAAA2D,aAAA,CAAC/D,0BAAA,CAAAI,OAAgB;IACf8D,KAAK,EAAE;MAAEC,IAAI,EAAE;IAAE,CAAE;IACnBlB,SAAS,EAAEA,SAAU;IACrBN,MAAM,EAAEA,MAAO;IACfyB,kBAAkB,EAAEvC,kBAAkB,CAACe,OAAO,EAAEd,WAAW,CAAE;IAC7DuC,kBAAkB,EAAE,CAAC,CAACzB,OAAO,EAAE0B;EAAY,CAC5C,CACG,CAAC;AAEX,CAAC;AAACC,OAAA,CAAA7B,MAAA,GAAAA,MAAA","ignoreList":[]}
1
+ {"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_WebGPUViewNativeComponent","_interopRequireDefault","e","__esModule","default","t","WeakMap","r","n","o","i","f","__proto__","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","_extends","assign","bind","arguments","length","apply","CONTEXT_COUNTER","generateContextId","resolveSurfaceType","surfaceType","resolveKind","opaque","android","warnIgnored","prop","console","warn","useBackingViewWarning","props","mount","useRef","prev","useEffect","__DEV__","Platform","OS","current","before","zOrderOnTop","Number","Version","measureLayout","view","clientWidth","clientHeight","width","height","size","_left","_top","Canvas","ref","viewRef","contextId","_","useState","RNWebGPU","destroyContext","useImperativeHandle","getContextId","getNativeSurface","getContext","contextName","Error","MakeWebGPUCanvasContext","createElement","View","collapsable","style","flex","androidSurfaceType","androidZOrderOnTop","exports"],"sourceRoot":"../../src","sources":["Canvas.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAEA,IAAAC,YAAA,GAAAD,OAAA;AAGA,IAAAE,0BAAA,GAAAC,sBAAA,CAAAH,OAAA;AAA2D,SAAAG,uBAAAC,CAAA,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AAE;MAAEC,IAAI,EAAE;IAAE,CAAE;IACnBhB,SAAS,EAAEA,SAAU;IACrBhC,MAAM,EAAEA,MAAO;IACfiD,kBAAkB,EAAEpD,kBAAkB,CAACI,OAAO,EAAEH,WAAW,CAAE;IAC7DoD,kBAAkB,EAAE,CAAC,CAACjD,OAAO,EAAEgB;EAAY,CAC5C,CACG,CAAC;AAEX,CAAC;AAACkC,OAAA,CAAAtB,MAAA,GAAAA,MAAA","ignoreList":[]}
@@ -1 +1 @@
1
- {"version":3,"names":["isRegistered","registerWebGPUForReanimated","helpers","globalThis","__webgpuWorkletHelpers","undefined","registerCustomSerializable","require","name","determine","value","isNativeObject","pack","box","unpack","boxed","unbox","exports"],"sourceRoot":"../../../../src","sources":["external/reanimated/registerWebGPUForReanimated.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;;AAkBA,IAAIA,YAAY,GAAG,KAAK;;AAExB;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,2BAA2B,GAAGA,CAAA,KAAM;EAC/C,IAAID,YAAY,EAAE;IAChB;EACF;EACAA,YAAY,GAAG,IAAI;EAEnB,MAAME,OAAO,GAAGC,UAAU,CAACC,sBAAsB;EACjD,IAAIF,OAAO,KAAKG,SAAS,EAAE;IACzB;IACA;EACF;EAEA,IAAI;IACF,MAAM;MAAEC;IAA2B,CAAC,GAAGC,OAAO,CAAC,uBAAuB,CAAC;IAEvED,0BAA0B,CAAC;MACzBE,IAAI,EAAE,QAAQ;MACdC,SAAS,EAAGC,KAAa,IAAsB;QAC7C,SAAS;;QACT,OAAOR,OAAO,CAACS,cAAc,CAACD,KAAK,CAAC;MACtC,CAAC;MACDE,IAAI,EAAGF,KAAa,IAAK;QACvB,SAAS;;QACT,OAAOR,OAAO,CAACW,GAAG,CAACH,KAAK,CAAC;MAC3B,CAAC;MACDI,MAAM,EAAGC,KAAwB,IAAK;QACpC,SAAS;;QACT,OAAOA,KAAK,CAACC,KAAK,CAAC,CAAC;MACtB;IACF,CAAC,CAAC;EACJ,CAAC,CAAC,MAAM;IACN;EAAA;AAEJ,CAAC;AAACC,OAAA,CAAAhB,2BAAA,GAAAA,2BAAA","ignoreList":[]}
1
+ {"version":3,"names":["isRegistered","registerWebGPUForReanimated","helpers","globalThis","__webgpuWorkletHelpers","undefined","registerCustomSerializable","require","name","determine","value","isNativeObject","pack","box","unpack","boxed","unbox","exports"],"sourceRoot":"../../../../src","sources":["external/reanimated/registerWebGPUForReanimated.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;;AAsBA,IAAIA,YAAY,GAAG,KAAK;;AAExB;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,2BAA2B,GAAGA,CAAA,KAAM;EAC/C,IAAID,YAAY,EAAE;IAChB;EACF;EACAA,YAAY,GAAG,IAAI;EAEnB,MAAME,OAAO,GAAGC,UAAU,CAACC,sBAAsB;EACjD,IAAIF,OAAO,KAAKG,SAAS,EAAE;IACzB;IACA;EACF;EAEA,IAAI;IACF,MAAM;MAAEC;IAA2B,CAAC,GAAGC,OAAO,CAAC,uBAAuB,CAAC;IAEvED,0BAA0B,CAAC;MACzBE,IAAI,EAAE,QAAQ;MACdC,SAAS,EAAGC,KAAa,IAAsB;QAC7C,SAAS;;QACT,OAAOR,OAAO,CAACS,cAAc,CAACD,KAAK,CAAC;MACtC,CAAC;MACDE,IAAI,EAAGF,KAAa,IAAK;QACvB,SAAS;;QACT,OAAOR,OAAO,CAACW,GAAG,CAACH,KAAK,CAAC;MAC3B,CAAC;MACDI,MAAM,EAAGC,KAAwB,IAAK;QACpC,SAAS;;QACT,OAAOA,KAAK,CAACC,KAAK,CAAC,CAAC;MACtB;IACF,CAAC,CAAC;EACJ,CAAC,CAAC,MAAM;IACN;EAAA;AAEJ,CAAC;AAACC,OAAA,CAAAhB,2BAAA,GAAAA,2BAAA","ignoreList":[]}
@@ -28,14 +28,25 @@ const constants = {
28
28
  // runtime installs the GPU prototype there.
29
29
  const gpu = typeof RNWebGPU === "undefined" ? undefined : RNWebGPU.gpu;
30
30
 
31
+ // The native half of the worklet serializer (see registerWebGPUForReanimated),
32
+ // read from the main runtime like `gpu` above and captured into the worklet
33
+ // closure: worklets hands a HostObject to every runtime by reference, so it is
34
+ // callable on the target runtime without installing anything there. Its
35
+ // installConstructors() installs the WebGPU class constructors on the runtime
36
+ // that calls it. Undefined on web and node, where the native module is not
37
+ // installed.
38
+ const helpers = globalThis.__webgpuWorkletHelpers;
39
+
31
40
  /**
32
41
  * Install WebGPU on the runtime that calls it.
33
42
  *
34
43
  * The native module sets up WebGPU on the main JS runtime, but worklet
35
44
  * runtimes (Reanimated UI, dedicated worklet runtimes, Vision Camera frame
36
- * processors) start without it: `navigator.gpu` and the flag constants
45
+ * processors) start without it: `navigator.gpu`, the flag constants
37
46
  * (`GPUBufferUsage`, `GPUTextureUsage`, `GPUShaderStage`, `GPUColorWrite`,
38
- * `GPUMapMode`) are all `undefined` there.
47
+ * `GPUMapMode`) and the class constructors (`GPUBuffer`, `GPUTexture`,
48
+ * `GPURenderBundleEncoder`, ...) are all `undefined` there. Without the
49
+ * constructors, `instanceof` against a WebGPU class throws a ReferenceError.
39
50
  *
40
51
  * Call `installWebGPU()` once at the top of a worklet to make them available:
41
52
  *
@@ -64,12 +75,13 @@ const gpu = typeof RNWebGPU === "undefined" ? undefined : RNWebGPU.gpu;
64
75
  * Observe those on a device created on the main JS thread.
65
76
  *
66
77
  * Everything is captured into the worklet by closure: the constants like a
67
- * shader string would be, and the GPU object through the Worklets custom
78
+ * shader string would be, the GPU object through the Worklets custom
68
79
  * serializer, which installs the native prototypes on the target runtime when
69
- * it crosses. Promises returned by `requestAdapter`/`requestDevice` settle on
70
- * the calling runtime (each runtime gets its own async pump). Calling it on a
71
- * runtime that already has the globals (e.g. the main JS runtime) is a safe
72
- * no-op.
80
+ * it crosses, and the native helper that installs the class constructors,
81
+ * which worklets hands to every runtime by reference. Promises returned by
82
+ * `requestAdapter`/`requestDevice` settle on the calling runtime (each
83
+ * runtime gets its own async pump). Calling it on a runtime that already has
84
+ * the globals (e.g. the main JS runtime) is a safe no-op.
73
85
  */
74
86
  const installWebGPU = () => {
75
87
  "worklet";
@@ -91,6 +103,13 @@ const installWebGPU = () => {
91
103
  nav.gpu = gpu;
92
104
  }
93
105
  }
106
+ // The class constructors, so `instanceof` works against any WebGPU class on
107
+ // this runtime (three.js checks `pass instanceof GPURenderBundleEncoder` on
108
+ // every render, for instance). Unboxing an object installs only its own
109
+ // class; this installs the rest and skips the ones already there.
110
+ if (helpers !== undefined) {
111
+ helpers.installConstructors();
112
+ }
94
113
  };
95
114
  exports.installWebGPU = installWebGPU;
96
115
  //# sourceMappingURL=install.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_constants","require","constants","GPUBufferUsage","GPUTextureUsage","GPUShaderStage","GPUColorWrite","GPUMapMode","gpu","RNWebGPU","undefined","installWebGPU","g","globalThis","key","value","Object","entries","nav","navigator","userAgent","exports"],"sourceRoot":"../../src","sources":["install.ts"],"mappings":";;;;;;AACA,IAAAA,UAAA,GAAAC,OAAA;AADA;;AASA;AACA;AACA;AACA;AACA,MAAMC,SAAS,GAAG;EAChBC,cAAc,EAAdA,yBAAc;EACdC,eAAe,EAAfA,0BAAe;EACfC,cAAc,EAAdA,yBAAc;EACdC,aAAa,EAAbA,wBAAa;EACbC,UAAU,EAAVA;AACF,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,GAAoB,GACxB,OAAOC,QAAQ,KAAK,WAAW,GAAGC,SAAS,GAAGD,QAAQ,CAACD,GAAG;;AAE5D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMG,aAAa,GAAGA,CAAA,KAAM;EACjC,SAAS;;EACT,MAAMC,CAAC,GAAGC,UAAgD;EAC1D,KAAK,MAAM,CAACC,GAAG,EAAEC,KAAK,CAAC,IAAIC,MAAM,CAACC,OAAO,CAACf,SAAS,CAAC,EAAE;IACpD,IAAIU,CAAC,CAACE,GAAG,CAAC,KAAKJ,SAAS,EAAE;MACxBE,CAAC,CAACE,GAAG,CAAC,GAAGC,KAAK;IAChB;EACF;EACA,IAAIP,GAAG,KAAKE,SAAS,EAAE;IACrB,MAAMQ,GAAG,GAAGN,CAAC,CAACO,SAA0D;IACxE,IAAID,GAAG,KAAKR,SAAS,EAAE;MACrBE,CAAC,CAACO,SAAS,GAAG;QAAEX,GAAG;QAAEY,SAAS,EAAE;MAAe,CAAC;IAClD,CAAC,MAAM,IAAIF,GAAG,CAACV,GAAG,KAAKE,SAAS,EAAE;MAChCQ,GAAG,CAACV,GAAG,GAAGA,GAAG;IACf;EACF;AACF,CAAC;AAACa,OAAA,CAAAV,aAAA,GAAAA,aAAA","ignoreList":[]}
1
+ {"version":3,"names":["_constants","require","constants","GPUBufferUsage","GPUTextureUsage","GPUShaderStage","GPUColorWrite","GPUMapMode","gpu","RNWebGPU","undefined","helpers","globalThis","__webgpuWorkletHelpers","installWebGPU","g","key","value","Object","entries","nav","navigator","userAgent","installConstructors","exports"],"sourceRoot":"../../src","sources":["install.ts"],"mappings":";;;;;;AACA,IAAAA,UAAA,GAAAC,OAAA;AADA;;AASA;AACA;AACA;AACA;AACA,MAAMC,SAAS,GAAG;EAChBC,cAAc,EAAdA,yBAAc;EACdC,eAAe,EAAfA,0BAAe;EACfC,cAAc,EAAdA,yBAAc;EACdC,aAAa,EAAbA,wBAAa;EACbC,UAAU,EAAVA;AACF,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,GAAoB,GACxB,OAAOC,QAAQ,KAAK,WAAW,GAAGC,SAAS,GAAGD,QAAQ,CAACD,GAAG;;AAE5D;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMG,OAAO,GAAGC,UAAU,CAACC,sBAAsB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,aAAa,GAAGA,CAAA,KAAM;EACjC,SAAS;;EACT,MAAMC,CAAC,GAAGH,UAAgD;EAC1D,KAAK,MAAM,CAACI,GAAG,EAAEC,KAAK,CAAC,IAAIC,MAAM,CAACC,OAAO,CAACjB,SAAS,CAAC,EAAE;IACpD,IAAIa,CAAC,CAACC,GAAG,CAAC,KAAKN,SAAS,EAAE;MACxBK,CAAC,CAACC,GAAG,CAAC,GAAGC,KAAK;IAChB;EACF;EACA,IAAIT,GAAG,KAAKE,SAAS,EAAE;IACrB,MAAMU,GAAG,GAAGL,CAAC,CAACM,SAA0D;IACxE,IAAID,GAAG,KAAKV,SAAS,EAAE;MACrBK,CAAC,CAACM,SAAS,GAAG;QAAEb,GAAG;QAAEc,SAAS,EAAE;MAAe,CAAC;IAClD,CAAC,MAAM,IAAIF,GAAG,CAACZ,GAAG,KAAKE,SAAS,EAAE;MAChCU,GAAG,CAACZ,GAAG,GAAGA,GAAG;IACf;EACF;EACA;EACA;EACA;EACA;EACA,IAAIG,OAAO,KAAKD,SAAS,EAAE;IACzBC,OAAO,CAACY,mBAAmB,CAAC,CAAC;EAC/B;AACF,CAAC;AAACC,OAAA,CAAAV,aAAA,GAAAA,aAAA","ignoreList":[]}