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
@@ -6,6 +6,7 @@ import android.graphics.SurfaceTexture;
6
6
  import android.os.Build;
7
7
  import android.view.Surface;
8
8
  import android.view.TextureView;
9
+ import android.view.View;
9
10
  import androidx.annotation.NonNull;
10
11
 
11
12
  @SuppressLint("ViewConstructor")
@@ -55,6 +56,26 @@ public class WebGPUTextureView extends TextureView implements TextureView.Surfac
55
56
  return true;
56
57
  }
57
58
 
59
+ /**
60
+ * A frame was just presented from the main thread: have this draw show it.
61
+ *
62
+ * TextureView hears of a new frame through its SurfaceTexture's
63
+ * frame-available listener, a plain message on the main looper. A frame
64
+ * presented on the main thread during the Choreographer's animation
65
+ * callbacks (the Reanimated UI runtime's requestAnimationFrame) is followed
66
+ * by that frame's draw before the message runs: the draw shows nothing new,
67
+ * the message schedules the next draw, and the canvas updates every other
68
+ * frame (60 fps on a 120 Hz display). What the listener does is private
69
+ * (updateLayerAndInvalidate()); onVisibilityChanged(VISIBLE) does the same
70
+ * for a visible view, so this calls it before the draw. Main thread.
71
+ */
72
+ void latchPresentedFrame() {
73
+ if (getSurfaceTexture() == null || !isShown()) {
74
+ return;
75
+ }
76
+ super.onVisibilityChanged(this, View.VISIBLE);
77
+ }
78
+
58
79
  @Override
59
80
  public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surfaceTexture) {
60
81
  // No implementation needed
@@ -26,7 +26,7 @@ import java.util.Set;
26
26
  * buffers the GPU can sample ({@link ImageFormat#PRIVATE} with
27
27
  * {@link HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE}). Each frame is handed to C++
28
28
  * (AndroidVideoPlayer) as an {@link Image} that stays acquired until the
29
- * NativeVideoFrame is released, which hands the buffer back to the decoder.
29
+ * NativeBuffer is released, which hands the buffer back to the decoder.
30
30
  *
31
31
  * The frames keep the decoder's native YUV layout, so they are sampled through
32
32
  * GPUDevice.importExternalTexture. MediaPlayer owns the audio, the clock and
@@ -112,7 +112,7 @@ public class WebGPUVideoPlayer {
112
112
  mp.setDataSource(context, uri);
113
113
  } catch (IOException | IllegalArgumentException | SecurityException e) {
114
114
  mp.release();
115
- throw new RuntimeException("createVideoPlayer: cannot open " + source, e);
115
+ throw new RuntimeException("createTestVideoPlayer: cannot open " + source, e);
116
116
  }
117
117
  mp.setLooping(loop);
118
118
  mp.prepareAsync();
@@ -194,7 +194,8 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
194
194
  if (votesFrameRate) {
195
195
  mFrameRateVote.attach(surface);
196
196
  }
197
- onSurfaceCreate(surface, mContextId, width, height, votesFrameRate);
197
+ boolean latchOnMainThread = mKind == BackingViewKind.TEXTURE_VIEW;
198
+ onSurfaceCreate(surface, mContextId, width, height, votesFrameRate, latchOnMainThread);
198
199
  }
199
200
 
200
201
  @Override
@@ -221,6 +222,18 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
221
222
  mFrameRateVote.onAnimating();
222
223
  }
223
224
 
225
+ /**
226
+ * Called from native (cpp-adapter.cpp) right after a frame is presented to
227
+ * a TextureView from the main thread, so that the draw of this frame shows
228
+ * it (see WebGPUTextureView.latchPresentedFrame).
229
+ */
230
+ @DoNotStrip
231
+ void onFramePresentedOnMainThread() {
232
+ if (mView instanceof WebGPUTextureView) {
233
+ ((WebGPUTextureView) mView).latchPresentedFrame();
234
+ }
235
+ }
236
+
224
237
  @Override
225
238
  public int getContextId() {
226
239
  return mContextId;
@@ -236,14 +249,17 @@ public class WebGPUView extends ReactViewGroup implements WebGPUAPI {
236
249
  }
237
250
 
238
251
  // reportAnimation asks native to call onAnimating() when frames start being
239
- // presented to this surface back to back.
252
+ // presented to this surface back to back; latchOnMainThread, to call
253
+ // onFramePresentedOnMainThread() after each frame presented from the main
254
+ // thread.
240
255
  @DoNotStrip
241
256
  private native void onSurfaceCreate(
242
257
  Surface surface,
243
258
  int contextId,
244
259
  float width,
245
260
  float height,
246
- boolean reportAnimation
261
+ boolean reportAnimation,
262
+ boolean latchOnMainThread
247
263
  );
248
264
 
249
265
  @DoNotStrip
@@ -9,7 +9,7 @@ import com.facebook.react.module.model.ReactModuleInfo;
9
9
  import com.facebook.react.module.model.ReactModuleInfoProvider;
10
10
  import com.facebook.react.uimanager.ViewManager;
11
11
 
12
- import java.util.Collections;
12
+ import java.util.Arrays;
13
13
  import java.util.HashMap;
14
14
  import java.util.List;
15
15
  import java.util.Map;
@@ -45,6 +45,6 @@ public class WebGPUViewPackage extends TurboReactPackage {
45
45
 
46
46
  @Override
47
47
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
48
- return Collections.singletonList(new WebGPUViewManager());
48
+ return Arrays.<ViewManager>asList(new WebGPUViewManager(), new WebGPUNativeBufferViewManager());
49
49
  }
50
50
  }
@@ -0,0 +1,24 @@
1
+ package com.webgpu;
2
+
3
+ import android.view.View;
4
+
5
+ import androidx.annotation.Nullable;
6
+
7
+ import com.facebook.react.uimanager.SimpleViewManager;
8
+ import com.facebook.react.uimanager.ViewManagerDelegate;
9
+ import com.facebook.react.viewmanagers.WebGPUNativeBufferViewManagerDelegate;
10
+ import com.facebook.react.viewmanagers.WebGPUNativeBufferViewManagerInterface;
11
+
12
+ public abstract class WebGPUNativeBufferViewManagerSpec<T extends View> extends SimpleViewManager<T> implements WebGPUNativeBufferViewManagerInterface<T> {
13
+ private final ViewManagerDelegate<T> mDelegate;
14
+
15
+ public WebGPUNativeBufferViewManagerSpec() {
16
+ mDelegate = new WebGPUNativeBufferViewManagerDelegate(this);
17
+ }
18
+
19
+ @Nullable
20
+ @Override
21
+ protected ViewManagerDelegate<T> getDelegate() {
22
+ return mDelegate;
23
+ }
24
+ }
@@ -0,0 +1,208 @@
1
+ #include "AnimationFrames.h"
2
+
3
+ #import "RNWGUIKit.h"
4
+ #import <QuartzCore/QuartzCore.h>
5
+ #import <React/RCTUtils.h>
6
+
7
+ #include <atomic>
8
+ #include <memory>
9
+
10
+ namespace rnwgpu {
11
+ class AppleAnimationFrameSource;
12
+ std::shared_ptr<AnimationFrameSource> makeAppleAnimationFrameSource();
13
+ } // namespace rnwgpu
14
+
15
+ // The display link's target. A CADisplayLink retains its target, and the
16
+ // source outlives it anyway: the target borrows the source.
17
+ @interface RNWGAnimationFrameTarget : NSObject
18
+ - (instancetype)initWithSource:(rnwgpu::AppleAnimationFrameSource *)source;
19
+ - (void)onFrame:(CADisplayLink *)link;
20
+ @end
21
+
22
+ namespace rnwgpu {
23
+
24
+ #if !TARGET_OS_OSX
25
+
26
+ /**
27
+ * The display's frames on iOS: a CADisplayLink on a run loop thread of its
28
+ * own, so that a busy main thread never delays them, asking for the highest
29
+ * rate the display has. React Native's own display link sets no frame rate
30
+ * range, which keeps it at 60 Hz on a ProMotion iPhone. (The app must still
31
+ * opt in to more than 60 Hz on iPhone with CADisableMinimumFrameDurationOnPhone
32
+ * in its Info.plist.)
33
+ *
34
+ * A request is delivered at the next frame of the link. The link keeps running
35
+ * between requests, and only pauses after kIdleFramesBeforePause frames
36
+ * without one: pausing it at every frame and unpausing it at the next request
37
+ * reads to the system as a link that keeps stopping, which it does not run at
38
+ * 120 Hz. The frame time is the link's timestamp, moved from its clock to
39
+ * std::chrono::steady_clock. The thread lives as long as the process.
40
+ */
41
+ class AppleAnimationFrameSource : public AnimationFrameSource {
42
+ public:
43
+ static std::shared_ptr<AppleAnimationFrameSource> shared() {
44
+ static std::shared_ptr<AppleAnimationFrameSource> source = create();
45
+ return source;
46
+ }
47
+
48
+ void requestFrame() override {
49
+ _wanted.store(true);
50
+ // Paired with onFrame(), which stores _paused before it reads _wanted:
51
+ // either it sees this request, or this sees the link paused.
52
+ if (_paused.load()) {
53
+ unpause();
54
+ }
55
+ }
56
+
57
+ // The link's thread.
58
+ void onFrame(CADisplayLink *link) {
59
+ if (!_wanted.exchange(false)) {
60
+ if (++_idleFrames >= kIdleFramesBeforePause) {
61
+ link.paused = YES;
62
+ _paused.store(true);
63
+ if (_wanted.load()) {
64
+ unpause();
65
+ }
66
+ }
67
+ return;
68
+ }
69
+ _idleFrames = 0;
70
+ const CFTimeInterval age = CACurrentMediaTime() - link.timestamp;
71
+ const int64_t ageNanos = age > 0 ? static_cast<int64_t>(age * 1e9) : 0;
72
+ deliverFrame(AnimationFrameDispatcher::nowNanos() - ageNanos);
73
+ }
74
+
75
+ private:
76
+ AppleAnimationFrameSource() = default;
77
+
78
+ static std::shared_ptr<AppleAnimationFrameSource> create() {
79
+ auto source = std::shared_ptr<AppleAnimationFrameSource>(
80
+ new AppleAnimationFrameSource());
81
+ auto *raw = source.get();
82
+ dispatch_semaphore_t ready = dispatch_semaphore_create(0);
83
+ NSThread *thread = [[NSThread alloc] initWithBlock:^{
84
+ RNWGAnimationFrameTarget *target =
85
+ [[RNWGAnimationFrameTarget alloc] initWithSource:raw];
86
+ CADisplayLink *link =
87
+ [CADisplayLink displayLinkWithTarget:target
88
+ selector:@selector(onFrame:)];
89
+ if (@available(iOS 15.0, tvOS 15.0, *)) {
90
+ // Up to what the display can do: the system lowers it as it sees fit
91
+ // (Low Power Mode, a display that has no more).
92
+ link.preferredFrameRateRange = CAFrameRateRangeMake(60, 120, 120);
93
+ }
94
+ link.paused = YES;
95
+ [link addToRunLoop:[NSRunLoop currentRunLoop]
96
+ forMode:NSRunLoopCommonModes];
97
+ raw->_link = link;
98
+ raw->_runLoop = CFRunLoopGetCurrent();
99
+ CFRetain(raw->_runLoop);
100
+ dispatch_semaphore_signal(ready);
101
+ // The display link is a source of the run loop: it keeps running.
102
+ for (;;) {
103
+ @autoreleasepool {
104
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
105
+ beforeDate:[NSDate distantFuture]];
106
+ }
107
+ }
108
+ }];
109
+ thread.name = @"RNWebGPU Frames";
110
+ thread.qualityOfService = NSQualityOfServiceUserInteractive;
111
+ [thread start];
112
+ dispatch_semaphore_wait(ready, DISPATCH_TIME_FOREVER);
113
+ return source;
114
+ }
115
+
116
+ // Any thread: the link runs again from its next frame.
117
+ void unpause() {
118
+ CFRunLoopPerformBlock(_runLoop, kCFRunLoopCommonModes, ^{
119
+ if (_paused.exchange(false)) {
120
+ _link.paused = NO;
121
+ }
122
+ });
123
+ CFRunLoopWakeUp(_runLoop);
124
+ }
125
+
126
+ static constexpr int kIdleFramesBeforePause = 8;
127
+
128
+ CADisplayLink *_link = nil;
129
+ CFRunLoopRef _runLoop = nullptr;
130
+ std::atomic<bool> _wanted{false};
131
+ std::atomic<bool> _paused{true};
132
+ // The link's thread only.
133
+ int _idleFrames = 0;
134
+ };
135
+
136
+ // Whether the app is in the foreground and active (see
137
+ // AnimationFrameDispatcher::setForeground): false from the moment it starts to
138
+ // resign active (a lock, a swipe home, an incoming call; the inactive and
139
+ // background states of iOS) until it is active again. The display link does
140
+ // not stop on its own when the app goes to the background: it runs on until
141
+ // the system suspends the app, a second or so of frames whose GPU work iOS
142
+ // refuses.
143
+ static void observeApplicationState() {
144
+ static dispatch_once_t once;
145
+ dispatch_once(&once, ^{
146
+ NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
147
+ [center addObserverForName:UIApplicationWillResignActiveNotification
148
+ object:nil
149
+ queue:nil
150
+ usingBlock:^(NSNotification *) {
151
+ AnimationFrameDispatcher::shared().setForeground(false);
152
+ }];
153
+ [center addObserverForName:UIApplicationDidBecomeActiveNotification
154
+ object:nil
155
+ queue:nil
156
+ usingBlock:^(NSNotification *) {
157
+ AnimationFrameDispatcher::shared().setForeground(true);
158
+ }];
159
+ // The state now, which is the main thread's to read. An app extension has
160
+ // no application: it stays in the foreground.
161
+ dispatch_async(dispatch_get_main_queue(), ^{
162
+ UIApplication *application = RCTSharedApplication();
163
+ if (application != nil &&
164
+ application.applicationState != UIApplicationStateActive) {
165
+ AnimationFrameDispatcher::shared().setForeground(false);
166
+ }
167
+ });
168
+ });
169
+ }
170
+
171
+ std::shared_ptr<AnimationFrameSource> makeAppleAnimationFrameSource() {
172
+ observeApplicationState();
173
+ return AppleAnimationFrameSource::shared();
174
+ }
175
+
176
+ #else
177
+
178
+ // macOS: none for now. Each runtime keeps its own requestAnimationFrame.
179
+ class AppleAnimationFrameSource : public AnimationFrameSource {
180
+ public:
181
+ void requestFrame() override {}
182
+ void onFrame(CADisplayLink *) {}
183
+ };
184
+
185
+ std::shared_ptr<AnimationFrameSource> makeAppleAnimationFrameSource() {
186
+ return nullptr;
187
+ }
188
+
189
+ #endif
190
+
191
+ } // namespace rnwgpu
192
+
193
+ @implementation RNWGAnimationFrameTarget {
194
+ rnwgpu::AppleAnimationFrameSource *_source;
195
+ }
196
+
197
+ - (instancetype)initWithSource:(rnwgpu::AppleAnimationFrameSource *)source {
198
+ if ((self = [super init])) {
199
+ _source = source;
200
+ }
201
+ return self;
202
+ }
203
+
204
+ - (void)onFrame:(CADisplayLink *)link {
205
+ _source->onFrame(link);
206
+ }
207
+
208
+ @end
@@ -45,8 +45,13 @@ public:
45
45
  VideoFrameHandle createTestVideoFrame(uint32_t width, uint32_t height,
46
46
  VideoPixelFormat format) override;
47
47
 
48
+ VideoFrameHandle createNativeBuffer(uint32_t width, uint32_t height,
49
+ wgpu::TextureFormat format,
50
+ bool displayP3) override;
51
+
48
52
  std::unique_ptr<IVideoPlayer>
49
- createVideoPlayer(const std::string &path, VideoPixelFormat format) override;
53
+ createTestVideoPlayer(const std::string &path,
54
+ VideoPixelFormat format) override;
50
55
 
51
56
  std::string writeTestVideoFile() override;
52
57
 
@@ -54,6 +59,8 @@ public:
54
59
 
55
60
  std::function<void(void *)> retainNativeWindow(void *window) override;
56
61
 
62
+ std::shared_ptr<AnimationFrameSource> makeAnimationFrameSource() override;
63
+
57
64
  private:
58
65
  ViewLookup _viewLookup;
59
66
  BlobManagerLookup _blobManagerLookup;
@@ -542,11 +542,19 @@ VideoFrameHandle ApplePlatformContext::loadVideoFrame(const std::string &path) {
542
542
  }
543
543
 
544
544
  std::unique_ptr<IVideoPlayer>
545
- ApplePlatformContext::createVideoPlayer(const std::string &path,
546
- VideoPixelFormat format) {
545
+ ApplePlatformContext::createTestVideoPlayer(const std::string &path,
546
+ VideoPixelFormat format) {
547
547
  return createAppleVideoPlayer(path, format);
548
548
  }
549
549
 
550
+ // AppleAnimationFrameSource.mm
551
+ std::shared_ptr<AnimationFrameSource> makeAppleAnimationFrameSource();
552
+
553
+ std::shared_ptr<AnimationFrameSource>
554
+ ApplePlatformContext::makeAnimationFrameSource() {
555
+ return makeAppleAnimationFrameSource();
556
+ }
557
+
550
558
  std::string ApplePlatformContext::writeTestVideoFile() {
551
559
  return writeAppleTestVideoFile();
552
560
  }
@@ -685,4 +693,69 @@ ApplePlatformContext::createTestVideoFrame(uint32_t width, uint32_t height,
685
693
  return handle;
686
694
  }
687
695
 
696
+ // An IOSurface-backed pixel buffer that Dawn imports as a render target and
697
+ // Core Animation displays as layer contents (NativeBufferView). The surface is
698
+ // tagged with its color space (kIOSurfaceColorSpace), which Core Animation
699
+ // reads; untagged surfaces are taken as sRGB.
700
+ VideoFrameHandle
701
+ ApplePlatformContext::createNativeBuffer(uint32_t width, uint32_t height,
702
+ wgpu::TextureFormat format,
703
+ bool displayP3) {
704
+ OSType pixelFormat;
705
+ CFStringRef colorSpaceName;
706
+ switch (format) {
707
+ case wgpu::TextureFormat::BGRA8Unorm:
708
+ pixelFormat = kCVPixelFormatType_32BGRA;
709
+ colorSpaceName = displayP3 ? kCGColorSpaceDisplayP3 : kCGColorSpaceSRGB;
710
+ break;
711
+ case wgpu::TextureFormat::RGBA16Float:
712
+ pixelFormat = kCVPixelFormatType_64RGBAHalf;
713
+ colorSpaceName =
714
+ displayP3 ? kCGColorSpaceExtendedDisplayP3 : kCGColorSpaceExtendedSRGB;
715
+ break;
716
+ default:
717
+ throw std::runtime_error(
718
+ "createNativeBuffer: unsupported format on Apple platforms (expected "
719
+ "'bgra8unorm' or 'rgba16float')");
720
+ }
721
+ NSDictionary *attrs = @{
722
+ (NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{},
723
+ (NSString *)kCVPixelBufferMetalCompatibilityKey : @YES,
724
+ };
725
+ CVPixelBufferRef pixelBuffer = NULL;
726
+ CVReturn err =
727
+ CVPixelBufferCreate(kCFAllocatorDefault, width, height, pixelFormat,
728
+ (__bridge CFDictionaryRef)attrs, &pixelBuffer);
729
+ if (err != kCVReturnSuccess || !pixelBuffer) {
730
+ throw std::runtime_error(
731
+ "createNativeBuffer: CVPixelBufferCreate failed (" +
732
+ std::to_string(err) + ")");
733
+ }
734
+ IOSurfaceRef ioSurface = CVPixelBufferGetIOSurface(pixelBuffer);
735
+ if (!ioSurface) {
736
+ CFRelease(pixelBuffer);
737
+ throw std::runtime_error(
738
+ "createNativeBuffer: pixel buffer is not IOSurface-backed");
739
+ }
740
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(colorSpaceName);
741
+ if (colorSpace != NULL) {
742
+ CFPropertyListRef plist = CGColorSpaceCopyPropertyList(colorSpace);
743
+ if (plist != NULL) {
744
+ IOSurfaceSetValue(ioSurface, kIOSurfaceColorSpace, plist);
745
+ CFRelease(plist);
746
+ }
747
+ CGColorSpaceRelease(colorSpace);
748
+ }
749
+
750
+ VideoFrameHandle handle;
751
+ handle.handle = (void *)ioSurface;
752
+ handle.width = width;
753
+ handle.height = height;
754
+ handle.colorSpace.primaries = displayP3
755
+ ? wgpu::ColorSpacePrimariesDawn::DisplayP3
756
+ : wgpu::ColorSpacePrimariesDawn::SRGB;
757
+ handle.deleter = [pixelBuffer]() { CFRelease(pixelBuffer); };
758
+ return handle;
759
+ }
760
+
688
761
  } // namespace rnwgpu
@@ -0,0 +1,88 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <Metal/Metal.h>
3
+
4
+ #include <atomic>
5
+ #include <memory>
6
+ #include <utility>
7
+
8
+ #include "NativeBufferViewRegistry.h"
9
+
10
+ namespace rnwgpu {
11
+
12
+ namespace {
13
+
14
+ // A fence that has not signaled after this long belongs to work that will
15
+ // never finish (a lost device): the buffer is dropped, as on Android.
16
+ constexpr int64_t kTimeoutSeconds = 10;
17
+
18
+ // One wait, finished by whichever comes first: the event's notification or
19
+ // the timeout. Metal cannot cancel a notification, so after a timeout the
20
+ // listener keeps its block (and this small object) until the event signals,
21
+ // possibly never; the fence and the buffer captured by `done` are released
22
+ // when the wait finishes. ANGLE's Metal sync (SyncMtl.mm) makes the same
23
+ // trade-off.
24
+ struct PendingFenceWait {
25
+ wgpu::SharedFence fence;
26
+ std::function<void(bool)> done;
27
+ std::atomic<bool> finished{false};
28
+
29
+ void finish(bool signaled) {
30
+ if (finished.exchange(true)) {
31
+ return;
32
+ }
33
+ auto callback = std::move(done);
34
+ fence = nullptr;
35
+ if (!signaled) {
36
+ NSLog(@"[WebGPU] presentNativeBuffer: the fence did not signal within "
37
+ @"%lld s; the buffer is dropped",
38
+ kTimeoutSeconds);
39
+ }
40
+ callback(signaled);
41
+ }
42
+ };
43
+
44
+ } // namespace
45
+
46
+ // The MTLSharedEvent behind a Dawn fence, waited on with a listener: no
47
+ // thread blocks. The notification and the timeout run on a private serial
48
+ // queue.
49
+ void waitForSharedFence(wgpu::SharedFence fence, uint64_t signaledValue,
50
+ std::function<void(bool)> done) {
51
+ wgpu::SharedFenceExportInfo info{};
52
+ wgpu::SharedFenceMTLSharedEventExportInfo mtlInfo{};
53
+ info.nextInChain = &mtlInfo;
54
+ fence.ExportInfo(&info);
55
+ id<MTLSharedEvent> event = (__bridge id<MTLSharedEvent>)mtlInfo.sharedEvent;
56
+ if (info.type != wgpu::SharedFenceType::MTLSharedEvent || event == nil) {
57
+ NSLog(@"[WebGPU] presentNativeBuffer: the fence is not an MTLSharedEvent; "
58
+ @"the buffer is dropped");
59
+ done(false);
60
+ return;
61
+ }
62
+ if (event.signaledValue >= signaledValue) {
63
+ done(true);
64
+ return;
65
+ }
66
+ static dispatch_queue_t queue = dispatch_queue_create(
67
+ "com.webgpu.NativeBufferView.fence", DISPATCH_QUEUE_SERIAL);
68
+ static MTLSharedEventListener *listener =
69
+ [[MTLSharedEventListener alloc] initWithDispatchQueue:queue];
70
+ // Holds the fence (which keeps the event, and Dawn's view of it, alive)
71
+ // until the wait finishes.
72
+ auto pending = std::make_shared<PendingFenceWait>();
73
+ pending->fence = std::move(fence);
74
+ pending->done = std::move(done);
75
+ [event
76
+ notifyListener:listener
77
+ atValue:signaledValue
78
+ block:^(id<MTLSharedEvent> /* event */, uint64_t /* value */) {
79
+ pending->finish(true);
80
+ }];
81
+ dispatch_after(
82
+ dispatch_time(DISPATCH_TIME_NOW, kTimeoutSeconds * NSEC_PER_SEC), queue,
83
+ ^{
84
+ pending->finish(false);
85
+ });
86
+ }
87
+
88
+ } // namespace rnwgpu
@@ -0,0 +1,15 @@
1
+ #pragma once
2
+
3
+ // RNWGUIKit selects AppKit for native macOS and UIKit for other Apple targets.
4
+ #import "RNWGUIKit.h"
5
+ #import <React/RCTViewComponentView.h>
6
+
7
+ NS_ASSUME_NONNULL_BEGIN
8
+
9
+ // NativeBufferView: displays an IOSurface that JS presented
10
+ // (RNWebGPU.presentNativeBuffer) as layer contents, with no swap chain and no
11
+ // GPU device on the display side. See NativeBufferViewRegistry.h.
12
+ @interface WebGPUNativeBufferView : RCTViewComponentView
13
+ @end
14
+
15
+ NS_ASSUME_NONNULL_END