react-native-webgpu 0.21.4 → 0.22.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.
Files changed (43) hide show
  1. package/android/CMakeLists.txt +8 -7
  2. package/android/build.gradle +111 -20
  3. package/android/src/main/java/com/webgpu/WebGPUViewManager.java +10 -0
  4. package/apple/MetalLayerColorSpace.h +17 -0
  5. package/apple/MetalLayerColorSpace.mm +36 -57
  6. package/apple/MetalView.h +4 -0
  7. package/apple/MetalView.mm +14 -0
  8. package/apple/RNWGUIKit.h +4 -1
  9. package/apple/WebGPUModule.mm +26 -1
  10. package/apple/WebGPUView.h +2 -1
  11. package/apple/WebGPUView.mm +6 -0
  12. package/binaries.json +156 -0
  13. package/cpp/jsi/NativeObject.h +12 -5
  14. package/cpp/rnwgpu/CanvasColorSpace.h +16 -0
  15. package/cpp/rnwgpu/SurfaceRegistry.h +26 -30
  16. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +41 -3
  17. package/cpp/rnwgpu/api/GPUCanvasContext.h +7 -3
  18. package/lib/commonjs/Canvas.js +33 -1
  19. package/lib/commonjs/Canvas.js.map +1 -1
  20. package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
  21. package/lib/commonjs/WebGPUViewNativeComponent.web.js +2 -0
  22. package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
  23. package/lib/module/Canvas.js +33 -1
  24. package/lib/module/Canvas.js.map +1 -1
  25. package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
  26. package/lib/module/WebGPUViewNativeComponent.web.js +2 -0
  27. package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
  28. package/lib/typescript/lib/commonjs/Canvas.d.ts +3 -1
  29. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
  30. package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  31. package/lib/typescript/lib/module/Canvas.d.ts +3 -1
  32. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  33. package/lib/typescript/lib/module/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  34. package/lib/typescript/src/Canvas.d.ts +17 -1
  35. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  36. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -0
  37. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
  38. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +2 -0
  39. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  40. package/package.json +2 -1
  41. package/src/Canvas.tsx +67 -1
  42. package/src/WebGPUViewNativeComponent.ts +4 -0
  43. package/src/WebGPUViewNativeComponent.web.ts +6 -0
package/src/Canvas.tsx CHANGED
@@ -91,6 +91,22 @@ export interface CanvasProps extends ViewProps {
91
91
  * options, see {@link AndroidCanvasProps}. Ignored on iOS and web.
92
92
  */
93
93
  android?: AndroidCanvasProps;
94
+ /**
95
+ * The primaries the canvas's values are in, as `colorSpace` in
96
+ * `context.configure()`: `"srgb"` (the default) or `"display-p3"`. On iOS
97
+ * and macOS the layer is tagged with it when the canvas mounts, on the main
98
+ * thread: pass the same value to `configure()` (a mismatch is reported in
99
+ * development). A change on a mounted canvas is ignored, with a warning in
100
+ * development; remount the canvas (with a `key`) to change it. Android
101
+ * ignores it; on the web, `configure()` applies it.
102
+ */
103
+ colorSpace?: PredefinedColorSpace;
104
+ /**
105
+ * What happens to values above 1.0 on an `rgba16float` canvas, as
106
+ * `toneMapping` in `context.configure()`. Read when the canvas mounts, like
107
+ * {@link CanvasProps.colorSpace}.
108
+ */
109
+ toneMapping?: GPUCanvasToneMapping;
94
110
  ref?: React.Ref<CanvasRef>;
95
111
  }
96
112
 
@@ -202,6 +218,48 @@ const useBackingViewWarning = (android?: AndroidCanvasProps) => {
202
218
  });
203
219
  };
204
220
 
221
+ // The native props the `colorSpace` and `toneMapping` props resolve to. Like
222
+ // configure(), anything but "display-p3" and "extended" is the default.
223
+ const appleNativeProps = (
224
+ colorSpace?: PredefinedColorSpace,
225
+ toneMapping?: GPUCanvasToneMapping,
226
+ ) => ({
227
+ appleDisplayP3: colorSpace === "display-p3",
228
+ appleExtendedDynamicRange: toneMapping?.mode === "extended",
229
+ });
230
+
231
+ // Warns, in development on Apple platforms, about a change of the colorspace
232
+ // props that the native view ignores: the layer is tagged with them when the
233
+ // canvas mounts (see WebGPUView.mm). A change of the toneMapping object
234
+ // itself (a new object every render) is not one.
235
+ const useAppleColorSpaceWarning = (
236
+ colorSpace?: PredefinedColorSpace,
237
+ toneMapping?: GPUCanvasToneMapping,
238
+ ) => {
239
+ const prev = useRef(appleNativeProps(colorSpace, toneMapping));
240
+ useEffect(() => {
241
+ if (!__DEV__ || (Platform.OS !== "ios" && Platform.OS !== "macos")) {
242
+ return;
243
+ }
244
+ const next = appleNativeProps(colorSpace, toneMapping);
245
+ const warn = (prop: string) =>
246
+ console.warn(
247
+ `Ignoring a change of ${prop} on a mounted canvas: the layer is ` +
248
+ "tagged with it when the canvas mounts. Remount the canvas (give " +
249
+ "it a new key) to change it.",
250
+ );
251
+ if (prev.current.appleDisplayP3 !== next.appleDisplayP3) {
252
+ warn("colorSpace");
253
+ }
254
+ if (
255
+ prev.current.appleExtendedDynamicRange !== next.appleExtendedDynamicRange
256
+ ) {
257
+ warn("toneMapping");
258
+ }
259
+ prev.current = next;
260
+ });
261
+ };
262
+
205
263
  // A view measured relative to itself gets its layout size, without the
206
264
  // transforms getBoundingClientRect() includes; measureLayout() calls back
207
265
  // synchronously on the new architecture. On the web react-native-web measures
@@ -222,10 +280,17 @@ const measureLayout = (view: ViewRef) => {
222
280
  return size;
223
281
  };
224
282
 
225
- export const Canvas = ({ android, ref, ...props }: CanvasProps) => {
283
+ export const Canvas = ({
284
+ android,
285
+ colorSpace,
286
+ toneMapping,
287
+ ref,
288
+ ...props
289
+ }: CanvasProps) => {
226
290
  const viewRef = useRef<ViewRef>(null);
227
291
  const [contextId, _] = useState(() => generateContextId());
228
292
  useBackingViewWarning(android);
293
+ useAppleColorSpaceWarning(colorSpace, toneMapping);
229
294
  // Retire the native registry entry for this contextId on unmount. When a
230
295
  // native surface is still attached, this is a no-op and the native view's
231
296
  // own teardown retires the entry instead — which keeps StrictMode's
@@ -270,6 +335,7 @@ export const Canvas = ({ android, ref, ...props }: CanvasProps) => {
270
335
  style={{ flex: 1 }}
271
336
  contextId={contextId}
272
337
  {...androidNativeProps(android)}
338
+ {...appleNativeProps(colorSpace, toneMapping)}
273
339
  />
274
340
  </View>
275
341
  );
@@ -11,6 +11,10 @@ export interface NativeProps extends ViewProps {
11
11
  androidOpaque?: CodegenTypes.WithDefault<boolean, false>;
12
12
  androidZOrderOnTop?: CodegenTypes.WithDefault<boolean, false>;
13
13
  androidZOrderMediaOverlay?: CodegenTypes.WithDefault<boolean, false>;
14
+ // The `colorSpace` and `toneMapping` props, resolved (see Canvas.tsx):
15
+ // read on iOS and macOS when the canvas mounts
16
+ appleDisplayP3?: CodegenTypes.WithDefault<boolean, false>;
17
+ appleExtendedDynamicRange?: CodegenTypes.WithDefault<boolean, false>;
14
18
  }
15
19
 
16
20
  // eslint-disable-next-line import/no-default-export
@@ -11,6 +11,10 @@ export interface NativeProps extends ViewProps {
11
11
  androidOpaque?: boolean;
12
12
  androidZOrderOnTop?: boolean;
13
13
  androidZOrderMediaOverlay?: boolean;
14
+ // The `colorSpace` and `toneMapping` props, resolved: Apple-only, never
15
+ // reach the DOM (on the web, configure() applies them)
16
+ appleDisplayP3?: boolean;
17
+ appleExtendedDynamicRange?: boolean;
14
18
  }
15
19
 
16
20
  // eslint-disable-next-line import/no-default-export
@@ -24,6 +28,8 @@ export default function WebGPUViewNativeComponent(
24
28
  androidOpaque: _androidOpaque,
25
29
  androidZOrderOnTop: _androidZOrderOnTop,
26
30
  androidZOrderMediaOverlay: _androidZOrderMediaOverlay,
31
+ appleDisplayP3: _appleDisplayP3,
32
+ appleExtendedDynamicRange: _appleExtendedDynamicRange,
27
33
  ...rest
28
34
  } = props;
29
35