react-native-webgpu 0.22.0 → 0.23.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 (129) hide show
  1. package/README.md +15 -2
  2. package/android/CMakeLists.txt +2 -1
  3. package/android/cpp/AndroidAnimationFrameSource.h +136 -0
  4. package/android/cpp/AndroidPlatformContext.h +55 -0
  5. package/android/cpp/cpp-adapter.cpp +148 -2
  6. package/android/src/main/java/com/webgpu/WebGPUModule.java +21 -4
  7. package/android/src/main/java/com/webgpu/WebGPUNativeBufferView.java +370 -0
  8. package/android/src/main/java/com/webgpu/WebGPUNativeBufferViewManager.java +56 -0
  9. package/android/src/main/java/com/webgpu/WebGPUTextureView.java +21 -0
  10. package/android/src/main/java/com/webgpu/WebGPUVideoPlayer.java +1 -1
  11. package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
  12. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
  13. package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
  14. package/apple/AppleAnimationFrameSource.mm +208 -0
  15. package/apple/ApplePlatformContext.h +6 -0
  16. package/apple/ApplePlatformContext.mm +73 -0
  17. package/apple/NativeBufferFenceWait.mm +88 -0
  18. package/apple/WebGPUNativeBufferView.h +15 -0
  19. package/apple/WebGPUNativeBufferView.mm +200 -0
  20. package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
  21. package/cpp/rnwgpu/AnimationFrames.h +233 -0
  22. package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
  23. package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
  24. package/cpp/rnwgpu/PlatformContext.h +20 -0
  25. package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
  26. package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
  27. package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
  28. package/cpp/rnwgpu/api/GPUDevice.h +0 -18
  29. package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
  30. package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
  31. package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
  32. package/cpp/rnwgpu/api/GPUQueue.h +1 -1
  33. package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
  34. package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
  35. package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
  36. package/cpp/rnwgpu/api/GPUTexture.h +18 -0
  37. package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
  38. package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
  39. package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
  40. package/cpp/rnwgpu/api/RNWebGPU.h +201 -3
  41. package/cpp/rnwgpu/api/VideoFrame.h +5 -4
  42. package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
  43. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
  44. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
  45. package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
  46. package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
  47. package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
  48. package/lib/commonjs/NativeBufferView.js +131 -0
  49. package/lib/commonjs/NativeBufferView.js.map +1 -0
  50. package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
  51. package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
  52. package/lib/commonjs/animationFrame.js +91 -0
  53. package/lib/commonjs/animationFrame.js.map +1 -0
  54. package/lib/commonjs/index.js +16 -1
  55. package/lib/commonjs/index.js.map +1 -1
  56. package/lib/commonjs/main/index.js +12 -0
  57. package/lib/commonjs/main/index.js.map +1 -1
  58. package/lib/commonjs/mock.js +30 -1
  59. package/lib/commonjs/mock.js.map +1 -1
  60. package/lib/commonjs/video.js +14 -4
  61. package/lib/commonjs/video.js.map +1 -1
  62. package/lib/module/NativeBufferView.js +122 -0
  63. package/lib/module/NativeBufferView.js.map +1 -0
  64. package/lib/module/NativeBufferViewNativeComponent.js +4 -0
  65. package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
  66. package/lib/module/animationFrame.js +83 -0
  67. package/lib/module/animationFrame.js.map +1 -0
  68. package/lib/module/index.js +1 -0
  69. package/lib/module/index.js.map +1 -1
  70. package/lib/module/main/index.js +1 -0
  71. package/lib/module/main/index.js.map +1 -1
  72. package/lib/module/mock.js +21 -0
  73. package/lib/module/mock.js.map +1 -1
  74. package/lib/module/video.js +11 -2
  75. package/lib/module/video.js.map +1 -1
  76. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
  77. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
  78. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
  79. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
  80. package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
  81. package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
  82. package/lib/typescript/lib/commonjs/index.d.ts +2 -0
  83. package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
  84. package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
  85. package/lib/typescript/lib/commonjs/video.d.ts +3 -2
  86. package/lib/typescript/lib/commonjs/video.d.ts.map +1 -1
  87. package/lib/typescript/lib/module/NativeBufferView.d.ts +14 -0
  88. package/lib/typescript/lib/module/NativeBufferView.d.ts.map +1 -0
  89. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts +3 -0
  90. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts.map +1 -0
  91. package/lib/typescript/lib/module/animationFrame.d.ts +3 -0
  92. package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
  93. package/lib/typescript/lib/module/index.d.ts +1 -0
  94. package/lib/typescript/lib/module/main/index.d.ts +1 -0
  95. package/lib/typescript/lib/module/mock.d.ts +14 -0
  96. package/lib/typescript/lib/module/mock.d.ts.map +1 -1
  97. package/lib/typescript/lib/module/video.d.ts +3 -2
  98. package/lib/typescript/lib/module/video.d.ts.map +1 -1
  99. package/lib/typescript/src/NativeBufferView.d.ts +99 -0
  100. package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
  101. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
  102. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
  103. package/lib/typescript/src/animationFrame.d.ts +59 -0
  104. package/lib/typescript/src/animationFrame.d.ts.map +1 -0
  105. package/lib/typescript/src/index.d.ts +18 -8
  106. package/lib/typescript/src/index.d.ts.map +1 -1
  107. package/lib/typescript/src/main/index.d.ts +1 -0
  108. package/lib/typescript/src/main/index.d.ts.map +1 -1
  109. package/lib/typescript/src/mock.d.ts +11 -0
  110. package/lib/typescript/src/mock.d.ts.map +1 -1
  111. package/lib/typescript/src/types.d.ts +32 -31
  112. package/lib/typescript/src/types.d.ts.map +1 -1
  113. package/lib/typescript/src/video.d.ts +12 -4
  114. package/lib/typescript/src/video.d.ts.map +1 -1
  115. package/package.json +7 -2
  116. package/react-native-webgpu.podspec +1 -1
  117. package/src/NativeBufferView.tsx +199 -0
  118. package/src/NativeBufferViewNativeComponent.ts +20 -0
  119. package/src/animationFrame.ts +82 -0
  120. package/src/index.tsx +45 -39
  121. package/src/main/index.tsx +1 -0
  122. package/src/mock.ts +35 -0
  123. package/src/types.ts +61 -87
  124. package/src/video.ts +20 -7
  125. package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
  126. package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
  127. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
  128. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
  129. package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
@@ -0,0 +1,199 @@
1
+ import React, {
2
+ useCallback,
3
+ useEffect,
4
+ useImperativeHandle,
5
+ useLayoutEffect,
6
+ useRef,
7
+ useState,
8
+ } from "react";
9
+ import type { NativeSyntheticEvent, ViewProps } from "react-native";
10
+
11
+ import NativeBufferViewNative from "./NativeBufferViewNativeComponent";
12
+ import type { GPUSharedFenceState, NativeBuffer } from "./types";
13
+
14
+ /**
15
+ * How a {@link NativeBufferView} fits a buffer whose size differs from its
16
+ * own (in physical pixels), for instance while the producer catches up with
17
+ * a resize. A buffer of exactly the view's size is shown 1:1 in every mode.
18
+ *
19
+ * - `stretch` (the default): scaled to the bounds, ignoring the aspect ratio.
20
+ * - `contain`: scaled to fit inside the bounds, keeping the aspect ratio.
21
+ * - `cover`: scaled to fill the bounds, keeping the aspect ratio; the excess
22
+ * is cropped.
23
+ * - `topLeft`: not scaled; anchored at the top left corner and cropped.
24
+ */
25
+ export type NativeBufferResizeMode =
26
+ "stretch" | "contain" | "cover" | "topLeft";
27
+
28
+ /** A size in physical pixels. */
29
+ export interface NativeBufferPixelSize {
30
+ width: number;
31
+ height: number;
32
+ }
33
+
34
+ export interface NativeBufferViewRef {
35
+ /**
36
+ * The id to pass to `RNWebGPU.presentNativeBuffer(viewId, frame, done)`,
37
+ * for instance from a worklet.
38
+ */
39
+ readonly viewId: number;
40
+ /**
41
+ * Displays `frame` once the GPU work that wrote it is done: `done` is what
42
+ * `GPUSharedTextureMemory.endAccess()` returned for that work (its
43
+ * `fences`), and the view waits for every fence. Without it, the buffer is
44
+ * shown right away. On Android, the view also waits for the frame's own
45
+ * acquire fence, if any. Returns immediately; the view shows the buffer on
46
+ * the next vsync after that.
47
+ *
48
+ * The buffer must not be written again while it is displayed: render the
49
+ * next content into another buffer and present that one. The view keeps
50
+ * its own reference on the buffer, so a buffer from `createNativeBuffer()`
51
+ * can be released right after this call.
52
+ *
53
+ * Frames from a pool (a `VideoPlayer`, a camera, a decoder) are different:
54
+ * the pool reuses the buffer for a later frame once the frame is released,
55
+ * even while the view still shows it, and the view then shows a later or
56
+ * half-written frame. Copy such a frame into a buffer from
57
+ * `createNativeBuffer()` (for instance by drawing it with WebGPU) and
58
+ * present the copy.
59
+ */
60
+ present(
61
+ frame: NativeBuffer,
62
+ done?: { fences: GPUSharedFenceState[] } | null,
63
+ ): void;
64
+ }
65
+
66
+ export interface NativeBufferViewProps extends ViewProps {
67
+ /** See {@link NativeBufferResizeMode}. Defaults to `"stretch"`. */
68
+ resizeMode?: NativeBufferResizeMode;
69
+ /**
70
+ * The view's size in physical pixels: the size to render at for a sharp,
71
+ * 1:1 display. Called on mount and on every size change, with the
72
+ * rounding the platform's layout applied. See {@link useNativeBufferSize}
73
+ * to re-render only once a resize settles.
74
+ */
75
+ onPixelSizeChange?: (size: NativeBufferPixelSize) => void;
76
+ ref?: React.Ref<NativeBufferViewRef>;
77
+ }
78
+
79
+ /**
80
+ * Displays a native buffer (an `AHardwareBuffer` on Android, an `IOSurface`
81
+ * on Apple platforms) with no swap chain, no canvas context and no GPU device
82
+ * on the display side. Meant for content that is static or updated
83
+ * occasionally; animated content belongs in a `Canvas`.
84
+ *
85
+ * The view never allocates buffers. It reports its size in physical pixels
86
+ * ({@link NativeBufferViewProps.onPixelSizeChange}), and any producer
87
+ * (WebGPU, Skia, a camera, a decoder) renders into a buffer and presents it
88
+ * through the ref (frames from a camera or a decoder are copied first, see
89
+ * {@link NativeBufferViewRef.present}). With WebGPU:
90
+ *
91
+ * ```ts
92
+ * const frame = createNativeBuffer(size.width, size.height);
93
+ * const memory = device.importSharedTextureMemory({ source: frame });
94
+ * const texture = memory.createTexture();
95
+ * memory.beginAccess(texture, false);
96
+ * // ... render into texture and submit ...
97
+ * ref.current.present(frame, memory.endAccess(texture));
98
+ * texture.destroy();
99
+ * memory.destroy();
100
+ * frame.release();
101
+ * ```
102
+ */
103
+ export const NativeBufferView = ({
104
+ resizeMode,
105
+ onPixelSizeChange,
106
+ ref,
107
+ ...props
108
+ }: NativeBufferViewProps) => {
109
+ // Native hands out the id: unique for the life of the process, as the
110
+ // native registry outlives a reload.
111
+ const [viewId] = useState(() => RNWebGPU.allocateNativeBufferViewId());
112
+
113
+ useImperativeHandle(
114
+ ref,
115
+ () => ({
116
+ viewId,
117
+ present: (frame, done) =>
118
+ RNWebGPU.presentNativeBuffer(viewId, frame, done ?? null),
119
+ }),
120
+ [viewId],
121
+ );
122
+
123
+ // The JS side's hold on the native entry; presents to an id nobody holds
124
+ // are dropped. A layout effect, so the entry exists before the ref reaches
125
+ // a parent's effects. Not taken in the useState initializer above: React
126
+ // StrictMode calls it twice and discards one id.
127
+ useLayoutEffect(() => {
128
+ RNWebGPU.retainNativeBufferView(viewId);
129
+ return () => RNWebGPU.releaseNativeBufferView(viewId);
130
+ }, [viewId]);
131
+
132
+ const onNativePixelSizeChange = useCallback(
133
+ (event: NativeSyntheticEvent<NativeBufferPixelSize>) => {
134
+ const { width, height } = event.nativeEvent;
135
+ onPixelSizeChange?.({ width, height });
136
+ },
137
+ [onPixelSizeChange],
138
+ );
139
+
140
+ return (
141
+ <NativeBufferViewNative
142
+ {...props}
143
+ viewId={viewId}
144
+ resizeMode={resizeMode}
145
+ onPixelSizeChange={onNativePixelSizeChange}
146
+ />
147
+ );
148
+ };
149
+
150
+ /**
151
+ * Tracks the pixel size of a {@link NativeBufferView}, settling resizes:
152
+ * the first size is reported right away, later ones once the view has kept
153
+ * the same size for `debounceMs` (during an animated resize the view
154
+ * stretches what it shows meanwhile). Pass `onPixelSizeChange` to the view
155
+ * and render a buffer of `size` whenever it changes.
156
+ */
157
+ export const useNativeBufferSize = (debounceMs = 150) => {
158
+ const [size, setSize] = useState<NativeBufferPixelSize | null>(null);
159
+ const current = useRef<NativeBufferPixelSize | null>(null);
160
+ const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
161
+
162
+ const onPixelSizeChange = useCallback(
163
+ (next: NativeBufferPixelSize) => {
164
+ if (timer.current !== null) {
165
+ clearTimeout(timer.current);
166
+ timer.current = null;
167
+ }
168
+ const settle = () => {
169
+ timer.current = null;
170
+ const previous = current.current;
171
+ if (
172
+ previous === null ||
173
+ previous.width !== next.width ||
174
+ previous.height !== next.height
175
+ ) {
176
+ current.current = next;
177
+ setSize(next);
178
+ }
179
+ };
180
+ if (current.current === null || debounceMs <= 0) {
181
+ settle();
182
+ } else {
183
+ timer.current = setTimeout(settle, debounceMs);
184
+ }
185
+ },
186
+ [debounceMs],
187
+ );
188
+
189
+ useEffect(
190
+ () => () => {
191
+ if (timer.current !== null) {
192
+ clearTimeout(timer.current);
193
+ }
194
+ },
195
+ [],
196
+ );
197
+
198
+ return { size, onPixelSizeChange };
199
+ };
@@ -0,0 +1,20 @@
1
+ import { codegenNativeComponent } from "react-native";
2
+ import type { CodegenTypes, ViewProps } from "react-native";
3
+
4
+ type PixelSizeChangeEvent = Readonly<{
5
+ width: CodegenTypes.Int32;
6
+ height: CodegenTypes.Int32;
7
+ }>;
8
+
9
+ export interface NativeProps extends ViewProps {
10
+ // The NativeBufferViewRegistry entry (see NativeBufferView.tsx).
11
+ viewId: CodegenTypes.Int32;
12
+ resizeMode?: CodegenTypes.WithDefault<
13
+ "stretch" | "contain" | "cover" | "topLeft",
14
+ "stretch"
15
+ >;
16
+ onPixelSizeChange?: CodegenTypes.DirectEventHandler<PixelSizeChangeEvent>;
17
+ }
18
+
19
+ // eslint-disable-next-line import/no-default-export
20
+ export default codegenNativeComponent<NativeProps>("WebGPUNativeBufferView");
@@ -0,0 +1,82 @@
1
+ // The native animation frames (AnimationFramesHostObject in
2
+ // cpp/rnwgpu/AnimationFrames.h), installed on the main runtime. Read at module
3
+ // scope, like the helpers of `./install`, and captured into the worklets
4
+ // below: worklets hands a HostObject to every runtime by reference, and its
5
+ // methods act on the runtime that calls them. Undefined on web and node, where
6
+ // the browser's requestAnimationFrame is the one to use.
7
+ const frames = globalThis.__webgpuAnimationFrames;
8
+
9
+ /**
10
+ * `requestAnimationFrame`, paced by the display, on any runtime.
11
+ *
12
+ * Drop-in for the global one in a render loop:
13
+ *
14
+ * ```ts
15
+ * import { requestAnimationFrame } from "react-native-webgpu";
16
+ *
17
+ * const render = (timestamp: number) => {
18
+ * draw(timestamp);
19
+ * context.present();
20
+ * requestAnimationFrame(render);
21
+ * };
22
+ * requestAnimationFrame(render);
23
+ * ```
24
+ *
25
+ * React Native's global `requestAnimationFrame` is a timer: on Android it
26
+ * makes the next frame of the display only when it is requested before that
27
+ * frame starts, which leaves a loop that requests its next frame at the end
28
+ * of its work about 6 ms of a 120 Hz frame (past that, it drops to 60 fps),
29
+ * and on iOS it runs at 60 Hz. On a worklet runtime of your own
30
+ * (`createWorkletRuntime`), the global one polls every 16 ms (about 48 fps).
31
+ * This one follows the display wherever it is called:
32
+ *
33
+ * - On the React Native runtime, each frame of the display runs the callbacks
34
+ * requested before it, with the frame's time.
35
+ * - On a worklet runtime of your own, the runtime waits on its thread for the
36
+ * next frame of the display (the rest of the current frame at most), then
37
+ * runs them. Nothing else runs on the runtime during that wait.
38
+ * - On the Reanimated UI runtime, it is worklets' `requestAnimationFrame`,
39
+ * which is paced by the display already.
40
+ *
41
+ * Like a browser's, a callback requested while the frame's callbacks run gets
42
+ * the next frame, and every callback of a frame gets the same timestamp: the
43
+ * time the frame started, on the clock of `performance.now()`. The callbacks
44
+ * run once per frame of the display. A loop that overruns its frame runs
45
+ * again in the frame it overran into rather than waiting for the frame after.
46
+ * On the React Native runtime that run goes behind whatever waits there
47
+ * (React's updates, the app's other tasks), and the run after it waits for a
48
+ * frame of the display: the runtime is never the loop's alone.
49
+ *
50
+ * On the React Native runtime and on a worklet runtime of your own, no frame
51
+ * comes while the app is not in the foreground (the host activity is paused
52
+ * on Android, the app is not active on iOS): a loop stops with the app, and
53
+ * starts again when it is back.
54
+ *
55
+ * On iPhone, an app only gets more than 60 Hz with
56
+ * `CADisableMinimumFrameDurationOnPhone` set to true in its Info.plist. On
57
+ * macOS and on web, this is the runtime's own `requestAnimationFrame`.
58
+ */
59
+ export const requestAnimationFrame = (
60
+ callback: (timestamp: number) => void,
61
+ ): number => {
62
+ "worklet";
63
+ if (frames === undefined) {
64
+ return globalThis.requestAnimationFrame(callback);
65
+ }
66
+ return frames.request(callback);
67
+ };
68
+
69
+ /**
70
+ * Cancels a callback requested with this package's `requestAnimationFrame`,
71
+ * on the runtime it was requested on. Not interchangeable with the global
72
+ * `cancelAnimationFrame`: each cancels the handles of its own
73
+ * `requestAnimationFrame`.
74
+ */
75
+ export const cancelAnimationFrame = (handle: number): void => {
76
+ "worklet";
77
+ if (frames === undefined) {
78
+ globalThis.cancelAnimationFrame(handle);
79
+ return;
80
+ }
81
+ frames.cancel(handle);
82
+ };
package/src/index.tsx CHANGED
@@ -9,14 +9,12 @@
9
9
  import "./main/installNative";
10
10
 
11
11
  import type {
12
- GPUNativeBufferExternalTexture,
13
- GPUNativeBufferExternalTextureDescriptor,
14
- GPUNativeBufferTexture,
15
- GPUNativeBufferTextureDescriptor,
16
12
  GPUDrawElementImageDestination,
17
13
  GPUDrawElementImageSource,
18
14
  GPUSharedFence,
19
15
  GPUSharedFenceDescriptor,
16
+ GPUSharedFenceState,
17
+ CreateNativeBufferOptions,
20
18
  GPUDawnTogglesDescriptor,
21
19
  GPUNativeExternalTextureDescriptor,
22
20
  GPUSharedTextureMemory,
@@ -25,15 +23,16 @@ import type {
25
23
  NativeCanvas,
26
24
  RNCanvasContext,
27
25
  VideoPlayer,
28
- NativeVideoFrame,
29
- NativeVideoPixelFormat,
26
+ NativeBuffer,
27
+ NativeBufferPixelFormat,
30
28
  } from "./types";
31
29
 
32
30
  export * from "./main";
33
31
  export * from "./constants";
34
32
  export { installWebGPU } from "./install";
33
+ export { requestAnimationFrame, cancelAnimationFrame } from "./animationFrame";
35
34
  export type {
36
- NativeVideoFrame,
35
+ NativeBuffer,
37
36
  VideoPlayer,
38
37
  GPUSharedFence,
39
38
  GPUSharedFenceDescriptor,
@@ -43,21 +42,17 @@ export type {
43
42
  GPUSharedTextureMemory,
44
43
  GPUSharedTextureMemoryDescriptor,
45
44
  GPUSharedTextureMemoryEndAccessState,
46
- NativeVideoPixelFormat,
45
+ NativeBufferPixelFormat,
47
46
  CreateVideoPlayerOptions,
48
47
  GPUCopyExternalNativeFrameSourceInfo,
49
48
  GPUDawnTogglesDescriptor,
50
49
  GPUNativeExternalTextureDescriptor,
51
- NativeVideoFrameColorSpace,
50
+ NativeBufferColorSpace,
52
51
  GPUExtent2D,
53
- GPUNativeBufferExternalTextureDescriptor,
54
- GPUNativeBufferTextureDescriptor,
55
- GPUNativeBufferWrapper,
56
- GPUNativeBufferTexture,
57
- GPUNativeBufferExternalTexture,
58
52
  GPUDrawElementImageDestination,
59
53
  GPUDrawElementImageSource,
60
54
  GPUDrawElementImageSourceView,
55
+ CreateNativeBufferOptions,
61
56
  } from "./types";
62
57
 
63
58
  declare global {
@@ -67,6 +62,14 @@ declare global {
67
62
 
68
63
  var navigator: Navigator;
69
64
 
65
+ // The native animation frames, see src/animationFrame.ts. Undefined on web.
66
+ var __webgpuAnimationFrames:
67
+ | {
68
+ request: (callback: (timestamp: number) => void) => number;
69
+ cancel: (handle: number) => void;
70
+ }
71
+ | undefined;
72
+
70
73
  var RNWebGPU: {
71
74
  gpu: GPU;
72
75
  fabric: boolean;
@@ -92,7 +95,7 @@ declare global {
92
95
  destroyContext: (contextId: number) => void;
93
96
  DecodeToUTF8: (buffer: NodeJS.ArrayBufferView | ArrayBuffer) => string;
94
97
  createImageBitmap: typeof createImageBitmap;
95
- loadVideoFrame: (path: string) => NativeVideoFrame;
98
+ loadVideoFrame: (path: string) => NativeBuffer;
96
99
  // A synthetic frame: an RGB gradient ('bgra8', the default) or a biplanar
97
100
  // YUV frame ('nv12', BT.709 limited range, red on the left half and green
98
101
  // on the right) that exercises the color conversion of
@@ -100,12 +103,12 @@ declare global {
100
103
  createTestVideoFrame: (
101
104
  width: number,
102
105
  height: number,
103
- pixelFormat?: NativeVideoPixelFormat,
104
- ) => NativeVideoFrame;
106
+ pixelFormat?: NativeBufferPixelFormat,
107
+ ) => NativeBuffer;
105
108
  // Wrap a NativeBuffer.pointer (CVPixelBufferRef on iOS / AHardwareBuffer*
106
- // on Android) into a NativeVideoFrame. Matches the shape used by libraries
109
+ // on Android) into a NativeBuffer. Matches the shape used by libraries
107
110
  // that emit NativeBuffer (e.g. react-native-vision-camera).
108
- createVideoFrameFromNativeBuffer: (pointer: bigint) => NativeVideoFrame;
111
+ createNativeBufferFromPointer: (pointer: bigint) => NativeBuffer;
109
112
  // Wrap an externally created WGPUDevice pointer (e.g. Skia's Graphite
110
113
  // device from Skia.getNativeDevice()) into a GPUDevice. Requires the
111
114
  // exporter to link this process's single Dawn copy.
@@ -117,9 +120,26 @@ declare global {
117
120
  // Prefer the createVideoPlayer() export, which takes an options object.
118
121
  createVideoPlayer: (
119
122
  source: string,
120
- pixelFormat?: NativeVideoPixelFormat,
123
+ pixelFormat?: NativeBufferPixelFormat,
121
124
  ) => VideoPlayer;
122
125
  writeTestVideoFile: () => string;
126
+ // An empty native buffer (AHardwareBuffer / IOSurface) to render into
127
+ // through importSharedTextureMemory() and display in a NativeBufferView.
128
+ // Prefer the createNativeBuffer() export.
129
+ createNativeBuffer: (
130
+ width: number,
131
+ height: number,
132
+ options?: CreateNativeBufferOptions,
133
+ ) => NativeBuffer;
134
+ // The NativeBufferView registry (see NativeBufferView.tsx).
135
+ allocateNativeBufferViewId: () => number;
136
+ retainNativeBufferView: (viewId: number) => void;
137
+ releaseNativeBufferView: (viewId: number) => void;
138
+ presentNativeBuffer: (
139
+ viewId: number,
140
+ frame: NativeBuffer,
141
+ done?: { fences: GPUSharedFenceState[] } | null,
142
+ ) => void;
123
143
  };
124
144
 
125
145
  interface GPUTexture {
@@ -131,10 +151,9 @@ declare global {
131
151
 
132
152
  interface GPUDevice {
133
153
  // Non-spec RN extension: the source of an external texture is a
134
- // NativeVideoFrame (from a VideoPlayer, createVideoFrameFromNativeBuffer
135
- // or createTestVideoFrame) instead of the web's HTMLVideoElement /
136
- // VideoFrame. Overloads the spec signature, which stays available for
137
- // the web implementation.
154
+ // NativeBuffer, or a texture of a GPUSharedTextureMemory, instead of the
155
+ // web's HTMLVideoElement / VideoFrame. Overloads the spec signature,
156
+ // which stays available for the web implementation.
138
157
  importExternalTexture(
139
158
  descriptor: GPUNativeExternalTextureDescriptor,
140
159
  ): GPUExternalTexture;
@@ -142,19 +161,6 @@ declare global {
142
161
  descriptor: GPUSharedTextureMemoryDescriptor,
143
162
  ): GPUSharedTextureMemory;
144
163
  importSharedFence(descriptor: GPUSharedFenceDescriptor): GPUSharedFence;
145
- // Non-spec RN extensions, the counterparts of createTexture /
146
- // createExternalTexture in GPUAndroidHardwareBufferUtil of Dawn's Android
147
- // layer: import a native buffer once, as a regular texture or as an
148
- // external texture, and bracket each use with beginAccess() / endAccess()
149
- // on the wrapper instead of importing the buffer again per frame.
150
- createNativeBufferTexture(
151
- source: NativeVideoFrame,
152
- descriptor?: GPUNativeBufferTextureDescriptor,
153
- ): GPUNativeBufferTexture;
154
- createNativeBufferExternalTexture(
155
- source: NativeVideoFrame,
156
- descriptor?: GPUNativeBufferExternalTextureDescriptor,
157
- ): GPUNativeBufferExternalTexture;
158
164
  }
159
165
 
160
166
  // Non-spec RN extension modeled on the html-in-canvas proposal's
@@ -179,8 +185,8 @@ declare global {
179
185
  source: GPUDrawElementImageSource,
180
186
  destination: GPUDrawElementImageDestination,
181
187
  ): Promise<void>;
182
- // Non-spec extension: a NativeVideoFrame (from createVideoPlayer,
183
- // createVideoFrameFromNativeBuffer or createTestVideoFrame) as the copy
188
+ // Non-spec extension: a NativeBuffer (from createVideoPlayer,
189
+ // createNativeBufferFromPointer or createTestVideoFrame) as the copy
184
190
  // source. The frame is rendered into the destination on the GPU through
185
191
  // an external texture, which converts YUV to RGB and applies `rotation`
186
192
  // and `mirrored`; the destination needs GPUTextureUsage.RENDER_ATTACHMENT
@@ -56,6 +56,7 @@ const installDrawElementImageToTexture = () => {
56
56
  };
57
57
 
58
58
  export * from "../Canvas";
59
+ export * from "../NativeBufferView";
59
60
  export * from "../Offscreen";
60
61
  export * from "../WebGPUViewNativeComponent";
61
62
  export * from "../hooks";
package/src/mock.ts CHANGED
@@ -4,6 +4,7 @@ import type { ReactNode } from "react";
4
4
  import { View } from "react-native";
5
5
 
6
6
  import type { CanvasProps, NativeCanvas, RNCanvasContext } from "./Canvas";
7
+ import type { NativeBufferViewProps } from "./NativeBufferView";
7
8
 
8
9
  // Jest mock for `react-native-webgpu`.
9
10
  //
@@ -105,6 +106,34 @@ export const Canvas = ({ android: _android, ref, ...props }: CanvasProps) => {
105
106
  return React.createElement(View, props);
106
107
  };
107
108
 
109
+ export type {
110
+ NativeBufferPixelSize,
111
+ NativeBufferResizeMode,
112
+ NativeBufferViewProps,
113
+ NativeBufferViewRef,
114
+ } from "./NativeBufferView";
115
+
116
+ // Renders as a plain View; present() fails like the other GPU entry points.
117
+ export const NativeBufferView = ({
118
+ resizeMode: _resizeMode,
119
+ onPixelSizeChange: _onPixelSizeChange,
120
+ ref,
121
+ ...props
122
+ }: NativeBufferViewProps) => {
123
+ React.useImperativeHandle(ref, () => ({
124
+ viewId: -1,
125
+ present: () => {
126
+ throw new Error(`NativeBufferView.present() ${NOT_AVAILABLE_UNDER_JEST}`);
127
+ },
128
+ }));
129
+ return React.createElement(View, props);
130
+ };
131
+
132
+ export const useNativeBufferSize = (_debounceMs?: number) => ({
133
+ size: null,
134
+ onPixelSizeChange: () => undefined,
135
+ });
136
+
108
137
  export const useCanvasRef = () => React.useRef(null);
109
138
  export const useSurface = () => ({ ref: React.useRef(null), surface: null });
110
139
  export const warnIfNotHardwareAccelerated = () => undefined;
@@ -128,4 +157,10 @@ export const adoptTexture = (_pointer: bigint): GPUTexture => {
128
157
 
129
158
  export const installWebGPU = () => undefined;
130
159
 
160
+ export const requestAnimationFrame = (callback: (timestamp: number) => void) =>
161
+ globalThis.requestAnimationFrame(callback);
162
+
163
+ export const cancelAnimationFrame = (handle: number) =>
164
+ globalThis.cancelAnimationFrame(handle);
165
+
131
166
  export const WebGPUModule = { install: () => false };