react-native-webgpu 0.21.4 → 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 (170) hide show
  1. package/README.md +15 -2
  2. package/android/CMakeLists.txt +10 -8
  3. package/android/build.gradle +111 -20
  4. package/android/cpp/AndroidAnimationFrameSource.h +136 -0
  5. package/android/cpp/AndroidPlatformContext.h +55 -0
  6. package/android/cpp/cpp-adapter.cpp +148 -2
  7. package/android/src/main/java/com/webgpu/WebGPUModule.java +21 -4
  8. package/android/src/main/java/com/webgpu/WebGPUNativeBufferView.java +370 -0
  9. package/android/src/main/java/com/webgpu/WebGPUNativeBufferViewManager.java +56 -0
  10. package/android/src/main/java/com/webgpu/WebGPUTextureView.java +21 -0
  11. package/android/src/main/java/com/webgpu/WebGPUVideoPlayer.java +1 -1
  12. package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
  13. package/android/src/main/java/com/webgpu/WebGPUViewManager.java +10 -0
  14. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
  15. package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
  16. package/apple/AppleAnimationFrameSource.mm +208 -0
  17. package/apple/ApplePlatformContext.h +6 -0
  18. package/apple/ApplePlatformContext.mm +73 -0
  19. package/apple/MetalLayerColorSpace.h +17 -0
  20. package/apple/MetalLayerColorSpace.mm +36 -57
  21. package/apple/MetalView.h +4 -0
  22. package/apple/MetalView.mm +14 -0
  23. package/apple/NativeBufferFenceWait.mm +88 -0
  24. package/apple/RNWGUIKit.h +4 -1
  25. package/apple/WebGPUModule.mm +26 -1
  26. package/apple/WebGPUNativeBufferView.h +15 -0
  27. package/apple/WebGPUNativeBufferView.mm +200 -0
  28. package/apple/WebGPUView.h +2 -1
  29. package/apple/WebGPUView.mm +6 -0
  30. package/binaries.json +156 -0
  31. package/cpp/jsi/NativeObject.h +12 -5
  32. package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
  33. package/cpp/rnwgpu/AnimationFrames.h +233 -0
  34. package/cpp/rnwgpu/CanvasColorSpace.h +16 -0
  35. package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
  36. package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
  37. package/cpp/rnwgpu/PlatformContext.h +20 -0
  38. package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
  39. package/cpp/rnwgpu/SurfaceRegistry.h +26 -30
  40. package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
  41. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +41 -3
  42. package/cpp/rnwgpu/api/GPUCanvasContext.h +7 -3
  43. package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
  44. package/cpp/rnwgpu/api/GPUDevice.h +0 -18
  45. package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
  46. package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
  47. package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
  48. package/cpp/rnwgpu/api/GPUQueue.h +1 -1
  49. package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
  50. package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
  51. package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
  52. package/cpp/rnwgpu/api/GPUTexture.h +18 -0
  53. package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
  54. package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
  55. package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
  56. package/cpp/rnwgpu/api/RNWebGPU.h +201 -3
  57. package/cpp/rnwgpu/api/VideoFrame.h +5 -4
  58. package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
  59. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
  60. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
  61. package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
  62. package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
  63. package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
  64. package/lib/commonjs/Canvas.js +33 -1
  65. package/lib/commonjs/Canvas.js.map +1 -1
  66. package/lib/commonjs/NativeBufferView.js +131 -0
  67. package/lib/commonjs/NativeBufferView.js.map +1 -0
  68. package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
  69. package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
  70. package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
  71. package/lib/commonjs/WebGPUViewNativeComponent.web.js +2 -0
  72. package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
  73. package/lib/commonjs/animationFrame.js +91 -0
  74. package/lib/commonjs/animationFrame.js.map +1 -0
  75. package/lib/commonjs/index.js +16 -1
  76. package/lib/commonjs/index.js.map +1 -1
  77. package/lib/commonjs/main/index.js +12 -0
  78. package/lib/commonjs/main/index.js.map +1 -1
  79. package/lib/commonjs/mock.js +30 -1
  80. package/lib/commonjs/mock.js.map +1 -1
  81. package/lib/commonjs/video.js +14 -4
  82. package/lib/commonjs/video.js.map +1 -1
  83. package/lib/module/Canvas.js +33 -1
  84. package/lib/module/Canvas.js.map +1 -1
  85. package/lib/module/NativeBufferView.js +122 -0
  86. package/lib/module/NativeBufferView.js.map +1 -0
  87. package/lib/module/NativeBufferViewNativeComponent.js +4 -0
  88. package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
  89. package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
  90. package/lib/module/WebGPUViewNativeComponent.web.js +2 -0
  91. package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
  92. package/lib/module/animationFrame.js +83 -0
  93. package/lib/module/animationFrame.js.map +1 -0
  94. package/lib/module/index.js +1 -0
  95. package/lib/module/index.js.map +1 -1
  96. package/lib/module/main/index.js +1 -0
  97. package/lib/module/main/index.js.map +1 -1
  98. package/lib/module/mock.js +21 -0
  99. package/lib/module/mock.js.map +1 -1
  100. package/lib/module/video.js +11 -2
  101. package/lib/module/video.js.map +1 -1
  102. package/lib/typescript/lib/commonjs/Canvas.d.ts +3 -1
  103. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
  104. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
  105. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
  106. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
  107. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
  108. package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  109. package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
  110. package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
  111. package/lib/typescript/lib/commonjs/index.d.ts +2 -0
  112. package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
  113. package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
  114. package/lib/typescript/lib/commonjs/video.d.ts +3 -2
  115. package/lib/typescript/lib/commonjs/video.d.ts.map +1 -1
  116. package/lib/typescript/lib/module/Canvas.d.ts +3 -1
  117. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  118. package/lib/typescript/lib/module/NativeBufferView.d.ts +14 -0
  119. package/lib/typescript/lib/module/NativeBufferView.d.ts.map +1 -0
  120. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts +3 -0
  121. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts.map +1 -0
  122. package/lib/typescript/lib/module/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  123. package/lib/typescript/lib/module/animationFrame.d.ts +3 -0
  124. package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
  125. package/lib/typescript/lib/module/index.d.ts +1 -0
  126. package/lib/typescript/lib/module/main/index.d.ts +1 -0
  127. package/lib/typescript/lib/module/mock.d.ts +14 -0
  128. package/lib/typescript/lib/module/mock.d.ts.map +1 -1
  129. package/lib/typescript/lib/module/video.d.ts +3 -2
  130. package/lib/typescript/lib/module/video.d.ts.map +1 -1
  131. package/lib/typescript/src/Canvas.d.ts +17 -1
  132. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  133. package/lib/typescript/src/NativeBufferView.d.ts +99 -0
  134. package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
  135. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
  136. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
  137. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -0
  138. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
  139. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +2 -0
  140. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  141. package/lib/typescript/src/animationFrame.d.ts +59 -0
  142. package/lib/typescript/src/animationFrame.d.ts.map +1 -0
  143. package/lib/typescript/src/index.d.ts +18 -8
  144. package/lib/typescript/src/index.d.ts.map +1 -1
  145. package/lib/typescript/src/main/index.d.ts +1 -0
  146. package/lib/typescript/src/main/index.d.ts.map +1 -1
  147. package/lib/typescript/src/mock.d.ts +11 -0
  148. package/lib/typescript/src/mock.d.ts.map +1 -1
  149. package/lib/typescript/src/types.d.ts +32 -31
  150. package/lib/typescript/src/types.d.ts.map +1 -1
  151. package/lib/typescript/src/video.d.ts +12 -4
  152. package/lib/typescript/src/video.d.ts.map +1 -1
  153. package/package.json +8 -2
  154. package/react-native-webgpu.podspec +1 -1
  155. package/src/Canvas.tsx +67 -1
  156. package/src/NativeBufferView.tsx +199 -0
  157. package/src/NativeBufferViewNativeComponent.ts +20 -0
  158. package/src/WebGPUViewNativeComponent.ts +4 -0
  159. package/src/WebGPUViewNativeComponent.web.ts +6 -0
  160. package/src/animationFrame.ts +82 -0
  161. package/src/index.tsx +45 -39
  162. package/src/main/index.tsx +1 -0
  163. package/src/mock.ts +35 -0
  164. package/src/types.ts +61 -87
  165. package/src/video.ts +20 -7
  166. package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
  167. package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
  168. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
  169. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
  170. package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
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 };
package/src/types.ts CHANGED
@@ -34,17 +34,20 @@ export interface CanvasRef {
34
34
  getNativeSurface: () => NativeCanvas;
35
35
  }
36
36
 
37
- // Pixel layout of a NativeVideoFrame. NOT the WebCodecs `VideoPixelFormat`
37
+ // Pixel layout of a NativeBuffer. NOT the WebCodecs `VideoPixelFormat`
38
38
  // enum — these are the two native surface layouts we support, lower-cased to
39
39
  // avoid being mistaken for the spec values ("NV12", "BGRA", …).
40
- export type NativeVideoPixelFormat = "bgra8" | "nv12";
40
+ export type NativeBufferPixelFormat = "bgra8" | "nv12";
41
41
 
42
- // A native, GPU-shareable handle to a single video frame.
42
+ // A native, GPU-shareable buffer: a video or camera frame, or a buffer made
43
+ // with createNativeBuffer() to render into and show in a NativeBufferView. It
44
+ // exists without any GPU device; a device imports it to draw into it or to
45
+ // sample it (importSharedTextureMemory, importExternalTexture).
43
46
  //
44
47
  // NOT the WebCodecs `VideoFrame`: there is no `close()`/`format`/`timestamp`,
45
48
  // the surface is referenced by a raw native pointer, and disposal is
46
- // `release()`. Named with a `Native` prefix so it doesn't shadow the global
47
- // WebCodecs type or imply spec semantics it doesn't have.
49
+ // `release()`. The same name as VisionCamera's `NativeBuffer` (a pointer and
50
+ // release()), which createNativeBufferFromPointer() wraps into this one.
48
51
  //
49
52
  // - handle is the raw pointer (IOSurfaceRef on Apple, AHardwareBuffer* on
50
53
  // Android) encoded as a BigInt. Pass it to
@@ -55,18 +58,18 @@ export type NativeVideoPixelFormat = "bgra8" | "nv12";
55
58
  // - acquireFenceFd is the sync fd the producer of the frame (the decoder,
56
59
  // the camera) signals once it has written the buffer, as a BigInt, on
57
60
  // Android; null when the frame has none (Apple platforms, a released
58
- // frame). importExternalTexture() waits for it on its own; a hardware
59
- // buffer wrapper takes it through importSharedFence({ type: "sync-fd",
60
- // handle }) in beginAccess(). The frame owns the descriptor: import it
61
+ // frame). importExternalTexture() waits for it on its own; with
62
+ // importSharedTextureMemory(), pass it to beginAccess() through
63
+ // importSharedFence({ type: "sync-fd", handle }). The frame owns the descriptor: import it
61
64
  // before release(), and never close it.
62
65
  // - release() drops the underlying backing object (a CVPixelBuffer on Apple).
63
66
  // The frame is also released when the JS wrapper is garbage-collected; call
64
67
  // release() eagerly when you know you're done.
65
- export interface NativeVideoFrame {
68
+ export interface NativeBuffer {
66
69
  readonly handle: bigint;
67
70
  readonly width: number;
68
71
  readonly height: number;
69
- readonly pixelFormat: NativeVideoPixelFormat;
72
+ readonly pixelFormat: NativeBufferPixelFormat;
70
73
  readonly acquireFenceFd: bigint | null;
71
74
  release(): void;
72
75
  }
@@ -75,7 +78,7 @@ export interface NativeVideoFrame {
75
78
  // matrix and range that encode the samples, and the primaries and transfer
76
79
  // function of the RGB they encode. Each member is optional; what is left out
77
80
  // comes from the frame, then from the platform's best guess for the buffer.
78
- export interface NativeVideoFrameColorSpace {
81
+ export interface NativeBufferColorSpace {
79
82
  matrix?: "identity" | "rec601" | "rec709" | "rec2020";
80
83
  range?: "identity" | "narrow" | "full";
81
84
  primaries?: "srgb" | "rec709" | "rec601" | "rec2020" | "display-p3";
@@ -92,93 +95,40 @@ export interface NativeVideoFrameColorSpace {
92
95
  // A 2D size, as { width, height } or [width, height].
93
96
  export type GPUExtent2D = { width: number; height: number } | [number, number];
94
97
 
95
- // GPUExternalTextureDescriptor with a NativeVideoFrame as the source: what
98
+ // GPUExternalTextureDescriptor with a native buffer as the source: what
96
99
  // GPUDevice.importExternalTexture() takes on React Native, where there is no
97
100
  // HTMLVideoElement or WebCodecs VideoFrame. The other members (label,
98
101
  // colorSpace, and the rotation / mirrored extensions) are the same.
102
+ //
103
+ // The source is either
104
+ // - a NativeBuffer, imported for this external texture alone: the access
105
+ // begins here and ends at destroy(), like a web external texture, or
106
+ // - a texture from GPUSharedTextureMemory.createTexture(), for a buffer
107
+ // imported once: the external texture can be kept for every frame of the
108
+ // buffer, and is sampled only between the memory's beginAccess() and
109
+ // endAccess().
99
110
  export interface GPUNativeExternalTextureDescriptor extends Omit<
100
111
  GPUExternalTextureDescriptor,
101
112
  "source"
102
113
  > {
103
- source: NativeVideoFrame;
114
+ source: NativeBuffer | GPUTexture;
104
115
  // Overrides the members of the frame's own color space, for a buffer that
105
116
  // carries none (one wrapped from another library) or reports it wrong.
106
- sourceColorSpace?: NativeVideoFrameColorSpace;
117
+ sourceColorSpace?: NativeBufferColorSpace;
107
118
  // The part of the frame the external texture shows, the whole frame by
108
119
  // default (Dawn's cropOrigin / cropSize).
109
120
  cropOrigin?: GPUOrigin2D;
110
121
  cropSize?: GPUExtent2D;
111
122
  }
112
123
 
113
- // The descriptor of GPUDevice.createNativeBufferExternalTexture(): the same
114
- // options as importExternalTexture(), without the source (the frame is the
115
- // first argument).
116
- export type GPUNativeBufferExternalTextureDescriptor = Omit<
117
- GPUNativeExternalTextureDescriptor,
118
- "source"
119
- >;
120
-
121
- // The options of GPUDevice.createNativeBufferTexture(). `usage` defaults to
122
- // every usage the buffer allows, as GPUSharedTextureMemory.createTexture()
123
- // without a descriptor.
124
- export interface GPUNativeBufferTextureDescriptor {
125
- usage?: GPUTextureUsageFlags;
126
- label?: string;
127
- }
128
-
129
- // A native buffer imported once, with the access window of each use explicit:
130
- // the counterpart of GPUHardwareBufferWrapper in Dawn's Android layer
131
- // (androidx.webgpu). A decoder or a camera renders into the same few buffers
132
- // in turn; keep one wrapper per buffer (keyed by NativeVideoFrame.handle), and
133
- // bracket each frame with beginAccess() / endAccess() instead of importing it
134
- // again. The wrapper keeps the frame it was created from alive until
135
- // destroy().
136
- export interface GPUNativeBufferWrapper {
137
- label: string;
138
- // Begins an access to the buffer: the GPU waits for each fence to reach its
139
- // signaledValue first (the producer's fence for the frame, e.g. the frame's
140
- // acquireFenceFd imported with importSharedFence, 1n for a sync fd). Throws,
141
- // after a validation error, on a destroyed wrapper or fence, and if Dawn
142
- // rejects the access (one is already open).
143
- beginAccess(fences?: GPUSharedFenceState[]): void;
144
- // Ends the access, after the queue.submit() that used the buffer, and
145
- // returns the one fence that signals once that GPU work is done (on Android,
146
- // the sync fds of the access merged into one), or null when there is none.
147
- // Hand it to the producer before it reuses the buffer, or destroy it.
148
- endAccess(): GPUSharedFenceState | null;
149
- // Releases the import (and the frame) now rather than when the object is
150
- // garbage collected. An access still open is a validation error; it is
151
- // ended, and its fence is lost. A later use is a validation error and a
152
- // throw.
153
- destroy(): void;
154
- }
155
-
156
- // A color buffer (an RGBA / R8 / ... AHardwareBuffer, a BGRA IOSurface)
157
- // imported once as a regular texture. Bind `texture` only between
158
- // beginAccess() and endAccess().
159
- export interface GPUNativeBufferTexture extends GPUNativeBufferWrapper {
160
- readonly __brand: "GPUNativeBufferTexture";
161
- readonly texture: GPUTexture;
162
- }
163
-
164
- // A frame buffer (the YUV AHardwareBuffer of a decoder or a camera, an NV12 or
165
- // BGRA IOSurface) imported once as an external texture, color conversion
166
- // included. Bind `externalTexture` only between beginAccess() and endAccess();
167
- // destroying it on its own (externalTexture.destroy()) only releases the
168
- // external texture, the wrapper keeps the import.
169
- export interface GPUNativeBufferExternalTexture extends GPUNativeBufferWrapper {
170
- readonly __brand: "GPUNativeBufferExternalTexture";
171
- readonly externalTexture: GPUExternalTexture;
172
- }
173
-
174
124
  // A decoded video, modeled on HTMLMediaElement: play(), pause(), paused,
175
125
  // currentTime (in seconds, assign it to seek), duration, volume and loop work
176
126
  // as on the web. Frames are pulled instead of displayed: poll
177
127
  // copyLatestFrame() on every render tick to obtain the most recently decoded
178
- // frame as an IOSurface/AHardwareBuffer-backed NativeVideoFrame (null between
128
+ // frame as an IOSurface/AHardwareBuffer-backed NativeBuffer (null between
179
129
  // frames, so callers can skip the import work).
180
130
  export interface VideoPlayer {
181
- copyLatestFrame(): NativeVideoFrame | null;
131
+ copyLatestFrame(): NativeBuffer | null;
182
132
  play(): void;
183
133
  pause(): void;
184
134
  // Stops playback and releases the decoder. Frames already copied stay
@@ -214,7 +164,7 @@ export interface VideoPlayer {
214
164
  // GPUTextureUsage.RENDER_ATTACHMENT. `origin` and the copy size are in the
215
165
  // upright frame, after rotation.
216
166
  export interface GPUCopyExternalNativeFrameSourceInfo {
217
- source: NativeVideoFrame;
167
+ source: NativeBuffer;
218
168
  origin?: GPUOrigin2D;
219
169
  flipY?: boolean;
220
170
  rotation?: 0 | 90 | 180 | 270;
@@ -228,7 +178,7 @@ export interface CreateVideoPlayerOptions {
228
178
  // 'nv12': biplanar Y + CbCr surfaces, for GPUDevice.importExternalTexture.
229
179
  // On Android the decoder always writes its native YUV layout (reported as
230
180
  // 'nv12'), which only importExternalTexture samples; the option is ignored.
231
- pixelFormat?: NativeVideoPixelFormat;
181
+ pixelFormat?: NativeBufferPixelFormat;
232
182
  }
233
183
 
234
184
  // A React Native view to draw with GPUQueue.drawElementImageToTexture: a host
@@ -266,15 +216,23 @@ export interface GPUDrawElementImageDestination extends GPUCopyExternalImageDest
266
216
  size?: GPUExtent3DStrict;
267
217
  }
268
218
 
269
- export interface GPUSharedTextureMemoryDescriptor {
270
- // Raw native handle (IOSurfaceRef on Apple, AHardwareBuffer* on Android),
271
- // encoded as a BigInt. The caller is responsible for keeping the underlying
272
- // object alive for as long as the shared memory (and any textures derived
273
- // from it) are in use. Using NativeVideoFrame.handle handles this
274
- // automatically.
275
- handle: bigint;
219
+ export type GPUSharedTextureMemoryDescriptor = {
276
220
  label?: string;
277
- }
221
+ } & (
222
+ | {
223
+ // The buffer to import. The memory keeps it alive, and the textures
224
+ // created from it carry its color space for importExternalTexture().
225
+ source: NativeBuffer;
226
+ handle?: undefined;
227
+ }
228
+ | {
229
+ // A raw native handle (IOSurfaceRef on Apple, AHardwareBuffer* on
230
+ // Android) as a BigInt, which the caller keeps alive for as long as the
231
+ // memory (and the textures created from it) are in use.
232
+ handle: bigint;
233
+ source?: undefined;
234
+ }
235
+ );
278
236
 
279
237
  // The kind of native synchronization primitive a GPUSharedFence wraps, matching
280
238
  // the shared-fence-* device feature names. Limited to the kinds react-native-webgpu
@@ -369,3 +327,19 @@ export interface GPUSharedTextureMemory {
369
327
  // returns an invalid texture, beginAccess() and endAccess() throw.
370
328
  destroy(): void;
371
329
  }
330
+
331
+ /**
332
+ * The options of `createNativeBuffer()`. The buffer holds premultiplied
333
+ * pixels.
334
+ */
335
+ export interface CreateNativeBufferOptions {
336
+ /**
337
+ * Defaults to the preferred canvas format of the platform:
338
+ * `"rgba8unorm"` on Android, `"bgra8unorm"` on Apple platforms.
339
+ * `"rgba16float"` works on both; it gives more precision, not extended
340
+ * range: a `NativeBufferView` shows values in [0, 1] and clamps the rest.
341
+ */
342
+ format?: "rgba8unorm" | "bgra8unorm" | "rgba16float";
343
+ /** The primaries of the values. Defaults to `"srgb"`. */
344
+ colorSpace?: PredefinedColorSpace;
345
+ }
package/src/video.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type {
2
+ CreateNativeBufferOptions,
2
3
  CreateVideoPlayerOptions,
3
- NativeVideoFrame,
4
+ NativeBuffer,
4
5
  VideoPlayer,
5
6
  } from "./types";
6
7
 
@@ -47,13 +48,11 @@ export const createVideoPlayer = (
47
48
  /**
48
49
  * Wraps a native buffer produced by another library (a `CVPixelBufferRef` on
49
50
  * Apple platforms, an `AHardwareBuffer*` on Android, for instance
50
- * VisionCamera's `frame.getNativeBuffer().pointer`) into a `NativeVideoFrame`
51
+ * VisionCamera's `frame.getNativeBuffer().pointer`) into a `NativeBuffer`
51
52
  * that keeps it alive until released.
52
53
  */
53
- export const createVideoFrameFromNativeBuffer = (
54
- pointer: bigint,
55
- ): NativeVideoFrame =>
56
- native("createVideoFrameFromNativeBuffer").createVideoFrameFromNativeBuffer(
54
+ export const createNativeBufferFromPointer = (pointer: bigint): NativeBuffer =>
55
+ native("createNativeBufferFromPointer").createNativeBufferFromPointer(
57
56
  pointer,
58
57
  );
59
58
 
@@ -65,5 +64,19 @@ export const createVideoFrameFromNativeBuffer = (
65
64
  export const createTestVideoFrame = (
66
65
  width: number,
67
66
  height: number,
68
- ): NativeVideoFrame =>
67
+ ): NativeBuffer =>
69
68
  native("createTestVideoFrame").createTestVideoFrame(width, height);
69
+
70
+ /**
71
+ * Allocates an empty native buffer (an `AHardwareBuffer` on Android, an
72
+ * `IOSurface` on Apple platforms) that the GPU renders into and a
73
+ * `NativeBufferView` displays: import it with
74
+ * `device.importSharedTextureMemory({ source: frame })`, render into
75
+ * `memory.createTexture()` inside an access, then present it.
76
+ */
77
+ export const createNativeBuffer = (
78
+ width: number,
79
+ height: number,
80
+ options?: CreateNativeBufferOptions,
81
+ ): NativeBuffer =>
82
+ native("createNativeBuffer").createNativeBuffer(width, height, options);
@@ -1,60 +0,0 @@
1
- #pragma once
2
-
3
- #include <memory>
4
- #include <string>
5
- #include <utility>
6
-
7
- #include "webgpu/webgpu_cpp.h"
8
-
9
- #include "GPUExternalTexture.h"
10
- #include "GPUNativeBufferWrapper.h"
11
- #include "VideoFrame.h"
12
-
13
- namespace rnwgpu {
14
-
15
- namespace jsi = facebook::jsi;
16
-
17
- // A native frame buffer (the YUV AHardwareBuffer of a decoder or a camera, an
18
- // NV12 or BGRA IOSurface) imported once as an external texture, with the
19
- // access window of each use explicit: GPUHardwareBufferExternalTexture of
20
- // Dawn's Android layer. Created by
21
- // GPUDevice.createNativeBufferExternalTexture(). Unlike
22
- // importExternalTexture(), which imports the buffer and begins the access on
23
- // every call, the external texture here is built once per buffer, color
24
- // conversion included, and only beginAccess() / endAccess() run per frame.
25
- class GPUNativeBufferExternalTexture
26
- : public GPUNativeBufferWrapper<GPUNativeBufferExternalTexture,
27
- GPUExternalTexture> {
28
- public:
29
- static constexpr const char *CLASS_NAME = "GPUNativeBufferExternalTexture";
30
-
31
- GPUNativeBufferExternalTexture(
32
- wgpu::Device device, wgpu::SharedTextureMemory memory,
33
- wgpu::Texture texture,
34
- std::shared_ptr<GPUExternalTexture> externalTexture,
35
- std::shared_ptr<VideoFrame> source, std::string label)
36
- : GPUNativeBufferWrapper(std::move(device), std::move(memory),
37
- std::move(texture), std::move(externalTexture),
38
- std::move(source), std::move(label)) {}
39
-
40
- // The external texture to bind (a texture_external binding). Sample it only
41
- // between beginAccess() and endAccess().
42
- std::shared_ptr<GPUExternalTexture> getExternalTexture() { return _wrapped; }
43
-
44
- // Releases the external texture and the import (see
45
- // NativeBufferAccess::destroy), the order of Dawn's layer.
46
- void destroy() {
47
- _wrapped->destroy();
48
- _access.destroy();
49
- }
50
-
51
- static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
52
- defineWrapperPrototype(runtime, prototype);
53
- installGetter(runtime, prototype, "externalTexture",
54
- &GPUNativeBufferExternalTexture::getExternalTexture);
55
- installMethod(runtime, prototype, "destroy",
56
- &GPUNativeBufferExternalTexture::destroy);
57
- }
58
- };
59
-
60
- } // namespace rnwgpu
@@ -1,52 +0,0 @@
1
- #pragma once
2
-
3
- #include <memory>
4
- #include <string>
5
- #include <utility>
6
-
7
- #include "webgpu/webgpu_cpp.h"
8
-
9
- #include "GPUNativeBufferWrapper.h"
10
- #include "GPUTexture.h"
11
- #include "VideoFrame.h"
12
-
13
- namespace rnwgpu {
14
-
15
- namespace jsi = facebook::jsi;
16
-
17
- // A native color buffer (an RGBA / R8 / ... AHardwareBuffer, a BGRA IOSurface)
18
- // imported once as a regular texture, with the access window of each use
19
- // explicit: GPUHardwareBufferTexture of Dawn's Android layer. Created by
20
- // GPUDevice.createNativeBufferTexture().
21
- class GPUNativeBufferTexture
22
- : public GPUNativeBufferWrapper<GPUNativeBufferTexture, GPUTexture> {
23
- public:
24
- static constexpr const char *CLASS_NAME = "GPUNativeBufferTexture";
25
-
26
- GPUNativeBufferTexture(wgpu::Device device, wgpu::SharedTextureMemory memory,
27
- std::shared_ptr<GPUTexture> texture,
28
- std::shared_ptr<VideoFrame> source, std::string label)
29
- : GPUNativeBufferWrapper(std::move(device), std::move(memory),
30
- texture->get(), texture, std::move(source),
31
- std::move(label)) {}
32
-
33
- // The texture that aliases the buffer. Bind it only between beginAccess()
34
- // and endAccess().
35
- std::shared_ptr<GPUTexture> getTexture() { return _wrapped; }
36
-
37
- // Releases the texture and the import (see NativeBufferAccess::destroy).
38
- void destroy() {
39
- _access.destroy();
40
- _wrapped->destroy();
41
- }
42
-
43
- static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
44
- defineWrapperPrototype(runtime, prototype);
45
- installGetter(runtime, prototype, "texture",
46
- &GPUNativeBufferTexture::getTexture);
47
- installMethod(runtime, prototype, "destroy",
48
- &GPUNativeBufferTexture::destroy);
49
- }
50
- };
51
-
52
- } // namespace rnwgpu