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
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
@@ -1,144 +0,0 @@
1
- #include "GPUNativeBufferWrapper.h"
2
-
3
- #include <cstdint>
4
- #include <memory>
5
- #include <stdexcept>
6
- #include <string>
7
- #include <utility>
8
- #include <vector>
9
-
10
- #include "DestroyedError.h"
11
- #include "GPUSharedFence.h"
12
- #include "GPUSharedFenceState.h"
13
- #include "NativeFrameImport.h"
14
-
15
- namespace rnwgpu {
16
-
17
- NativeBufferAccess::NativeBufferAccess(const char *className,
18
- wgpu::Device device,
19
- wgpu::SharedTextureMemory memory,
20
- wgpu::Texture texture,
21
- std::shared_ptr<VideoFrame> source,
22
- std::string label)
23
- : _className(className), _device(std::move(device)),
24
- _memory(std::move(memory)), _texture(std::move(texture)),
25
- _source(std::move(source)), _label(std::move(label)) {}
26
-
27
- // The garbage-collection fallback: releases without reporting, there is no
28
- // one left to report to.
29
- NativeBufferAccess::~NativeBufferAccess() { release(false); }
30
-
31
- void NativeBufferAccess::setLabel(const std::string &label) {
32
- _label = label;
33
- if (_memory) {
34
- _memory.SetLabel(_label.c_str());
35
- }
36
- }
37
-
38
- void NativeBufferAccess::injectDestroyedError(const char *method) {
39
- reportDestroyedUse(_device, _className, _label, method);
40
- }
41
-
42
- void NativeBufferAccess::beginAccess(jsi::Runtime &runtime,
43
- const jsi::Value *args, size_t count) {
44
- const std::string caller = std::string(_className) + "::beginAccess";
45
- // Fails like an access Dawn rejects: a validation error, then the throw.
46
- if (!_memory) {
47
- injectDestroyedError("beginAccess");
48
- throw jsi::JSError(runtime, caller + "(): the wrapper was destroyed");
49
- }
50
- // Built in lockstep so fenceCount covers both arrays, and kept in locals so
51
- // the raw pointers outlive the synchronous BeginAccess() below.
52
- std::vector<wgpu::SharedFence> fences;
53
- std::vector<uint64_t> signaledValues;
54
- if (count > 0 && !args[0].isUndefined() && !args[0].isNull()) {
55
- auto states = JSIConverter<
56
- std::vector<std::shared_ptr<GPUSharedFenceState>>>::fromJSI(runtime,
57
- args[0],
58
- false);
59
- for (const auto &state : states) {
60
- if (!state || !state->fence) {
61
- continue;
62
- }
63
- auto fence = state->fence->get();
64
- if (!fence) {
65
- state->fence->injectDestroyedError("beginAccess");
66
- throw jsi::JSError(runtime, caller + "(): a fence was destroyed");
67
- }
68
- fences.push_back(std::move(fence));
69
- signaledValues.push_back(state->signaledValue);
70
- }
71
- }
72
- try {
73
- native_frame::beginAccess(_memory, _texture, true, fences, signaledValues,
74
- caller.c_str());
75
- } catch (const std::exception &e) {
76
- throw jsi::JSError(runtime, e.what());
77
- }
78
- _accessOpen = true;
79
- }
80
-
81
- jsi::Value NativeBufferAccess::endAccess(jsi::Runtime &runtime) {
82
- const std::string caller = std::string(_className) + "::endAccess";
83
- if (!_memory) {
84
- injectDestroyedError("endAccess");
85
- throw jsi::JSError(runtime, caller + "(): the wrapper was destroyed");
86
- }
87
- wgpu::SharedTextureMemoryEndAccessState state{};
88
- try {
89
- native_frame::endAccess(_memory, _texture, &state, caller.c_str());
90
- } catch (const std::exception &e) {
91
- throw jsi::JSError(runtime, e.what());
92
- }
93
- _accessOpen = false;
94
-
95
- wgpu::SharedFence fence;
96
- uint64_t signaledValue = 0;
97
- bool hasFence = false;
98
- try {
99
- hasFence = native_frame::mergeFences(_device, state, &fence, &signaledValue,
100
- caller.c_str());
101
- } catch (const std::exception &e) {
102
- throw jsi::JSError(runtime, e.what());
103
- }
104
- if (!hasFence) {
105
- return jsi::Value::null();
106
- }
107
- jsi::Object result(runtime);
108
- result.setProperty(
109
- runtime, "fence",
110
- GPUSharedFence::create(runtime, std::make_shared<GPUSharedFence>(
111
- _device, std::move(fence), "")));
112
- result.setProperty(runtime, "signaledValue",
113
- jsi::BigInt::fromUint64(runtime, signaledValue));
114
- return result;
115
- }
116
-
117
- void NativeBufferAccess::destroy() { release(true); }
118
-
119
- void NativeBufferAccess::release(bool reportOpenAccess) {
120
- if (!_memory) {
121
- return;
122
- }
123
- if (_accessOpen) {
124
- if (reportOpenAccess) {
125
- // Dawn's contract is an endAccess() for every beginAccess(). Report the
126
- // misuse, and end the access anyway: the wrapper owns the texture, so
127
- // an access left open would keep the buffer from its producer for good.
128
- auto message = "Destroyed " + describeDestroyed(_className, _label) +
129
- " with an access still open: call endAccess() first. The "
130
- "access was ended; its fence is lost.";
131
- _device.InjectError(wgpu::ErrorType::Validation, message.c_str());
132
- }
133
- native_frame::endAccess(_memory, _texture);
134
- _accessOpen = false;
135
- }
136
- if (_texture) {
137
- _texture.Destroy();
138
- }
139
- _texture = nullptr;
140
- _memory = nullptr;
141
- _source = nullptr;
142
- }
143
-
144
- } // namespace rnwgpu
@@ -1,143 +0,0 @@
1
- #pragma once
2
-
3
- #include <memory>
4
- #include <string>
5
- #include <utility>
6
-
7
- #include <jsi/jsi.h>
8
-
9
- #include "webgpu/webgpu_cpp.h"
10
-
11
- #include "NativeObject.h"
12
- #include "VideoFrame.h"
13
-
14
- namespace rnwgpu {
15
-
16
- namespace jsi = facebook::jsi;
17
-
18
- // What GPUNativeBufferTexture and GPUNativeBufferExternalTexture share:
19
- // the import of a native buffer, done once, the access window around each use
20
- // and the release. The counterpart of GPUHardwareBufferWrapper in Dawn's
21
- // Android layer (tools/android/webgpu), whose JNI side owns the same objects:
22
- // the shared texture memory, the texture, the buffer reference.
23
- //
24
- // A decoder or a camera renders into the same few buffers in turn: importing
25
- // each one once and bracketing every frame with beginAccess() / endAccess()
26
- // is what Dawn's layer does, and saves the import (and the pipeline recreation
27
- // behind a new external texture) that importExternalTexture() pays per frame.
28
- class NativeBufferAccess {
29
- public:
30
- NativeBufferAccess(const char *className, wgpu::Device device,
31
- wgpu::SharedTextureMemory memory, wgpu::Texture texture,
32
- std::shared_ptr<VideoFrame> source, std::string label);
33
- ~NativeBufferAccess();
34
-
35
- NativeBufferAccess(const NativeBufferAccess &) = delete;
36
- NativeBufferAccess &operator=(const NativeBufferAccess &) = delete;
37
-
38
- // beginAccess(fences?): begins an access to the buffer's texture, after the
39
- // GPU waited for the optional GPUSharedFenceState[] (the producer's fence
40
- // for the frame: NativeVideoFrame.acquireFenceFd imported with
41
- // importSharedFence, or the fences a native module hands over). The frame
42
- // is taken as initialized, as Dawn's layer does. Throws (after a validation
43
- // error) on a destroyed wrapper or fence, and if Dawn rejects the access.
44
- void beginAccess(jsi::Runtime &runtime, const jsi::Value *args, size_t count);
45
-
46
- // endAccess(): ends the access and returns the one fence that signals once
47
- // the GPU work of the access is done, as { fence, signaledValue }, or null
48
- // when there is none. Hand it to the buffer's producer before it reuses the
49
- // buffer, or destroy it.
50
- jsi::Value endAccess(jsi::Runtime &runtime);
51
-
52
- // Releases the import now rather than when the JS object is garbage
53
- // collected: the texture, the memory and the frame that keeps the buffer
54
- // alive. An access still open is a validation error; it is ended (its fence
55
- // is lost) so the producer gets its buffer back. A later use is a validation
56
- // error and a throw.
57
- void destroy();
58
-
59
- bool destroyed() const { return _memory == nullptr; }
60
-
61
- const std::string &label() const { return _label; }
62
- void setLabel(const std::string &label);
63
-
64
- private:
65
- void release(bool reportOpenAccess);
66
- void injectDestroyedError(const char *method);
67
-
68
- const char *_className;
69
- wgpu::Device _device;
70
- wgpu::SharedTextureMemory _memory;
71
- wgpu::Texture _texture;
72
- // Keeps the native buffer (and, on Android, its acquire fence) alive for as
73
- // long as the import is.
74
- std::shared_ptr<VideoFrame> _source;
75
- std::string _label;
76
- bool _accessOpen = false;
77
- };
78
-
79
- // The JS side GPUNativeBufferTexture and GPUNativeBufferExternalTexture share:
80
- // beginAccess(), endAccess(), the label and the brand, around a
81
- // NativeBufferAccess and the object it wraps (a GPUTexture, a
82
- // GPUExternalTexture), which takes the label too. Each class installs the
83
- // getter of its wrapped object and its destroy(), whose order differs.
84
- template <typename Derived, typename Wrapped>
85
- class GPUNativeBufferWrapper : public NativeObject<Derived> {
86
- public:
87
- GPUNativeBufferWrapper(wgpu::Device device, wgpu::SharedTextureMemory memory,
88
- wgpu::Texture texture,
89
- std::shared_ptr<Wrapped> wrapped,
90
- std::shared_ptr<VideoFrame> source, std::string label)
91
- : NativeObject<Derived>(Derived::CLASS_NAME),
92
- _access(Derived::CLASS_NAME, std::move(device), std::move(memory),
93
- std::move(texture), std::move(source), std::move(label)),
94
- _wrapped(std::move(wrapped)) {}
95
-
96
- std::string getBrand() { return Derived::CLASS_NAME; }
97
-
98
- jsi::Value beginAccess(jsi::Runtime &runtime, const jsi::Value &,
99
- const jsi::Value *args, size_t count) {
100
- _access.beginAccess(runtime, args, count);
101
- return jsi::Value::undefined();
102
- }
103
-
104
- jsi::Value endAccess(jsi::Runtime &runtime, const jsi::Value &,
105
- const jsi::Value *, size_t) {
106
- return _access.endAccess(runtime);
107
- }
108
-
109
- std::string getLabel() { return _access.label(); }
110
- void setLabel(const std::string &label) {
111
- _access.setLabel(label);
112
- _wrapped->setLabel(label);
113
- }
114
-
115
- protected:
116
- using HostMethod = jsi::Value (Derived::*)(jsi::Runtime &, const jsi::Value &,
117
- const jsi::Value *, size_t);
118
-
119
- static void defineWrapperPrototype(jsi::Runtime &runtime,
120
- jsi::Object &prototype) {
121
- using Base = NativeObject<Derived>;
122
- Base::installGetter(runtime, prototype, "__brand",
123
- static_cast<std::string (Derived::*)()>(
124
- &GPUNativeBufferWrapper::getBrand));
125
- Base::installMethod(
126
- runtime, prototype, "beginAccess",
127
- static_cast<HostMethod>(&GPUNativeBufferWrapper::beginAccess));
128
- Base::installMethod(
129
- runtime, prototype, "endAccess",
130
- static_cast<HostMethod>(&GPUNativeBufferWrapper::endAccess));
131
- Base::installGetterSetter(
132
- runtime, prototype, "label",
133
- static_cast<std::string (Derived::*)()>(
134
- &GPUNativeBufferWrapper::getLabel),
135
- static_cast<void (Derived::*)(const std::string &)>(
136
- &GPUNativeBufferWrapper::setLabel));
137
- }
138
-
139
- NativeBufferAccess _access;
140
- std::shared_ptr<Wrapped> _wrapped;
141
- };
142
-
143
- } // namespace rnwgpu