react-native-webgpu 0.22.0 → 0.24.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 (165) hide show
  1. package/Package.swift +18 -129
  2. package/README.md +15 -2
  3. package/android/CMakeLists.txt +2 -1
  4. package/android/cpp/AndroidAnimationFrameSource.h +136 -0
  5. package/android/cpp/AndroidPlatformContext.h +65 -9
  6. package/android/cpp/cpp-adapter.cpp +148 -2
  7. package/android/src/main/java/com/webgpu/WebGPUModule.java +24 -7
  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 +2 -2
  12. package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
  13. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
  14. package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
  15. package/apple/AppleAnimationFrameSource.mm +208 -0
  16. package/apple/ApplePlatformContext.h +8 -1
  17. package/apple/ApplePlatformContext.mm +75 -2
  18. package/apple/NativeBufferFenceWait.mm +88 -0
  19. package/apple/WebGPUNativeBufferView.h +15 -0
  20. package/apple/WebGPUNativeBufferView.mm +200 -0
  21. package/binaries.json +53 -105
  22. package/cpp/dawn/common/ExternalTextureParams.h +3 -3
  23. package/cpp/dawn/include/dawn/dawn_proc.h +53 -0
  24. package/cpp/dawn/include/dawn/dawn_proc_table.h +329 -0
  25. package/cpp/dawn/include/dawn/dawn_thread_dispatch_proc.h +47 -0
  26. package/cpp/dawn/include/dawn/dawn_version.h +51 -0
  27. package/cpp/dawn/include/dawn/native/D3D11Backend.h +65 -0
  28. package/cpp/dawn/include/dawn/native/D3D12Backend.h +104 -0
  29. package/cpp/dawn/include/dawn/native/D3DBackend.h +56 -0
  30. package/cpp/dawn/include/dawn/native/DawnNative.h +375 -0
  31. package/cpp/dawn/include/dawn/native/MetalBackend.h +67 -0
  32. package/cpp/dawn/include/dawn/native/NullBackend.h +39 -0
  33. package/cpp/dawn/include/dawn/native/OpenGLBackend.h +89 -0
  34. package/cpp/dawn/include/dawn/native/VulkanBackend.h +199 -0
  35. package/cpp/dawn/include/dawn/native/WebGPUBackend.h +49 -0
  36. package/cpp/dawn/include/dawn/native/dawn_native_export.h +49 -0
  37. package/cpp/dawn/include/dawn/platform/DawnPlatform.h +210 -0
  38. package/cpp/dawn/include/dawn/platform/dawn_platform_export.h +49 -0
  39. package/cpp/dawn/include/dawn/replay/Replay.h +99 -0
  40. package/cpp/dawn/include/dawn/replay/dawn_replay_export.h +49 -0
  41. package/cpp/dawn/include/dawn/webgpu.h +5119 -0
  42. package/cpp/dawn/include/dawn/webgpu_cpp.h +10506 -0
  43. package/cpp/dawn/include/dawn/webgpu_cpp_print.h +2897 -0
  44. package/cpp/dawn/include/webgpu/webgpu.h +33 -0
  45. package/cpp/dawn/include/webgpu/webgpu_cpp.h +33 -0
  46. package/cpp/dawn/include/webgpu/webgpu_cpp_chained_struct.h +57 -0
  47. package/cpp/dawn/include/webgpu/webgpu_cpp_print.h +33 -0
  48. package/cpp/dawn/include/webgpu/webgpu_enum_class_bitmasks.h +163 -0
  49. package/cpp/dawn/include/webgpu/webgpu_glfw.h +88 -0
  50. package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
  51. package/cpp/rnwgpu/AnimationFrames.h +233 -0
  52. package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
  53. package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
  54. package/cpp/rnwgpu/PlatformContext.h +21 -1
  55. package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
  56. package/cpp/rnwgpu/SurfaceRegistry.h +2 -2
  57. package/cpp/rnwgpu/api/GPUAdapter.cpp +2 -2
  58. package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
  59. package/cpp/rnwgpu/api/GPUDevice.h +0 -18
  60. package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
  61. package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
  62. package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
  63. package/cpp/rnwgpu/api/GPUQueue.h +1 -1
  64. package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
  65. package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
  66. package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
  67. package/cpp/rnwgpu/api/GPUTexture.h +18 -0
  68. package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
  69. package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
  70. package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
  71. package/cpp/rnwgpu/api/RNWebGPU.h +206 -7
  72. package/cpp/rnwgpu/api/VideoFrame.h +5 -4
  73. package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
  74. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
  75. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
  76. package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
  77. package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
  78. package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
  79. package/lib/commonjs/NativeBufferView.js +131 -0
  80. package/lib/commonjs/NativeBufferView.js.map +1 -0
  81. package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
  82. package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
  83. package/lib/commonjs/animationFrame.js +91 -0
  84. package/lib/commonjs/animationFrame.js.map +1 -0
  85. package/lib/commonjs/index.js +16 -1
  86. package/lib/commonjs/index.js.map +1 -1
  87. package/lib/commonjs/main/index.js +12 -0
  88. package/lib/commonjs/main/index.js.map +1 -1
  89. package/lib/commonjs/mock.js +30 -1
  90. package/lib/commonjs/mock.js.map +1 -1
  91. package/lib/commonjs/video.js +21 -7
  92. package/lib/commonjs/video.js.map +1 -1
  93. package/lib/module/NativeBufferView.js +122 -0
  94. package/lib/module/NativeBufferView.js.map +1 -0
  95. package/lib/module/NativeBufferViewNativeComponent.js +4 -0
  96. package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
  97. package/lib/module/animationFrame.js +83 -0
  98. package/lib/module/animationFrame.js.map +1 -0
  99. package/lib/module/index.js +1 -0
  100. package/lib/module/index.js.map +1 -1
  101. package/lib/module/main/index.js +1 -0
  102. package/lib/module/main/index.js.map +1 -1
  103. package/lib/module/mock.js +21 -0
  104. package/lib/module/mock.js.map +1 -1
  105. package/lib/module/video.js +17 -4
  106. package/lib/module/video.js.map +1 -1
  107. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
  108. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
  109. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
  110. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
  111. package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
  112. package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
  113. package/lib/typescript/lib/commonjs/index.d.ts +2 -0
  114. package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
  115. package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
  116. package/lib/typescript/lib/commonjs/video.d.ts +9 -4
  117. package/lib/typescript/lib/commonjs/video.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/animationFrame.d.ts +3 -0
  123. package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
  124. package/lib/typescript/lib/module/index.d.ts +1 -0
  125. package/lib/typescript/lib/module/main/index.d.ts +1 -0
  126. package/lib/typescript/lib/module/mock.d.ts +14 -0
  127. package/lib/typescript/lib/module/mock.d.ts.map +1 -1
  128. package/lib/typescript/lib/module/video.d.ts +4 -3
  129. package/lib/typescript/lib/module/video.d.ts.map +1 -1
  130. package/lib/typescript/react-native.config.d.ts +1 -0
  131. package/lib/typescript/src/NativeBufferView.d.ts +99 -0
  132. package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
  133. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
  134. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
  135. package/lib/typescript/src/animationFrame.d.ts +59 -0
  136. package/lib/typescript/src/animationFrame.d.ts.map +1 -0
  137. package/lib/typescript/src/index.d.ts +18 -8
  138. package/lib/typescript/src/index.d.ts.map +1 -1
  139. package/lib/typescript/src/main/index.d.ts +1 -0
  140. package/lib/typescript/src/main/index.d.ts.map +1 -1
  141. package/lib/typescript/src/mock.d.ts +11 -0
  142. package/lib/typescript/src/mock.d.ts.map +1 -1
  143. package/lib/typescript/src/types.d.ts +33 -32
  144. package/lib/typescript/src/types.d.ts.map +1 -1
  145. package/lib/typescript/src/video.d.ts +18 -6
  146. package/lib/typescript/src/video.d.ts.map +1 -1
  147. package/package.json +16 -6
  148. package/react-native-webgpu.podspec +47 -79
  149. package/react-native.config.js +9 -6
  150. package/scripts/apple-binaries-utils.sh +280 -0
  151. package/scripts/prepare-apple-binaries.sh +309 -0
  152. package/scripts/spm-plugin.js +130 -0
  153. package/src/NativeBufferView.tsx +199 -0
  154. package/src/NativeBufferViewNativeComponent.ts +20 -0
  155. package/src/animationFrame.ts +82 -0
  156. package/src/index.tsx +48 -42
  157. package/src/main/index.tsx +1 -0
  158. package/src/mock.ts +35 -0
  159. package/src/types.ts +62 -88
  160. package/src/video.ts +32 -12
  161. package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
  162. package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
  163. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
  164. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
  165. package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
@@ -15,6 +15,8 @@
15
15
  #include "GPUSharedFenceState.h"
16
16
  #include "GPUTexture.h"
17
17
  #include "GPUTextureDescriptor.h"
18
+ #include "NativeFrameImport.h"
19
+ #include "VideoFrame.h"
18
20
 
19
21
  namespace rnwgpu {
20
22
 
@@ -24,11 +26,23 @@ class GPUSharedTextureMemory : public NativeObject<GPUSharedTextureMemory> {
24
26
  public:
25
27
  static constexpr const char *CLASS_NAME = "GPUSharedTextureMemory";
26
28
 
29
+ // `source` is the NativeBuffer the memory was imported from, null for an
30
+ // import from a raw handle: the memory keeps it alive, and its color space
31
+ // goes to the textures created from the memory (see SharedTextureOrigin).
27
32
  explicit GPUSharedTextureMemory(wgpu::Device device,
28
33
  wgpu::SharedTextureMemory instance,
29
- std::string label)
34
+ std::string label,
35
+ std::shared_ptr<VideoFrame> source = nullptr)
30
36
  : NativeObject(CLASS_NAME), _device(std::move(device)),
31
- _instance(std::move(instance)), _label(std::move(label)) {}
37
+ _instance(std::move(instance)), _source(std::move(source)),
38
+ _label(std::move(label)) {
39
+ VideoFrameColorSpace colorSpace;
40
+ if (_source) {
41
+ colorSpace = _source->handle().colorSpace;
42
+ }
43
+ _origin = std::make_shared<native_frame::SharedTextureOrigin>(
44
+ native_frame::SharedTextureOrigin{_instance, colorSpace, _label});
45
+ }
32
46
 
33
47
  public:
34
48
  std::string getBrand() { return CLASS_NAME; }
@@ -50,6 +64,7 @@ public:
50
64
  std::string getLabel() { return _label; }
51
65
  void setLabel(const std::string &label) {
52
66
  _label = label;
67
+ _origin->label = label;
53
68
  if (_instance) {
54
69
  _instance.SetLabel(_label.c_str());
55
70
  }
@@ -87,6 +102,10 @@ private:
87
102
 
88
103
  wgpu::Device _device;
89
104
  wgpu::SharedTextureMemory _instance;
105
+ std::shared_ptr<VideoFrame> _source;
106
+ // Shared with every texture created from the memory; destroy() clears its
107
+ // reference to the import.
108
+ std::shared_ptr<native_frame::SharedTextureOrigin> _origin;
90
109
  // Kept by destroy(): like Dawn, createTexture() describes the error texture
91
110
  // it then returns with them.
92
111
  wgpu::SharedTextureMemoryProperties _properties;
@@ -3,6 +3,7 @@
3
3
  #include <algorithm>
4
4
  #include <memory>
5
5
  #include <string>
6
+ #include <utility>
6
7
 
7
8
  #include "Unions.h"
8
9
 
@@ -17,6 +18,10 @@ namespace rnwgpu {
17
18
 
18
19
  namespace jsi = facebook::jsi;
19
20
 
21
+ namespace native_frame {
22
+ struct SharedTextureOrigin;
23
+ } // namespace native_frame
24
+
20
25
  class GPUTexture : public NativeObject<GPUTexture> {
21
26
  public:
22
27
  static constexpr const char *CLASS_NAME = "GPUTexture";
@@ -77,6 +82,18 @@ public:
77
82
 
78
83
  inline const wgpu::Texture get() { return _instance; }
79
84
 
85
+ // Set on a texture created by GPUSharedTextureMemory.createTexture(): the
86
+ // import it aliases, which importExternalTexture({ source: texture }) needs
87
+ // to sample a YUV buffer. Null for every other texture.
88
+ void setSharedTextureOrigin(
89
+ std::shared_ptr<native_frame::SharedTextureOrigin> origin) {
90
+ _sharedTextureOrigin = std::move(origin);
91
+ }
92
+ const std::shared_ptr<native_frame::SharedTextureOrigin> &
93
+ sharedTextureOrigin() const {
94
+ return _sharedTextureOrigin;
95
+ }
96
+
80
97
  size_t getMemoryPressure() override {
81
98
  if (!_reportsMemoryPressure) {
82
99
  return sizeof(GPUTexture);
@@ -171,6 +188,7 @@ private:
171
188
  wgpu::Texture _instance;
172
189
  std::string _label;
173
190
  bool _reportsMemoryPressure = true;
191
+ std::shared_ptr<native_frame::SharedTextureOrigin> _sharedTextureOrigin;
174
192
  };
175
193
 
176
194
  } // namespace rnwgpu
@@ -8,7 +8,7 @@ namespace rnwgpu {
8
8
 
9
9
  // The Dawn features behind native frame import on the current platform:
10
10
  // importSharedTextureMemory, importExternalTexture and
11
- // copyExternalImageToTexture with a NativeVideoFrame. Like
11
+ // copyExternalImageToTexture with a NativeBuffer. Like
12
12
  // importExternalTexture on the web, the capability needs no feature request:
13
13
  // requestDevice() enables each of these the adapter supports (see
14
14
  // GPUAdapter::requestDevice). A device created by another library and wrapped
@@ -399,7 +399,7 @@ wgpu::SharedFence importAcquireFence(const wgpu::Device &device,
399
399
 
400
400
  wgpu::ExternalTexture createExternalTexture(
401
401
  const wgpu::Device &device, const wgpu::SharedTextureMemory &memory,
402
- const wgpu::Texture &texture, const VideoFrameHandle &frame,
402
+ const wgpu::Texture &texture, const VideoFrameColorSpace &colorSpace,
403
403
  const GPUExternalTextureDescriptor &descriptor, const std::string &label,
404
404
  const char *caller) {
405
405
  wgpu::ExternalTextureDescriptor extDesc{};
@@ -419,7 +419,8 @@ wgpu::ExternalTexture createExternalTexture(
419
419
  // frame's color space (from the CVPixelBuffer attachments; BT.709 limited
420
420
  // range, the norm for HD video, where they are missing). A BGRA frame is
421
421
  // RGB already: no-op parameters.
422
- bool isYuv = frame.pixelFormat == VideoPixelFormat::NV12;
422
+ bool isYuv =
423
+ texture.GetFormat() == wgpu::TextureFormat::R8BG8Biplanar420Unorm;
423
424
  {
424
425
  wgpu::TextureViewDescriptor v{};
425
426
  v.aspect =
@@ -431,7 +432,7 @@ wgpu::ExternalTexture createExternalTexture(
431
432
  v.aspect = wgpu::TextureAspect::Plane1Only;
432
433
  plane1 = texture.CreateView(&v);
433
434
  params = colorSpaceParams(
434
- resolveColorSpace(frame.colorSpace, descriptor.sourceColorSpace,
435
+ resolveColorSpace(colorSpace, descriptor.sourceColorSpace,
435
436
  wgpu::ColorSpaceYCbCrMatrixDawn::Rec709,
436
437
  wgpu::ColorSpaceYCbCrRangeDawn::Narrow),
437
438
  descriptor.colorSpace, caller);
@@ -482,7 +483,7 @@ wgpu::ExternalTexture createExternalTexture(
482
483
  : wgpu::ColorSpaceYCbCrRangeDawn::Narrow;
483
484
  }
484
485
  }
485
- params = colorSpaceParams(resolveColorSpace(frame.colorSpace,
486
+ params = colorSpaceParams(resolveColorSpace(colorSpace,
486
487
  descriptor.sourceColorSpace,
487
488
  fallbackMatrix, fallbackRange),
488
489
  descriptor.colorSpace, caller);
@@ -491,7 +492,7 @@ wgpu::ExternalTexture createExternalTexture(
491
492
  extDesc.plane0 = plane0;
492
493
  #else
493
494
  (void)memory;
494
- (void)frame;
495
+ (void)colorSpace;
495
496
  throw std::runtime_error(prefix(caller) + "unsupported platform");
496
497
  #endif
497
498
  applyColorSpaceParams(extDesc, params);
@@ -541,15 +542,17 @@ wgpu::ExternalTexture createExternalTexture(
541
542
  }
542
543
 
543
544
  bool mergeFences(const wgpu::Device &device,
544
- const wgpu::SharedTextureMemoryEndAccessState &state,
545
+ const std::vector<wgpu::SharedFence> &fences,
546
+ const std::vector<uint64_t> &signaledValues,
545
547
  wgpu::SharedFence *fence, uint64_t *signaledValue,
546
548
  const char *caller) {
547
- if (state.fenceCount == 0) {
549
+ const size_t fenceCount = fences.size();
550
+ if (fenceCount == 0) {
548
551
  return false;
549
552
  }
550
- if (state.fenceCount == 1) {
551
- *fence = state.fences[0];
552
- *signaledValue = state.signaledValues[0];
553
+ if (fenceCount == 1) {
554
+ *fence = fences[0];
555
+ *signaledValue = signaledValues[0];
553
556
  return true;
554
557
  }
555
558
  #if defined(__ANDROID__)
@@ -557,8 +560,8 @@ bool mergeFences(const wgpu::Device &device,
557
560
  // duplicates into one sync fd, then import that as the fence to return.
558
561
  // Dawn duplicates the fd on import, so the merged one is closed here.
559
562
  UniqueFd merged;
560
- for (size_t i = 0; i < state.fenceCount; i++) {
561
- wgpu::SharedFence source = state.fences[i];
563
+ for (size_t i = 0; i < fenceCount; i++) {
564
+ wgpu::SharedFence source = fences[i];
562
565
  wgpu::SharedFenceExportInfo info{};
563
566
  wgpu::SharedFenceSyncFDExportInfo fdInfo{};
564
567
  info.nextInChain = &fdInfo;
@@ -611,8 +614,8 @@ bool mergeFences(const wgpu::Device &device,
611
614
  (void)device;
612
615
  void *event = nullptr;
613
616
  size_t last = 0;
614
- for (size_t i = 0; i < state.fenceCount; i++) {
615
- wgpu::SharedFence source = state.fences[i];
617
+ for (size_t i = 0; i < fenceCount; i++) {
618
+ wgpu::SharedFence source = fences[i];
616
619
  wgpu::SharedFenceExportInfo info{};
617
620
  wgpu::SharedFenceMTLSharedEventExportInfo eventInfo{};
618
621
  info.nextInChain = &eventInfo;
@@ -624,12 +627,12 @@ bool mergeFences(const wgpu::Device &device,
624
627
  "shared events and cannot be merged into one");
625
628
  }
626
629
  event = eventInfo.sharedEvent;
627
- if (state.signaledValues[i] > state.signaledValues[last]) {
630
+ if (signaledValues[i] > signaledValues[last]) {
628
631
  last = i;
629
632
  }
630
633
  }
631
- *fence = state.fences[last];
632
- *signaledValue = state.signaledValues[last];
634
+ *fence = fences[last];
635
+ *signaledValue = signaledValues[last];
633
636
  return true;
634
637
  #endif
635
638
  }
@@ -12,17 +12,28 @@ namespace rnwgpu {
12
12
 
13
13
  struct GPUExternalTextureDescriptor;
14
14
 
15
- // The steps of importing a native frame (an IOSurface on Apple platforms, an
15
+ // The steps of importing a native buffer (an IOSurface on Apple platforms, an
16
16
  // AHardwareBuffer on Android) into Dawn, shared by importExternalTexture(),
17
- // importSharedTextureMemory() and the native buffer wrappers of
18
- // createNativeBufferTexture() / createNativeBufferExternalTexture(): the
19
- // import, the access window around each use, and the external texture that
20
- // samples a YUV frame as RGB. The same steps as Dawn's own Android layer
21
- // (tools/android/webgpu, GPUImportedAHB.cpp), so a JS caller can follow its
22
- // import-once, begin/end-per-frame recipe. Each step throws with the cause,
23
- // prefixed with `caller`, the entry point the message names.
17
+ // importSharedTextureMemory() and copyExternalImageToTexture(): the import,
18
+ // the access window around each use, and the external texture that samples a
19
+ // YUV buffer as RGB. Each step throws with the cause, prefixed with `caller`,
20
+ // the entry point the message names.
24
21
  namespace native_frame {
25
22
 
23
+ // What a texture created from a GPUSharedTextureMemory remembers of its
24
+ // import, so importExternalTexture({ source: texture }) can build an external
25
+ // texture over it: the memory (the YCbCr properties of an Android buffer) and
26
+ // the color space the producer reported for the buffer, if any. One per
27
+ // GPUSharedTextureMemory, shared with its textures: the memory's destroy()
28
+ // clears `memory`, so a texture that is still referenced from JS never keeps
29
+ // Dawn's import (and the native buffer behind it) alive after that.
30
+ struct SharedTextureOrigin {
31
+ wgpu::SharedTextureMemory memory;
32
+ VideoFrameColorSpace colorSpace;
33
+ // The label of the GPUSharedTextureMemory, to name it once destroyed.
34
+ std::string label;
35
+ };
36
+
26
37
  // Import the frame's buffer as shared texture memory.
27
38
  wgpu::SharedTextureMemory importMemory(const wgpu::Device &device,
28
39
  const VideoFrameHandle &frame,
@@ -60,24 +71,24 @@ void endAccess(const wgpu::SharedTextureMemory &memory,
60
71
  wgpu::SharedFence importAcquireFence(const wgpu::Device &device,
61
72
  const VideoFrameHandle &frame);
62
73
 
63
- // The external texture that samples `texture`, created from the buffer of
64
- // `frame`, as RGB: a YUV frame is converted with the color space of the frame
65
- // (overridden by the descriptor's sourceColorSpace), and the descriptor's
66
- // crop, rotation and mirroring apply.
74
+ // The external texture that samples `texture`, created from `memory`, as
75
+ // RGB: a YUV buffer is converted with `colorSpace`, what its producer
76
+ // reported (overridden by the descriptor's sourceColorSpace), and the
77
+ // descriptor's crop, rotation and mirroring apply. No access begins here.
67
78
  wgpu::ExternalTexture createExternalTexture(
68
79
  const wgpu::Device &device, const wgpu::SharedTextureMemory &memory,
69
- const wgpu::Texture &texture, const VideoFrameHandle &frame,
80
+ const wgpu::Texture &texture, const VideoFrameColorSpace &colorSpace,
70
81
  const GPUExternalTextureDescriptor &descriptor, const std::string &label,
71
82
  const char *caller);
72
83
 
73
- // The fences of an end-access state as one: on Android their sync fds are
74
- // merged into a single one (what Dawn's Android layer hands back from
75
- // endAccess), on Apple platforms the device's shared event at the largest
76
- // value. False when the state has no fence (or, on Android, only signaled
77
- // ones). Throws if the fences cannot be merged into one: Android fences that
78
- // are not sync fds, Apple fences on different shared events.
84
+ // Fences as one: on Android their sync fds are merged into a single one, on
85
+ // Apple platforms the shared event at the largest value. False when there is
86
+ // no fence (or, on Android, only signaled ones). Throws if the fences cannot
87
+ // be merged into one: Android fences that are not sync fds, Apple fences on
88
+ // different shared events.
79
89
  bool mergeFences(const wgpu::Device &device,
80
- const wgpu::SharedTextureMemoryEndAccessState &state,
90
+ const std::vector<wgpu::SharedFence> &fences,
91
+ const std::vector<uint64_t> &signaledValues,
81
92
  wgpu::SharedFence *fence, uint64_t *signaledValue,
82
93
  const char *caller);
83
94
 
@@ -6,6 +6,10 @@
6
6
  #include <utility>
7
7
  #include <vector>
8
8
 
9
+ #if defined(__ANDROID__)
10
+ #include <unistd.h>
11
+ #endif
12
+
9
13
  #include "NativeObject.h"
10
14
 
11
15
  #include "ArrayBuffer.h"
@@ -13,8 +17,11 @@
13
17
  #include "GPU.h"
14
18
  #include "GPUCanvasContext.h"
15
19
  #include "GPUDevice.h"
20
+ #include "GPUSharedFenceState.h"
16
21
  #include "GPUTexture.h"
17
22
  #include "ImageBitmap.h"
23
+ #include "NativeBufferViewRegistry.h"
24
+ #include "NativeFrameImport.h"
18
25
  #include "PlatformContext.h"
19
26
  #include "VideoFrame.h"
20
27
  #include "VideoPlayer.h"
@@ -313,17 +320,199 @@ public:
313
320
  // Wrap a CVPixelBufferRef / AHardwareBuffer* pointer (typed as void* via
314
321
  // BigInt on the JS side) into one of our VideoFrames. The native side
315
322
  // CFRetains / acquires so the caller can release immediately.
316
- std::shared_ptr<VideoFrame> createVideoFrameFromNativeBuffer(void *pointer) {
323
+ std::shared_ptr<VideoFrame> createNativeBufferFromPointer(void *pointer) {
317
324
  auto handle = _platformContext->wrapNativeBuffer(pointer);
318
325
  return std::make_shared<VideoFrame>(std::move(handle));
319
326
  }
320
327
 
328
+ // createNativeBuffer(width, height, { format?, colorSpace? }): an empty
329
+ // native buffer (AHardwareBuffer / IOSurface) to render into through
330
+ // importSharedTextureMemory() and display with presentNativeBuffer().
331
+ // `format` defaults to the preferred canvas format of the platform
332
+ // (rgba8unorm on Android, bgra8unorm on Apple platforms); rgba16float works
333
+ // on both (more precision, not extended range: NativeBufferView clamps to
334
+ // [0, 1]). `colorSpace` is "srgb" (the default) or "display-p3".
335
+ jsi::Value createNativeBuffer(jsi::Runtime &runtime,
336
+ const jsi::Value & /*thisVal*/,
337
+ const jsi::Value *args, size_t count) {
338
+ auto dimension = [&](size_t index, const char *name) {
339
+ if (count <= index || !args[index].isNumber()) {
340
+ throw jsi::JSError(runtime, std::string("createNativeBuffer: ") + name +
341
+ " must be a number");
342
+ }
343
+ const double value = args[index].asNumber();
344
+ if (!(value >= 1 && value <= 16384) || value != std::floor(value)) {
345
+ throw jsi::JSError(runtime, std::string("createNativeBuffer: ") + name +
346
+ " must be an integer in [1, 16384]");
347
+ }
348
+ return static_cast<uint32_t>(value);
349
+ };
350
+ const uint32_t width = dimension(0, "width");
351
+ const uint32_t height = dimension(1, "height");
352
+ #if defined(__APPLE__)
353
+ wgpu::TextureFormat format = wgpu::TextureFormat::BGRA8Unorm;
354
+ #else
355
+ wgpu::TextureFormat format = wgpu::TextureFormat::RGBA8Unorm;
356
+ #endif
357
+ bool displayP3 = false;
358
+ if (count > 2 && args[2].isObject()) {
359
+ auto options = args[2].getObject(runtime);
360
+ auto formatValue = options.getProperty(runtime, "format");
361
+ if (formatValue.isString()) {
362
+ auto name = formatValue.getString(runtime).utf8(runtime);
363
+ if (name == "rgba8unorm") {
364
+ format = wgpu::TextureFormat::RGBA8Unorm;
365
+ } else if (name == "bgra8unorm") {
366
+ format = wgpu::TextureFormat::BGRA8Unorm;
367
+ } else if (name == "rgba16float") {
368
+ format = wgpu::TextureFormat::RGBA16Float;
369
+ } else {
370
+ throw jsi::JSError(
371
+ runtime, "createNativeBuffer: unsupported format '" + name + "'");
372
+ }
373
+ }
374
+ auto colorSpaceValue = options.getProperty(runtime, "colorSpace");
375
+ if (colorSpaceValue.isString()) {
376
+ auto name = colorSpaceValue.getString(runtime).utf8(runtime);
377
+ if (name == "display-p3") {
378
+ displayP3 = true;
379
+ } else if (name != "srgb") {
380
+ throw jsi::JSError(runtime,
381
+ "createNativeBuffer: unsupported colorSpace '" +
382
+ name + "'");
383
+ }
384
+ }
385
+ }
386
+ VideoFrameHandle handle;
387
+ try {
388
+ handle = _platformContext->createNativeBuffer(width, height, format,
389
+ displayP3);
390
+ } catch (const std::exception &e) {
391
+ throw jsi::JSError(runtime, e.what());
392
+ }
393
+ return VideoFrame::create(runtime,
394
+ std::make_shared<VideoFrame>(std::move(handle)));
395
+ }
396
+
397
+ // The id of a NativeBufferView about to mount, unique for the life of the
398
+ // process (see NativeBufferViewRegistry). Presents are accepted once the
399
+ // component retains it.
400
+ int allocateNativeBufferViewId() {
401
+ return NativeBufferViewRegistry::getInstance().allocateViewId();
402
+ }
403
+
404
+ // The NativeBufferView component mounted (again, when React StrictMode
405
+ // re-runs its effects).
406
+ void retainNativeBufferView(int viewId) {
407
+ NativeBufferViewRegistry::getInstance().retainFromJS(viewId);
408
+ }
409
+
410
+ // The NativeBufferView component unmounted.
411
+ void releaseNativeBufferView(int viewId) {
412
+ NativeBufferViewRegistry::getInstance().releaseFromJS(viewId);
413
+ }
414
+
415
+ // presentNativeBuffer(viewId, frame, fences?): makes `frame` the content of
416
+ // the NativeBufferView `viewId` once the GPU work that wrote it is done, or
417
+ // right away without fences. `fences` is what
418
+ // GPUSharedTextureMemory.endAccess() returns ({ fences }), a list of
419
+ // GPUSharedFenceState, or a single one; several are merged into one. On
420
+ // Android it also waits for the frame's own acquire fence, if it has one (a
421
+ // buffer the producer may still be writing). Returns immediately; the view
422
+ // shows the buffer on the next vsync after that. The view takes its own
423
+ // reference on the buffer, but that does not keep a buffer pool from reusing
424
+ // it: release the frame right away only if nothing else will write the buffer
425
+ // while it is displayed (see NativeBufferView.tsx).
426
+ jsi::Value presentNativeBuffer(jsi::Runtime &runtime,
427
+ const jsi::Value & /*thisVal*/,
428
+ const jsi::Value *args, size_t count) {
429
+ if (count < 2 || !args[0].isNumber() || !args[1].isObject()) {
430
+ throw jsi::JSError(runtime, "presentNativeBuffer(viewId, frame, fence?): "
431
+ "expected a view id and a NativeBuffer");
432
+ }
433
+ const int viewId = static_cast<int>(args[0].asNumber());
434
+ auto frame = JSIConverter<std::shared_ptr<VideoFrame>>::fromJSI(
435
+ runtime, args[1], false);
436
+ if (!frame) {
437
+ throw jsi::JSError(
438
+ runtime, "presentNativeBuffer(): frame is not a NativeBuffer");
439
+ }
440
+ wgpu::SharedFence fence;
441
+ uint64_t signaledValue = 0;
442
+ if (count > 2 && args[2].isObject()) {
443
+ // { fences } (an end-access state), an array, or one fence state.
444
+ auto arg = args[2].getObject(runtime);
445
+ jsi::Value list = jsi::Value::undefined();
446
+ if (arg.isArray(runtime)) {
447
+ list = jsi::Value(runtime, arg);
448
+ } else if (arg.hasProperty(runtime, "fences")) {
449
+ list = arg.getProperty(runtime, "fences");
450
+ }
451
+ std::vector<std::shared_ptr<GPUSharedFenceState>> states;
452
+ if (list.isObject()) {
453
+ states = JSIConverter<
454
+ std::vector<std::shared_ptr<GPUSharedFenceState>>>::fromJSI(runtime,
455
+ list,
456
+ false);
457
+ } else {
458
+ states.push_back(
459
+ JSIConverter<std::shared_ptr<GPUSharedFenceState>>::fromJSI(
460
+ runtime, args[2], false));
461
+ }
462
+ std::vector<wgpu::SharedFence> fences;
463
+ std::vector<uint64_t> values;
464
+ wgpu::Device device;
465
+ for (const auto &state : states) {
466
+ if (!state || !state->fence) {
467
+ continue;
468
+ }
469
+ auto native = state->fence->get();
470
+ if (!native) {
471
+ throw jsi::JSError(runtime,
472
+ "presentNativeBuffer(): a fence was destroyed");
473
+ }
474
+ device = state->fence->device();
475
+ fences.push_back(std::move(native));
476
+ values.push_back(state->signaledValue);
477
+ }
478
+ try {
479
+ native_frame::mergeFences(device, fences, values, &fence,
480
+ &signaledValue, "presentNativeBuffer");
481
+ } catch (const std::exception &e) {
482
+ throw jsi::JSError(runtime, e.what());
483
+ }
484
+ }
485
+ std::shared_ptr<NativeBufferContent> content;
486
+ try {
487
+ content = NativeBufferContent::fromFrame(frame->handle());
488
+ } catch (const std::exception &e) {
489
+ throw jsi::JSError(runtime, e.what());
490
+ }
491
+ int acquireFenceFd = -1;
492
+ #if defined(__ANDROID__)
493
+ // Duplicated: the frame closes its own fd when it is released, which may
494
+ // be before the wait is over.
495
+ if (frame->handle().acquireFenceFd >= 0) {
496
+ acquireFenceFd = ::dup(frame->handle().acquireFenceFd);
497
+ if (acquireFenceFd < 0) {
498
+ throw jsi::JSError(runtime, "presentNativeBuffer(): could not "
499
+ "duplicate the frame's acquire fence");
500
+ }
501
+ }
502
+ #endif
503
+ NativeBufferViewRegistry::getInstance().present(
504
+ viewId, std::move(content), std::move(fence), signaledValue,
505
+ acquireFenceFd);
506
+ return jsi::Value::undefined();
507
+ }
508
+
321
509
  std::shared_ptr<VideoPlayer>
322
- createVideoPlayer(std::string path, std::optional<std::string> pixelFormat) {
510
+ createTestVideoPlayer(std::string path,
511
+ std::optional<std::string> pixelFormat) {
323
512
  auto format = (pixelFormat && pixelFormat.value() == "nv12")
324
513
  ? VideoPixelFormat::NV12
325
514
  : VideoPixelFormat::BGRA8;
326
- auto impl = _platformContext->createVideoPlayer(path, format);
515
+ auto impl = _platformContext->createTestVideoPlayer(path, format);
327
516
  return std::make_shared<VideoPlayer>(std::move(impl));
328
517
  }
329
518
 
@@ -388,13 +577,23 @@ public:
388
577
  &RNWebGPU::loadVideoFrame);
389
578
  installMethod(runtime, prototype, "createTestVideoFrame",
390
579
  &RNWebGPU::createTestVideoFrame);
391
- installMethod(runtime, prototype, "createVideoFrameFromNativeBuffer",
392
- &RNWebGPU::createVideoFrameFromNativeBuffer);
580
+ installMethod(runtime, prototype, "createNativeBufferFromPointer",
581
+ &RNWebGPU::createNativeBufferFromPointer);
393
582
  installMethodWithRuntime(runtime, prototype, "importDevice",
394
583
  &RNWebGPU::importDevice);
395
584
  installMethod(runtime, prototype, "adoptTexture", &RNWebGPU::adoptTexture);
396
- installMethod(runtime, prototype, "createVideoPlayer",
397
- &RNWebGPU::createVideoPlayer);
585
+ installMethod(runtime, prototype, "createTestVideoPlayer",
586
+ &RNWebGPU::createTestVideoPlayer);
587
+ installMethod(runtime, prototype, "createNativeBuffer",
588
+ &RNWebGPU::createNativeBuffer);
589
+ installMethod(runtime, prototype, "allocateNativeBufferViewId",
590
+ &RNWebGPU::allocateNativeBufferViewId);
591
+ installMethod(runtime, prototype, "retainNativeBufferView",
592
+ &RNWebGPU::retainNativeBufferView);
593
+ installMethod(runtime, prototype, "releaseNativeBufferView",
594
+ &RNWebGPU::releaseNativeBufferView);
595
+ installMethod(runtime, prototype, "presentNativeBuffer",
596
+ &RNWebGPU::presentNativeBuffer);
398
597
  installMethod(runtime, prototype, "writeTestVideoFile",
399
598
  &RNWebGPU::writeTestVideoFile);
400
599
  }
@@ -24,10 +24,11 @@ public:
24
24
  // globalThis[CLASS_NAME], and the NativeObjectRegistry / worklet serializer
25
25
  // are keyed off it, so it must NOT be "VideoFrame": that would install our
26
26
  // constructor over the WebCodecs `VideoFrame` global and shadow it at
27
- // runtime. "NativeVideoFrame" matches the public TypeScript type (see
28
- // src/types.ts) and keeps the two namespaces distinct. The C++ class stays
29
- // `VideoFrame` for brevity; only the brand is namespaced.
30
- static constexpr const char *CLASS_NAME = "NativeVideoFrame";
27
+ // runtime. "NativeBuffer" matches the public TypeScript type (see
28
+ // src/types.ts) and keeps the two
29
+ // namespaces distinct. The C++ class stays `VideoFrame` for brevity; only
30
+ // the brand is namespaced.
31
+ static constexpr const char *CLASS_NAME = "NativeBuffer";
31
32
 
32
33
  explicit VideoFrame(VideoFrameHandle handle)
33
34
  : NativeObject(CLASS_NAME), _handle(std::move(handle)) {}
@@ -23,7 +23,7 @@ namespace rnwgpu {
23
23
  // imported as an external texture, so Dawn does the YUV to RGB conversion and
24
24
  // applies the rotation and the mirroring; a full-screen triangle then samples
25
25
  // it into the destination. This is what backs
26
- // GPUQueue.copyExternalImageToTexture for NativeVideoFrame sources, and the
26
+ // GPUQueue.copyExternalImageToTexture for NativeBuffer sources, and the
27
27
  // zero-copy path of GPUQueue.drawElementImageToTexture, which draws the
28
28
  // buffer the platform rendered a view into.
29
29
  //
@@ -64,7 +64,7 @@ public:
64
64
  const Request &request) {
65
65
  if (!request.frame || request.frame->handle().handle == nullptr) {
66
66
  throw std::runtime_error(
67
- "copyExternalImageToTexture: the NativeVideoFrame was released");
67
+ "copyExternalImageToTexture: the NativeBuffer was released");
68
68
  }
69
69
  if (!request.texture) {
70
70
  throw std::runtime_error(
@@ -9,6 +9,7 @@
9
9
 
10
10
  #include "GPUExtent2D.h"
11
11
  #include "GPUOrigin2D.h"
12
+ #include "GPUTexture.h"
12
13
  #include "JSIConverter.h"
13
14
  #include "VideoFrame.h"
14
15
 
@@ -16,12 +17,14 @@ namespace jsi = facebook::jsi;
16
17
 
17
18
  namespace rnwgpu {
18
19
 
19
- // Mirror of GPUExternalTextureDescriptor from the WebGPU spec, but with our
20
- // VideoFrame as the (only) supported source. `colorSpace` is the spec member:
21
- // the color space of the RGB the shader samples, "srgb" (the default) or
22
- // "display-p3"; the frame's own color space comes with the frame, and
23
- // `sourceColorSpace` overrides the members of it the caller knows better (a
24
- // buffer wrapped from another library carries none).
20
+ // Mirror of GPUExternalTextureDescriptor from the WebGPU spec, with a native
21
+ // buffer as the source: a NativeBuffer (`source`), imported for this external
22
+ // texture alone, or a texture created from a GPUSharedTextureMemory
23
+ // (`sourceTexture`), whose import and access windows the caller owns.
24
+ // `colorSpace` is the spec member: the color space of the RGB the shader
25
+ // samples, "srgb" (the default) or "display-p3"; the frame's own color space
26
+ // comes with the frame, and `sourceColorSpace` overrides the members of it the
27
+ // caller knows better (a buffer wrapped from another library carries none).
25
28
  //
26
29
  // `rotation` / `mirrored` are a non-spec extension: camera frames (e.g. from
27
30
  // VisionCamera) arrive in the sensor's native orientation, which differs
@@ -31,11 +34,9 @@ namespace rnwgpu {
31
34
  // passes. `rotation` is in degrees and must be one of 0 / 90 / 180 / 270.
32
35
  // `cropOrigin` / `cropSize` select the part of the frame the external texture
33
36
  // shows (Dawn's cropOrigin / cropSize), the whole frame by default.
34
- //
35
- // createNativeBufferExternalTexture() takes the same descriptor without a
36
- // source: the frame is its first argument.
37
37
  struct GPUExternalTextureDescriptor {
38
38
  std::shared_ptr<VideoFrame> source;
39
+ std::shared_ptr<GPUTexture> sourceTexture;
39
40
  std::optional<std::string> label;
40
41
  std::optional<std::string> colorSpace;
41
42
  std::optional<VideoFrameColorSpace> sourceColorSpace;
@@ -49,7 +50,7 @@ namespace external_texture_color_space {
49
50
 
50
51
  // The JS names of the members of a source color space, the Dawn enums
51
52
  // spelled the way the frame producers report them (see
52
- // NativeVideoFrameColorSpace in src/types.ts).
53
+ // NativeBufferColorSpace in src/types.ts).
53
54
  inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &name) {
54
55
  if (name == "identity") {
55
56
  return wgpu::ColorSpaceYCbCrMatrixDawn::Identity;
@@ -174,10 +175,16 @@ struct JSIConverter<std::shared_ptr<rnwgpu::GPUExternalTextureDescriptor>> {
174
175
  auto value = arg.getObject(runtime);
175
176
  if (value.hasProperty(runtime, "source")) {
176
177
  auto prop = value.getProperty(runtime, "source");
177
- if (!prop.isUndefined() && !prop.isNull()) {
178
- result->source =
179
- JSIConverter<std::shared_ptr<rnwgpu::VideoFrame>>::fromJSI(
180
- runtime, prop, false);
178
+ if (prop.isObject()) {
179
+ auto brand = prop.getObject(runtime).getProperty(runtime, "__brand");
180
+ if (brand.isString() && brand.getString(runtime).utf8(runtime) ==
181
+ GPUTexture::CLASS_NAME) {
182
+ result->sourceTexture = GPUTexture::fromValue(runtime, prop);
183
+ } else {
184
+ result->source =
185
+ JSIConverter<std::shared_ptr<rnwgpu::VideoFrame>>::fromJSI(
186
+ runtime, prop, false);
187
+ }
181
188
  }
182
189
  }
183
190
  if (value.hasProperty(runtime, "label")) {
@@ -19,7 +19,7 @@ namespace jsi = facebook::jsi;
19
19
  namespace rnwgpu {
20
20
 
21
21
  // GPUCopyExternalImageSourceInfo. The source is either an ImageBitmap (the
22
- // spec's CPU upload) or, as a React Native extension, a NativeVideoFrame that
22
+ // spec's CPU upload) or, as a React Native extension, a NativeBuffer that
23
23
  // is rendered into the destination through an external texture (see
24
24
  // VideoFrameBlit). `rotation` and `mirrored` only apply to a native frame and
25
25
  // mirror the extension of GPUExternalTextureDescriptor.