react-native-webgpu 0.21.4 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/README.md +15 -2
  2. package/android/CMakeLists.txt +10 -8
  3. package/android/build.gradle +111 -20
  4. package/android/cpp/AndroidAnimationFrameSource.h +136 -0
  5. package/android/cpp/AndroidPlatformContext.h +55 -0
  6. package/android/cpp/cpp-adapter.cpp +148 -2
  7. package/android/src/main/java/com/webgpu/WebGPUModule.java +21 -4
  8. package/android/src/main/java/com/webgpu/WebGPUNativeBufferView.java +370 -0
  9. package/android/src/main/java/com/webgpu/WebGPUNativeBufferViewManager.java +56 -0
  10. package/android/src/main/java/com/webgpu/WebGPUTextureView.java +21 -0
  11. package/android/src/main/java/com/webgpu/WebGPUVideoPlayer.java +1 -1
  12. package/android/src/main/java/com/webgpu/WebGPUView.java +19 -3
  13. package/android/src/main/java/com/webgpu/WebGPUViewManager.java +10 -0
  14. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +2 -2
  15. package/android/src/newarch/com/webgpu/WebGPUNativeBufferViewManagerSpec.java +24 -0
  16. package/apple/AppleAnimationFrameSource.mm +208 -0
  17. package/apple/ApplePlatformContext.h +6 -0
  18. package/apple/ApplePlatformContext.mm +73 -0
  19. package/apple/MetalLayerColorSpace.h +17 -0
  20. package/apple/MetalLayerColorSpace.mm +36 -57
  21. package/apple/MetalView.h +4 -0
  22. package/apple/MetalView.mm +14 -0
  23. package/apple/NativeBufferFenceWait.mm +88 -0
  24. package/apple/RNWGUIKit.h +4 -1
  25. package/apple/WebGPUModule.mm +26 -1
  26. package/apple/WebGPUNativeBufferView.h +15 -0
  27. package/apple/WebGPUNativeBufferView.mm +200 -0
  28. package/apple/WebGPUView.h +2 -1
  29. package/apple/WebGPUView.mm +6 -0
  30. package/binaries.json +156 -0
  31. package/cpp/jsi/NativeObject.h +12 -5
  32. package/cpp/rnwgpu/AnimationFrames.cpp +576 -0
  33. package/cpp/rnwgpu/AnimationFrames.h +233 -0
  34. package/cpp/rnwgpu/CanvasColorSpace.h +16 -0
  35. package/cpp/rnwgpu/NativeBufferViewRegistry.cpp +396 -0
  36. package/cpp/rnwgpu/NativeBufferViewRegistry.h +163 -0
  37. package/cpp/rnwgpu/PlatformContext.h +20 -0
  38. package/cpp/rnwgpu/RNWebGPUManager.cpp +17 -4
  39. package/cpp/rnwgpu/SurfaceRegistry.h +26 -30
  40. package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
  41. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +41 -3
  42. package/cpp/rnwgpu/api/GPUCanvasContext.h +7 -3
  43. package/cpp/rnwgpu/api/GPUDevice.cpp +16 -131
  44. package/cpp/rnwgpu/api/GPUDevice.h +0 -18
  45. package/cpp/rnwgpu/api/GPUExternalTexture.cpp +35 -3
  46. package/cpp/rnwgpu/api/GPUExternalTexture.h +4 -4
  47. package/cpp/rnwgpu/api/GPUQueue.cpp +1 -1
  48. package/cpp/rnwgpu/api/GPUQueue.h +1 -1
  49. package/cpp/rnwgpu/api/GPUSharedFence.h +1 -0
  50. package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +11 -5
  51. package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +21 -2
  52. package/cpp/rnwgpu/api/GPUTexture.h +18 -0
  53. package/cpp/rnwgpu/api/NativeFrameFeatures.h +1 -1
  54. package/cpp/rnwgpu/api/NativeFrameImport.cpp +20 -17
  55. package/cpp/rnwgpu/api/NativeFrameImport.h +31 -20
  56. package/cpp/rnwgpu/api/RNWebGPU.h +201 -3
  57. package/cpp/rnwgpu/api/VideoFrame.h +5 -4
  58. package/cpp/rnwgpu/api/VideoFrameBlit.h +2 -2
  59. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -14
  60. package/cpp/rnwgpu/api/descriptors/GPUImageCopyExternalImage.h +1 -1
  61. package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +17 -9
  62. package/cpp/rnwgpu/async/RuntimeContext.cpp +21 -4
  63. package/cpp/rnwgpu/async/RuntimeContext.h +8 -0
  64. package/lib/commonjs/Canvas.js +33 -1
  65. package/lib/commonjs/Canvas.js.map +1 -1
  66. package/lib/commonjs/NativeBufferView.js +131 -0
  67. package/lib/commonjs/NativeBufferView.js.map +1 -0
  68. package/lib/commonjs/NativeBufferViewNativeComponent.js +10 -0
  69. package/lib/commonjs/NativeBufferViewNativeComponent.js.map +1 -0
  70. package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
  71. package/lib/commonjs/WebGPUViewNativeComponent.web.js +2 -0
  72. package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
  73. package/lib/commonjs/animationFrame.js +91 -0
  74. package/lib/commonjs/animationFrame.js.map +1 -0
  75. package/lib/commonjs/index.js +16 -1
  76. package/lib/commonjs/index.js.map +1 -1
  77. package/lib/commonjs/main/index.js +12 -0
  78. package/lib/commonjs/main/index.js.map +1 -1
  79. package/lib/commonjs/mock.js +30 -1
  80. package/lib/commonjs/mock.js.map +1 -1
  81. package/lib/commonjs/video.js +14 -4
  82. package/lib/commonjs/video.js.map +1 -1
  83. package/lib/module/Canvas.js +33 -1
  84. package/lib/module/Canvas.js.map +1 -1
  85. package/lib/module/NativeBufferView.js +122 -0
  86. package/lib/module/NativeBufferView.js.map +1 -0
  87. package/lib/module/NativeBufferViewNativeComponent.js +4 -0
  88. package/lib/module/NativeBufferViewNativeComponent.js.map +1 -0
  89. package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
  90. package/lib/module/WebGPUViewNativeComponent.web.js +2 -0
  91. package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
  92. package/lib/module/animationFrame.js +83 -0
  93. package/lib/module/animationFrame.js.map +1 -0
  94. package/lib/module/index.js +1 -0
  95. package/lib/module/index.js.map +1 -1
  96. package/lib/module/main/index.js +1 -0
  97. package/lib/module/main/index.js.map +1 -1
  98. package/lib/module/mock.js +21 -0
  99. package/lib/module/mock.js.map +1 -1
  100. package/lib/module/video.js +11 -2
  101. package/lib/module/video.js.map +1 -1
  102. package/lib/typescript/lib/commonjs/Canvas.d.ts +3 -1
  103. package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
  104. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts +48 -0
  105. package/lib/typescript/lib/commonjs/NativeBufferView.d.ts.map +1 -0
  106. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts +5 -0
  107. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.d.ts.map +1 -0
  108. package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  109. package/lib/typescript/lib/commonjs/animationFrame.d.ts +54 -0
  110. package/lib/typescript/lib/commonjs/animationFrame.d.ts.map +1 -0
  111. package/lib/typescript/lib/commonjs/index.d.ts +2 -0
  112. package/lib/typescript/lib/commonjs/mock.d.ts +12 -0
  113. package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
  114. package/lib/typescript/lib/commonjs/video.d.ts +3 -2
  115. package/lib/typescript/lib/commonjs/video.d.ts.map +1 -1
  116. package/lib/typescript/lib/module/Canvas.d.ts +3 -1
  117. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  118. package/lib/typescript/lib/module/NativeBufferView.d.ts +14 -0
  119. package/lib/typescript/lib/module/NativeBufferView.d.ts.map +1 -0
  120. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts +3 -0
  121. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.d.ts.map +1 -0
  122. package/lib/typescript/lib/module/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  123. package/lib/typescript/lib/module/animationFrame.d.ts +3 -0
  124. package/lib/typescript/lib/module/animationFrame.d.ts.map +1 -0
  125. package/lib/typescript/lib/module/index.d.ts +1 -0
  126. package/lib/typescript/lib/module/main/index.d.ts +1 -0
  127. package/lib/typescript/lib/module/mock.d.ts +14 -0
  128. package/lib/typescript/lib/module/mock.d.ts.map +1 -1
  129. package/lib/typescript/lib/module/video.d.ts +3 -2
  130. package/lib/typescript/lib/module/video.d.ts.map +1 -1
  131. package/lib/typescript/src/Canvas.d.ts +17 -1
  132. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  133. package/lib/typescript/src/NativeBufferView.d.ts +99 -0
  134. package/lib/typescript/src/NativeBufferView.d.ts.map +1 -0
  135. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts +13 -0
  136. package/lib/typescript/src/NativeBufferViewNativeComponent.d.ts.map +1 -0
  137. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +2 -0
  138. package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
  139. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +2 -0
  140. package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
  141. package/lib/typescript/src/animationFrame.d.ts +59 -0
  142. package/lib/typescript/src/animationFrame.d.ts.map +1 -0
  143. package/lib/typescript/src/index.d.ts +18 -8
  144. package/lib/typescript/src/index.d.ts.map +1 -1
  145. package/lib/typescript/src/main/index.d.ts +1 -0
  146. package/lib/typescript/src/main/index.d.ts.map +1 -1
  147. package/lib/typescript/src/mock.d.ts +11 -0
  148. package/lib/typescript/src/mock.d.ts.map +1 -1
  149. package/lib/typescript/src/types.d.ts +32 -31
  150. package/lib/typescript/src/types.d.ts.map +1 -1
  151. package/lib/typescript/src/video.d.ts +12 -4
  152. package/lib/typescript/src/video.d.ts.map +1 -1
  153. package/package.json +8 -2
  154. package/react-native-webgpu.podspec +1 -1
  155. package/src/Canvas.tsx +67 -1
  156. package/src/NativeBufferView.tsx +199 -0
  157. package/src/NativeBufferViewNativeComponent.ts +20 -0
  158. package/src/WebGPUViewNativeComponent.ts +4 -0
  159. package/src/WebGPUViewNativeComponent.web.ts +6 -0
  160. package/src/animationFrame.ts +82 -0
  161. package/src/index.tsx +45 -39
  162. package/src/main/index.tsx +1 -0
  163. package/src/mock.ts +35 -0
  164. package/src/types.ts +61 -87
  165. package/src/video.ts +20 -7
  166. package/cpp/rnwgpu/api/GPUNativeBufferExternalTexture.h +0 -60
  167. package/cpp/rnwgpu/api/GPUNativeBufferTexture.h +0 -52
  168. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.cpp +0 -144
  169. package/cpp/rnwgpu/api/GPUNativeBufferWrapper.h +0 -143
  170. package/cpp/rnwgpu/api/descriptors/GPUNativeBufferTextureDescriptor.h +0 -55
@@ -37,9 +37,6 @@
37
37
  #include "GPUError.h"
38
38
  #include "GPUExternalTexture.h"
39
39
  #include "GPUExternalTextureDescriptor.h"
40
- #include "GPUNativeBufferExternalTexture.h"
41
- #include "GPUNativeBufferTexture.h"
42
- #include "GPUNativeBufferTextureDescriptor.h"
43
40
  #include "GPUPipelineLayout.h"
44
41
  #include "GPUPipelineLayoutDescriptor.h"
45
42
  #include "GPUQuerySet.h"
@@ -137,17 +134,6 @@ public:
137
134
  std::shared_ptr<GPUSharedTextureMemoryDescriptor> descriptor);
138
135
  std::shared_ptr<GPUSharedFence>
139
136
  importSharedFence(std::shared_ptr<GPUSharedFenceDescriptor> descriptor);
140
- // Import a native buffer once and bracket each use with beginAccess /
141
- // endAccess: the counterparts of createTexture / createExternalTexture in
142
- // GPUAndroidHardwareBufferUtil of Dawn's Android layer.
143
- std::shared_ptr<GPUNativeBufferTexture> createNativeBufferTexture(
144
- std::shared_ptr<VideoFrame> source,
145
- std::optional<std::shared_ptr<GPUNativeBufferTextureDescriptor>>
146
- descriptor);
147
- std::shared_ptr<GPUNativeBufferExternalTexture>
148
- createNativeBufferExternalTexture(
149
- std::shared_ptr<VideoFrame> source,
150
- std::optional<std::shared_ptr<GPUExternalTextureDescriptor>> descriptor);
151
137
  std::shared_ptr<GPUBindGroupLayout> createBindGroupLayout(
152
138
  std::shared_ptr<GPUBindGroupLayoutDescriptor> descriptor);
153
139
  std::shared_ptr<GPUPipelineLayout>
@@ -208,10 +194,6 @@ public:
208
194
  &GPUDevice::importSharedTextureMemory);
209
195
  installMethod(runtime, prototype, "importSharedFence",
210
196
  &GPUDevice::importSharedFence);
211
- installMethod(runtime, prototype, "createNativeBufferTexture",
212
- &GPUDevice::createNativeBufferTexture);
213
- installMethod(runtime, prototype, "createNativeBufferExternalTexture",
214
- &GPUDevice::createNativeBufferExternalTexture);
215
197
  installMethod(runtime, prototype, "createBindGroupLayout",
216
198
  &GPUDevice::createBindGroupLayout);
217
199
  installMethod(runtime, prototype, "createPipelineLayout",
@@ -7,6 +7,7 @@
7
7
  #include <utility>
8
8
  #include <vector>
9
9
 
10
+ #include "DestroyedError.h"
10
11
  #include "GPUExternalTextureDescriptor.h"
11
12
  #include "NativeFrameImport.h"
12
13
 
@@ -16,10 +17,41 @@ std::shared_ptr<GPUExternalTexture> GPUExternalTexture::Create(
16
17
  wgpu::Device device,
17
18
  std::shared_ptr<GPUExternalTextureDescriptor> descriptor) {
18
19
  static constexpr const char *kCaller = "GPUDevice::importExternalTexture";
20
+ if (descriptor && descriptor->sourceTexture) {
21
+ // A texture of a GPUSharedTextureMemory: the caller imported the buffer
22
+ // once and brackets each use with the memory's beginAccess() / endAccess(),
23
+ // so the external texture only samples it, and can be kept for every
24
+ // access of the buffer.
25
+ const auto &source = descriptor->sourceTexture;
26
+ const auto &origin = source->sharedTextureOrigin();
27
+ if (!origin) {
28
+ throw std::runtime_error(
29
+ "GPUDevice::importExternalTexture(): descriptor.source is a "
30
+ "GPUTexture that was not created by "
31
+ "GPUSharedTextureMemory.createTexture()");
32
+ }
33
+ // Copied: destroy() on the memory clears the origin's reference.
34
+ wgpu::SharedTextureMemory memory = origin->memory;
35
+ if (!memory) {
36
+ // Like an access to a destroyed memory: a validation error, then the
37
+ // throw.
38
+ reportDestroyedUse(device, "GPUSharedTextureMemory", origin->label,
39
+ "importExternalTexture");
40
+ throw std::runtime_error(
41
+ "GPUDevice::importExternalTexture(): descriptor.source is a "
42
+ "texture of a GPUSharedTextureMemory that was destroyed");
43
+ }
44
+ std::string label = descriptor->label.value_or("external-texture");
45
+ auto external = native_frame::createExternalTexture(
46
+ device, memory, source->get(), origin->colorSpace, *descriptor, label,
47
+ kCaller);
48
+ return std::make_shared<GPUExternalTexture>(
49
+ std::move(external), nullptr, nullptr, nullptr, std::move(label));
50
+ }
19
51
  if (!descriptor || !descriptor->source) {
20
52
  throw std::runtime_error(
21
- "GPUDevice::importExternalTexture(): descriptor.source "
22
- "(NativeVideoFrame) is required");
53
+ "GPUDevice::importExternalTexture(): descriptor.source (a "
54
+ "NativeBuffer, or a texture of a GPUSharedTextureMemory) is required");
23
55
  }
24
56
  const auto &frame = descriptor->source->handle();
25
57
  std::string label = descriptor->label.value_or("external-texture");
@@ -51,7 +83,7 @@ std::shared_ptr<GPUExternalTexture> GPUExternalTexture::Create(
51
83
  wgpu::ExternalTexture external;
52
84
  try {
53
85
  external = native_frame::createExternalTexture(
54
- device, memory, texture, frame, *descriptor, label, kCaller);
86
+ device, memory, texture, frame.colorSpace, *descriptor, label, kCaller);
55
87
  } catch (...) {
56
88
  native_frame::endAccess(memory, texture);
57
89
  throw;
@@ -30,10 +30,10 @@ public:
30
30
 
31
31
  // `memory` and `texture` are the import this external texture owns:
32
32
  // destroy() ends their access and releases them, so the producer (e.g. an
33
- // AVPlayer) can reclaim the surface. A GPUNativeBufferExternalTexture
34
- // passes null for both: it owns the import itself and brackets each use with
35
- // its own beginAccess() / endAccess(), so destroy() here only releases the
36
- // external texture.
33
+ // AVPlayer) can reclaim the surface. An external texture over a texture of
34
+ // a GPUSharedTextureMemory passes null for both: the caller owns the import
35
+ // and brackets each use with the memory's beginAccess() / endAccess(), so
36
+ // destroy() here only releases the external texture.
37
37
  GPUExternalTexture(wgpu::ExternalTexture instance,
38
38
  wgpu::SharedTextureMemory memory, wgpu::Texture texture,
39
39
  std::shared_ptr<VideoFrame> source, std::string label)
@@ -190,7 +190,7 @@ void GPUQueue::copyExternalImageToTexture(
190
190
  }
191
191
  if (!source->source) {
192
192
  throw std::runtime_error("copyExternalImageToTexture: the source must be "
193
- "an ImageBitmap or a NativeVideoFrame");
193
+ "an ImageBitmap or a NativeBuffer");
194
194
  }
195
195
  uint32_t bytesPerPixel =
196
196
  source->source->getSize() /
@@ -55,7 +55,7 @@ public:
55
55
  std::shared_ptr<GPUImageDataLayout> dataLayout,
56
56
  std::shared_ptr<GPUExtent3D> size);
57
57
  // The source is an ImageBitmap (uploaded from the CPU) or, as a React
58
- // Native extension, a NativeVideoFrame rendered into the destination through
58
+ // Native extension, a NativeBuffer rendered into the destination through
59
59
  // an external texture (see VideoFrameBlit).
60
60
  void copyExternalImageToTexture(
61
61
  std::shared_ptr<GPUImageCopyExternalImage> source,
@@ -56,6 +56,7 @@ public:
56
56
  }
57
57
 
58
58
  inline wgpu::SharedFence get() { return _instance; }
59
+ inline const wgpu::Device &device() const { return _device; }
59
60
 
60
61
  // Reports a use of the destroyed fence the way Dawn reports an invalid
61
62
  // object: a validation error, caught by error scopes or else delivered to
@@ -27,7 +27,11 @@ void GPUSharedTextureMemory::destroy() {
27
27
  _device.InjectError(wgpu::ErrorType::Validation, message.c_str());
28
28
  _accessed.clear();
29
29
  }
30
+ // The textures created from the memory share the origin: without this, each
31
+ // one still referenced from JS would keep the import alive.
32
+ _origin->memory = nullptr;
30
33
  _instance = nullptr;
34
+ _source = nullptr;
31
35
  }
32
36
 
33
37
  void GPUSharedTextureMemory::injectDestroyedError(const char *method) {
@@ -61,8 +65,9 @@ std::shared_ptr<GPUTexture> GPUSharedTextureMemory::createTexture(
61
65
  std::move(label), false);
62
66
  }
63
67
  if (!descriptor.has_value() || descriptor.value() == nullptr) {
64
- auto texture = _instance.CreateTexture();
65
- return std::make_shared<GPUTexture>(texture, "");
68
+ auto texture = std::make_shared<GPUTexture>(_instance.CreateTexture(), "");
69
+ texture->setSharedTextureOrigin(_origin);
70
+ return texture;
66
71
  }
67
72
 
68
73
  wgpu::TextureDescriptor desc{};
@@ -72,9 +77,10 @@ std::shared_ptr<GPUTexture> GPUSharedTextureMemory::createTexture(
72
77
  "GPUSharedTextureMemory::createTexture(): Error with "
73
78
  "GPUTextureDescriptor");
74
79
  }
75
- auto texture = _instance.CreateTexture(&desc);
76
- return std::make_shared<GPUTexture>(texture,
77
- descriptor.value()->label.value_or(""));
80
+ auto texture = std::make_shared<GPUTexture>(
81
+ _instance.CreateTexture(&desc), descriptor.value()->label.value_or(""));
82
+ texture->setSharedTextureOrigin(_origin);
83
+ return texture;
78
84
  }
79
85
 
80
86
  void GPUSharedTextureMemory::beginAccess(
@@ -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