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.
- package/README.md +15 -2
- package/android/CMakeLists.txt +10 -8
- package/android/build.gradle +111 -20
- package/android/cpp/AndroidAnimationFrameSource.h +136 -0
- package/android/cpp/AndroidPlatformContext.h +55 -0
- package/android/cpp/cpp-adapter.cpp +148 -2
- package/android/src/main/java/com/webgpu/WebGPUModule.java +21 -4
- package/android/src/main/java/com/webgpu/WebGPUNativeBufferView.java +370 -0
- package/android/src/main/java/com/webgpu/WebGPUNativeBufferViewManager.java +56 -0
- package/android/src/main/java/com/webgpu/WebGPUTextureView.java +21 -0
- package/android/src/main/java/com/webgpu/WebGPUVideoPlayer.java +1 -1
- package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
- package/android/src/main/java/com/webgpu/WebGPUViewManager.java +10 -0
- package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
- package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
- package/apple/AppleAnimationFrameSource.mm +208 -0
- package/apple/ApplePlatformContext.h +6 -0
- package/apple/ApplePlatformContext.mm +73 -0
- package/apple/MetalLayerColorSpace.h +17 -0
- package/apple/MetalLayerColorSpace.mm +36 -57
- package/apple/MetalView.h +4 -0
- package/apple/MetalView.mm +14 -0
- package/apple/NativeBufferFenceWait.mm +88 -0
- package/apple/RNWGUIKit.h +4 -1
- package/apple/WebGPUModule.mm +26 -1
- package/apple/WebGPUNativeBufferView.h +15 -0
- package/apple/WebGPUNativeBufferView.mm +200 -0
- package/apple/WebGPUView.h +2 -1
- package/apple/WebGPUView.mm +6 -0
- package/binaries.json +156 -0
- package/cpp/jsi/NativeObject.h +12 -5
- package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
- package/cpp/rnwgpu/AnimationFrames.h +233 -0
- package/cpp/rnwgpu/CanvasColorSpace.h +16 -0
- package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
- package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
- package/cpp/rnwgpu/PlatformContext.h +20 -0
- package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
- package/cpp/rnwgpu/SurfaceRegistry.h +26 -30
- package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
- package/cpp/rnwgpu/api/GPUCanvasContext.cpp +41 -3
- package/cpp/rnwgpu/api/GPUCanvasContext.h +7 -3
- package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
- package/cpp/rnwgpu/api/GPUDevice.h +0 -18
- package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
- package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
- package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
- package/cpp/rnwgpu/api/GPUQueue.h +1 -1
- package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
- package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
- package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
- package/cpp/rnwgpu/api/GPUTexture.h +18 -0
- package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
- package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
- package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
- package/cpp/rnwgpu/api/RNWebGPU.h +201 -3
- package/cpp/rnwgpu/api/VideoFrame.h +5 -4
- package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
- package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
- package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
- package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
- package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
- package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
- package/lib/commonjs/Canvas.js +33 -1
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/NativeBufferView.js +131 -0
- package/lib/commonjs/NativeBufferView.js.map +1 -0
- package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
- package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
- package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/commonjs/WebGPUViewNativeComponent.web.js +2 -0
- package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
- package/lib/commonjs/animationFrame.js +91 -0
- package/lib/commonjs/animationFrame.js.map +1 -0
- package/lib/commonjs/index.js +16 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/main/index.js +12 -0
- package/lib/commonjs/main/index.js.map +1 -1
- package/lib/commonjs/mock.js +30 -1
- package/lib/commonjs/mock.js.map +1 -1
- package/lib/commonjs/video.js +14 -4
- package/lib/commonjs/video.js.map +1 -1
- package/lib/module/Canvas.js +33 -1
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/NativeBufferView.js +122 -0
- package/lib/module/NativeBufferView.js.map +1 -0
- package/lib/module/NativeBufferViewNativeComponent.js +4 -0
- package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
- package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/module/WebGPUViewNativeComponent.web.js +2 -0
- package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
- package/lib/module/animationFrame.js +83 -0
- package/lib/module/animationFrame.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/main/index.js +1 -0
- package/lib/module/main/index.js.map +1 -1
- package/lib/module/mock.js +21 -0
- package/lib/module/mock.js.map +1 -1
- package/lib/module/video.js +11 -2
- package/lib/module/video.js.map +1 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts +3 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
- package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
- package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/index.d.ts +2 -0
- package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
- package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/video.d.ts +3 -2
- package/lib/typescript/lib/commonjs/video.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts +3 -1
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/NativeBufferView.d.ts +14 -0
- package/lib/typescript/lib/module/NativeBufferView.d.ts.map +1 -0
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts +3 -0
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/lib/module/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/lib/module/animationFrame.d.ts +3 -0
- package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
- package/lib/typescript/lib/module/index.d.ts +1 -0
- package/lib/typescript/lib/module/main/index.d.ts +1 -0
- package/lib/typescript/lib/module/mock.d.ts +14 -0
- package/lib/typescript/lib/module/mock.d.ts.map +1 -1
- package/lib/typescript/lib/module/video.d.ts +3 -2
- package/lib/typescript/lib/module/video.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +17 -1
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/NativeBufferView.d.ts +99 -0
- package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
- package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
- package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +2 -0
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/src/animationFrame.d.ts +59 -0
- package/lib/typescript/src/animationFrame.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +18 -8
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.d.ts +1 -0
- package/lib/typescript/src/main/index.d.ts.map +1 -1
- package/lib/typescript/src/mock.d.ts +11 -0
- package/lib/typescript/src/mock.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +32 -31
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/video.d.ts +12 -4
- package/lib/typescript/src/video.d.ts.map +1 -1
- package/package.json +8 -2
- package/react-native-webgpu.podspec +1 -1
- package/src/Canvas.tsx +67 -1
- package/src/NativeBufferView.tsx +199 -0
- package/src/NativeBufferViewNativeComponent.ts +20 -0
- package/src/WebGPUViewNativeComponent.ts +4 -0
- package/src/WebGPUViewNativeComponent.web.ts +6 -0
- package/src/animationFrame.ts +82 -0
- package/src/index.tsx +45 -39
- package/src/main/index.tsx +1 -0
- package/src/mock.ts +35 -0
- package/src/types.ts +61 -87
- package/src/video.ts +20 -7
- package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
- package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
- package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
- package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
- package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateVideoPlayerOptions,
|
|
1
|
+
import type { CreateNativeBufferOptions, CreateVideoPlayerOptions, NativeBuffer, VideoPlayer } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* Decodes a video into native frames the GPU samples without any copy.
|
|
4
4
|
*
|
|
@@ -28,14 +28,22 @@ export declare const createVideoPlayer: (source: string, options?: CreateVideoPl
|
|
|
28
28
|
/**
|
|
29
29
|
* Wraps a native buffer produced by another library (a `CVPixelBufferRef` on
|
|
30
30
|
* Apple platforms, an `AHardwareBuffer*` on Android, for instance
|
|
31
|
-
* VisionCamera's `frame.getNativeBuffer().pointer`) into a `
|
|
31
|
+
* VisionCamera's `frame.getNativeBuffer().pointer`) into a `NativeBuffer`
|
|
32
32
|
* that keeps it alive until released.
|
|
33
33
|
*/
|
|
34
|
-
export declare const
|
|
34
|
+
export declare const createNativeBufferFromPointer: (pointer: bigint) => NativeBuffer;
|
|
35
35
|
/**
|
|
36
36
|
* Allocates a native frame of the given size filled with a test pattern (a
|
|
37
37
|
* red/green gradient with diagonal stripes in the blue channel), for examples
|
|
38
38
|
* and tests that need a frame without a camera or a video.
|
|
39
39
|
*/
|
|
40
|
-
export declare const createTestVideoFrame: (width: number, height: number) =>
|
|
40
|
+
export declare const createTestVideoFrame: (width: number, height: number) => NativeBuffer;
|
|
41
|
+
/**
|
|
42
|
+
* Allocates an empty native buffer (an `AHardwareBuffer` on Android, an
|
|
43
|
+
* `IOSurface` on Apple platforms) that the GPU renders into and a
|
|
44
|
+
* `NativeBufferView` displays: import it with
|
|
45
|
+
* `device.importSharedTextureMemory({ source: frame })`, render into
|
|
46
|
+
* `memory.createTexture()` inside an access, then present it.
|
|
47
|
+
*/
|
|
48
|
+
export declare const createNativeBuffer: (width: number, height: number, options?: CreateNativeBufferOptions) => NativeBuffer;
|
|
41
49
|
//# sourceMappingURL=video.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video.d.ts","sourceRoot":"","sources":["../../../src/video.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,
|
|
1
|
+
{"version":3,"file":"video.d.ts","sourceRoot":"","sources":["../../../src/video.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,wBAAwB,EACxB,YAAY,EACZ,WAAW,EACZ,MAAM,SAAS,CAAC;AAWjB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,MAAM,EACd,UAAU,wBAAwB,KACjC,WAC0E,CAAC;AAE9E;;;;;GAKG;AACH,eAAO,MAAM,6BAA6B,GAAI,SAAS,MAAM,KAAG,YAG7D,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAC/B,OAAO,MAAM,EACb,QAAQ,MAAM,KACb,YACiE,CAAC;AAErE;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAC7B,OAAO,MAAM,EACb,QAAQ,MAAM,EACd,UAAU,yBAAyB,KAClC,YACsE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-webgpu",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "React Native WebGPU",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"apple/**",
|
|
21
21
|
"*.podspec",
|
|
22
22
|
"Package.swift",
|
|
23
|
+
"binaries.json",
|
|
23
24
|
"react-native.config.js",
|
|
24
25
|
"app.plugin.js",
|
|
25
26
|
"plugin/build/**"
|
|
@@ -57,6 +58,10 @@
|
|
|
57
58
|
},
|
|
58
59
|
"author": "William Candillon <wcandillon@gmail.com> (https://github.com/wcandillon)",
|
|
59
60
|
"license": "MIT",
|
|
61
|
+
"funding": {
|
|
62
|
+
"type": "github",
|
|
63
|
+
"url": "https://github.com/sponsors/wcandillon"
|
|
64
|
+
},
|
|
60
65
|
"bugs": {
|
|
61
66
|
"url": "https://github.com/wcandillon/react-native-webgpu/issues"
|
|
62
67
|
},
|
|
@@ -148,7 +153,8 @@
|
|
|
148
153
|
},
|
|
149
154
|
"ios": {
|
|
150
155
|
"componentProvider": {
|
|
151
|
-
"WebGPUView": "WebGPUView"
|
|
156
|
+
"WebGPUView": "WebGPUView",
|
|
157
|
+
"WebGPUNativeBufferView": "WebGPUNativeBufferView"
|
|
152
158
|
}
|
|
153
159
|
}
|
|
154
160
|
}
|
|
@@ -56,7 +56,7 @@ Pod::Spec.new do |s|
|
|
|
56
56
|
s.platforms = {
|
|
57
57
|
:ios => min_ios_version_supported,
|
|
58
58
|
:tvos => min_ios_version_supported,
|
|
59
|
-
:osx => "
|
|
59
|
+
:osx => "14.0",
|
|
60
60
|
}
|
|
61
61
|
s.source = { :git => "https://github.com/wcandillon/react-native-webgpu.git", :tag => "#{s.version}" }
|
|
62
62
|
|
package/src/Canvas.tsx
CHANGED
|
@@ -91,6 +91,22 @@ export interface CanvasProps extends ViewProps {
|
|
|
91
91
|
* options, see {@link AndroidCanvasProps}. Ignored on iOS and web.
|
|
92
92
|
*/
|
|
93
93
|
android?: AndroidCanvasProps;
|
|
94
|
+
/**
|
|
95
|
+
* The primaries the canvas's values are in, as `colorSpace` in
|
|
96
|
+
* `context.configure()`: `"srgb"` (the default) or `"display-p3"`. On iOS
|
|
97
|
+
* and macOS the layer is tagged with it when the canvas mounts, on the main
|
|
98
|
+
* thread: pass the same value to `configure()` (a mismatch is reported in
|
|
99
|
+
* development). A change on a mounted canvas is ignored, with a warning in
|
|
100
|
+
* development; remount the canvas (with a `key`) to change it. Android
|
|
101
|
+
* ignores it; on the web, `configure()` applies it.
|
|
102
|
+
*/
|
|
103
|
+
colorSpace?: PredefinedColorSpace;
|
|
104
|
+
/**
|
|
105
|
+
* What happens to values above 1.0 on an `rgba16float` canvas, as
|
|
106
|
+
* `toneMapping` in `context.configure()`. Read when the canvas mounts, like
|
|
107
|
+
* {@link CanvasProps.colorSpace}.
|
|
108
|
+
*/
|
|
109
|
+
toneMapping?: GPUCanvasToneMapping;
|
|
94
110
|
ref?: React.Ref<CanvasRef>;
|
|
95
111
|
}
|
|
96
112
|
|
|
@@ -202,6 +218,48 @@ const useBackingViewWarning = (android?: AndroidCanvasProps) => {
|
|
|
202
218
|
});
|
|
203
219
|
};
|
|
204
220
|
|
|
221
|
+
// The native props the `colorSpace` and `toneMapping` props resolve to. Like
|
|
222
|
+
// configure(), anything but "display-p3" and "extended" is the default.
|
|
223
|
+
const appleNativeProps = (
|
|
224
|
+
colorSpace?: PredefinedColorSpace,
|
|
225
|
+
toneMapping?: GPUCanvasToneMapping,
|
|
226
|
+
) => ({
|
|
227
|
+
appleDisplayP3: colorSpace === "display-p3",
|
|
228
|
+
appleExtendedDynamicRange: toneMapping?.mode === "extended",
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// Warns, in development on Apple platforms, about a change of the colorspace
|
|
232
|
+
// props that the native view ignores: the layer is tagged with them when the
|
|
233
|
+
// canvas mounts (see WebGPUView.mm). A change of the toneMapping object
|
|
234
|
+
// itself (a new object every render) is not one.
|
|
235
|
+
const useAppleColorSpaceWarning = (
|
|
236
|
+
colorSpace?: PredefinedColorSpace,
|
|
237
|
+
toneMapping?: GPUCanvasToneMapping,
|
|
238
|
+
) => {
|
|
239
|
+
const prev = useRef(appleNativeProps(colorSpace, toneMapping));
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
if (!__DEV__ || (Platform.OS !== "ios" && Platform.OS !== "macos")) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const next = appleNativeProps(colorSpace, toneMapping);
|
|
245
|
+
const warn = (prop: string) =>
|
|
246
|
+
console.warn(
|
|
247
|
+
`Ignoring a change of ${prop} on a mounted canvas: the layer is ` +
|
|
248
|
+
"tagged with it when the canvas mounts. Remount the canvas (give " +
|
|
249
|
+
"it a new key) to change it.",
|
|
250
|
+
);
|
|
251
|
+
if (prev.current.appleDisplayP3 !== next.appleDisplayP3) {
|
|
252
|
+
warn("colorSpace");
|
|
253
|
+
}
|
|
254
|
+
if (
|
|
255
|
+
prev.current.appleExtendedDynamicRange !== next.appleExtendedDynamicRange
|
|
256
|
+
) {
|
|
257
|
+
warn("toneMapping");
|
|
258
|
+
}
|
|
259
|
+
prev.current = next;
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
|
|
205
263
|
// A view measured relative to itself gets its layout size, without the
|
|
206
264
|
// transforms getBoundingClientRect() includes; measureLayout() calls back
|
|
207
265
|
// synchronously on the new architecture. On the web react-native-web measures
|
|
@@ -222,10 +280,17 @@ const measureLayout = (view: ViewRef) => {
|
|
|
222
280
|
return size;
|
|
223
281
|
};
|
|
224
282
|
|
|
225
|
-
export const Canvas = ({
|
|
283
|
+
export const Canvas = ({
|
|
284
|
+
android,
|
|
285
|
+
colorSpace,
|
|
286
|
+
toneMapping,
|
|
287
|
+
ref,
|
|
288
|
+
...props
|
|
289
|
+
}: CanvasProps) => {
|
|
226
290
|
const viewRef = useRef<ViewRef>(null);
|
|
227
291
|
const [contextId, _] = useState(() => generateContextId());
|
|
228
292
|
useBackingViewWarning(android);
|
|
293
|
+
useAppleColorSpaceWarning(colorSpace, toneMapping);
|
|
229
294
|
// Retire the native registry entry for this contextId on unmount. When a
|
|
230
295
|
// native surface is still attached, this is a no-op and the native view's
|
|
231
296
|
// own teardown retires the entry instead — which keeps StrictMode's
|
|
@@ -270,6 +335,7 @@ export const Canvas = ({ android, ref, ...props }: CanvasProps) => {
|
|
|
270
335
|
style={{ flex: 1 }}
|
|
271
336
|
contextId={contextId}
|
|
272
337
|
{...androidNativeProps(android)}
|
|
338
|
+
{...appleNativeProps(colorSpace, toneMapping)}
|
|
273
339
|
/>
|
|
274
340
|
</View>
|
|
275
341
|
);
|
|
@@ -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");
|
|
@@ -11,6 +11,10 @@ export interface NativeProps extends ViewProps {
|
|
|
11
11
|
androidOpaque?: CodegenTypes.WithDefault<boolean, false>;
|
|
12
12
|
androidZOrderOnTop?: CodegenTypes.WithDefault<boolean, false>;
|
|
13
13
|
androidZOrderMediaOverlay?: CodegenTypes.WithDefault<boolean, false>;
|
|
14
|
+
// The `colorSpace` and `toneMapping` props, resolved (see Canvas.tsx):
|
|
15
|
+
// read on iOS and macOS when the canvas mounts
|
|
16
|
+
appleDisplayP3?: CodegenTypes.WithDefault<boolean, false>;
|
|
17
|
+
appleExtendedDynamicRange?: CodegenTypes.WithDefault<boolean, false>;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -11,6 +11,10 @@ export interface NativeProps extends ViewProps {
|
|
|
11
11
|
androidOpaque?: boolean;
|
|
12
12
|
androidZOrderOnTop?: boolean;
|
|
13
13
|
androidZOrderMediaOverlay?: boolean;
|
|
14
|
+
// The `colorSpace` and `toneMapping` props, resolved: Apple-only, never
|
|
15
|
+
// reach the DOM (on the web, configure() applies them)
|
|
16
|
+
appleDisplayP3?: boolean;
|
|
17
|
+
appleExtendedDynamicRange?: boolean;
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -24,6 +28,8 @@ export default function WebGPUViewNativeComponent(
|
|
|
24
28
|
androidOpaque: _androidOpaque,
|
|
25
29
|
androidZOrderOnTop: _androidZOrderOnTop,
|
|
26
30
|
androidZOrderMediaOverlay: _androidZOrderMediaOverlay,
|
|
31
|
+
appleDisplayP3: _appleDisplayP3,
|
|
32
|
+
appleExtendedDynamicRange: _appleExtendedDynamicRange,
|
|
27
33
|
...rest
|
|
28
34
|
} = props;
|
|
29
35
|
|
|
@@ -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
|
+
};
|