react-native-wgpu 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +155 -69
- package/android/CMakeLists.txt +4 -5
- package/android/build.gradle +7 -18
- package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +34 -10
- package/apple/MetalView.mm +0 -19
- package/apple/WebGPUModule.h +1 -9
- package/apple/WebGPUModule.mm +0 -3
- package/apple/WebGPUView.h +0 -3
- package/apple/WebGPUView.mm +0 -2
- package/cpp/WGPULogger.h +10 -0
- package/cpp/dawn/dawn_proc_table.h +1 -1
- package/cpp/dawn/webgpu.h +4855 -0
- package/cpp/dawn/webgpu_cpp.h +10168 -0
- package/cpp/dawn/wire/client/webgpu.h +354 -0
- package/cpp/dawn/wire/client/webgpu_cpp.h +10343 -0
- package/cpp/dawn/wire/client/webgpu_cpp_print.h +2715 -0
- package/cpp/jsi/RNFJSIConverter.h +17 -59
- package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
- package/cpp/rnwgpu/api/GPU.cpp +51 -26
- package/cpp/rnwgpu/api/GPU.h +5 -18
- package/cpp/rnwgpu/api/GPUAdapter.cpp +75 -54
- package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
- package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
- package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
- package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
- package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
- package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
- package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
- package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
- package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
- package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
- package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
- package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
- package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
- package/cpp/rnwgpu/api/GPUDevice.h +22 -21
- package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
- package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
- package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
- package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
- package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
- package/cpp/rnwgpu/api/GPUQueue.h +7 -6
- package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
- package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
- package/cpp/rnwgpu/api/GPUSampler.h +1 -3
- package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
- package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
- package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
- package/cpp/rnwgpu/api/GPUTexture.h +1 -3
- package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
- package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
- package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
- package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
- package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
- package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
- package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
- package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
- package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
- package/cpp/webgpu/webgpu.h +5 -4827
- package/cpp/webgpu/webgpu_cpp.h +5 -10140
- package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
- package/lib/commonjs/Canvas.js +6 -66
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/hooks.js +6 -42
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/Canvas.js +7 -67
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/hooks.js +5 -40
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
- package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/hooks.d.ts +1 -5
- package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +0 -1
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/hooks.d.ts +2 -7
- package/lib/typescript/src/hooks.d.ts.map +1 -1
- package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
- package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
- package/libs/android/x86/libwebgpu_dawn.so +0 -0
- package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
- package/package.json +4 -3
- package/react-native-wgpu.podspec +12 -16
- package/src/Canvas.tsx +8 -69
- package/src/hooks.tsx +14 -48
- package/android/cpp/platform/ThreadUtils.cpp +0 -41
- package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
- package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
- package/apple/WebGPUViewManager.mm +0 -24
- package/apple/platform/ThreadUtils.cpp +0 -34
- package/cpp/dawn/dawn_proc.h +0 -50
- package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
- package/cpp/dawn/native/D3D11Backend.h +0 -77
- package/cpp/dawn/native/D3D12Backend.h +0 -68
- package/cpp/dawn/native/D3DBackend.h +0 -56
- package/cpp/dawn/native/MetalBackend.h +0 -56
- package/cpp/dawn/platform/DawnPlatform.h +0 -167
- package/cpp/dawn/platform/dawn_platform_export.h +0 -49
- package/cpp/platform/ThreadUtils.h +0 -30
- package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
- package/cpp/threading/CallInvokerDispatcher.h +0 -37
- package/cpp/threading/Dispatcher.cpp +0 -55
- package/cpp/threading/Dispatcher.h +0 -93
- package/cpp/threading/ThreadPool.cpp +0 -88
- package/cpp/threading/ThreadPool.h +0 -53
- package/cpp/webgpu/webgpu_glfw.h +0 -88
- package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
- package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
- package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/config.d.ts +0 -3
- package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
- package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
- package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/setup.d.ts +0 -63
- package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
- package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
- package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/dawn.json +0 -4670
- package/src/__tests__/Alpha.spec.ts +0 -28
- package/src/__tests__/ArrayBuffer.spec.ts +0 -76
- package/src/__tests__/Buffer.spec.ts +0 -357
- package/src/__tests__/ComputeShader.spec.ts +0 -375
- package/src/__tests__/Constants.spec.ts +0 -89
- package/src/__tests__/Device.spec.ts +0 -84
- package/src/__tests__/ErrorScope.spec.ts +0 -92
- package/src/__tests__/ExternalTexture.spec.ts +0 -284
- package/src/__tests__/GPU.spec.ts +0 -272
- package/src/__tests__/ImageData.spec.ts +0 -26
- package/src/__tests__/Shaders.spec.ts +0 -232
- package/src/__tests__/Texture.spec.ts +0 -197
- package/src/__tests__/assets/Di-3d.png +0 -0
- package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
- package/src/__tests__/components/Wireframe/models.ts +0 -113
- package/src/__tests__/components/Wireframe/utils.ts +0 -22
- package/src/__tests__/components/cube.ts +0 -51
- package/src/__tests__/components/meshes/mesh.ts +0 -96
- package/src/__tests__/components/meshes/sphere.ts +0 -103
- package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
- package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
- package/src/__tests__/components/meshes/teapot.ts +0 -13
- package/src/__tests__/components/meshes/utils.ts +0 -235
- package/src/__tests__/components/triangle.ts +0 -17
- package/src/__tests__/config.ts +0 -2
- package/src/__tests__/demos/ABuffer.spec.ts +0 -890
- package/src/__tests__/demos/Blur.spec.ts +0 -398
- package/src/__tests__/demos/Cube.spec.ts +0 -929
- package/src/__tests__/demos/FractalCube.spec.ts +0 -240
- package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
- package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
- package/src/__tests__/demos/Triangle.spec.ts +0 -266
- package/src/__tests__/demos/Wireframe.spec.ts +0 -188
- package/src/__tests__/globalSetup.ts +0 -45
- package/src/__tests__/globalTeardown.ts +0 -11
- package/src/__tests__/setup.ts +0 -423
- package/src/__tests__/snapshots/abuffer.png +0 -0
- package/src/__tests__/snapshots/asteroid.png +0 -0
- package/src/__tests__/snapshots/blur.png +0 -0
- package/src/__tests__/snapshots/buffer.png +0 -0
- package/src/__tests__/snapshots/constant-triangle.png +0 -0
- package/src/__tests__/snapshots/cube.png +0 -0
- package/src/__tests__/snapshots/f.png +0 -0
- package/src/__tests__/snapshots/f2.png +0 -0
- package/src/__tests__/snapshots/fractal-cubes.png +0 -0
- package/src/__tests__/snapshots/instanced-cubes.png +0 -0
- package/src/__tests__/snapshots/occlusion-query.png +0 -0
- package/src/__tests__/snapshots/ref.png +0 -0
- package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
- package/src/__tests__/snapshots/texture.png +0 -0
- package/src/__tests__/snapshots/textured-cube.png +0 -0
- package/src/__tests__/snapshots/triangle-msaa.png +0 -0
- package/src/__tests__/snapshots/triangle.png +0 -0
- package/src/__tests__/snapshots/two-cube.png +0 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
#include "AsyncTaskHandle.h"
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <utility>
|
|
5
|
+
|
|
6
|
+
#include "RNFPromise.h"
|
|
7
|
+
|
|
8
|
+
#include "AsyncRunner.h"
|
|
9
|
+
|
|
10
|
+
namespace rnwgpu::async {
|
|
11
|
+
|
|
12
|
+
using Action = std::function<void(jsi::Runtime &, margelo::Promise &)>;
|
|
13
|
+
|
|
14
|
+
struct AsyncTaskHandle::State
|
|
15
|
+
: public std::enable_shared_from_this<AsyncTaskHandle::State> {
|
|
16
|
+
State(std::shared_ptr<AsyncRunner> runner, bool keepPumping)
|
|
17
|
+
: runner(std::move(runner)), keepPumping(keepPumping) {}
|
|
18
|
+
|
|
19
|
+
void settle(Action action);
|
|
20
|
+
void attachPromise(const std::shared_ptr<margelo::Promise> &promise);
|
|
21
|
+
void schedule(Action action);
|
|
22
|
+
|
|
23
|
+
ResolveFunction createResolveFunction();
|
|
24
|
+
RejectFunction createRejectFunction();
|
|
25
|
+
|
|
26
|
+
std::shared_ptr<margelo::Promise> currentPromise();
|
|
27
|
+
|
|
28
|
+
std::mutex mutex;
|
|
29
|
+
std::weak_ptr<AsyncRunner> runner;
|
|
30
|
+
std::shared_ptr<margelo::Promise> promise;
|
|
31
|
+
std::optional<Action> pendingAction;
|
|
32
|
+
bool settled = false;
|
|
33
|
+
std::shared_ptr<State> keepAlive;
|
|
34
|
+
bool keepPumping;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// MARK: - State helpers
|
|
38
|
+
|
|
39
|
+
void AsyncTaskHandle::State::settle(Action action) {
|
|
40
|
+
std::optional<Action> actionToSchedule;
|
|
41
|
+
|
|
42
|
+
{
|
|
43
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
44
|
+
if (settled) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
settled = true;
|
|
48
|
+
|
|
49
|
+
if (promise) {
|
|
50
|
+
actionToSchedule = std::move(action);
|
|
51
|
+
} else {
|
|
52
|
+
pendingAction = std::move(action);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (actionToSchedule.has_value()) {
|
|
57
|
+
schedule(std::move(*actionToSchedule));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
void AsyncTaskHandle::State::attachPromise(
|
|
62
|
+
const std::shared_ptr<margelo::Promise> &newPromise) {
|
|
63
|
+
std::optional<Action> actionToSchedule;
|
|
64
|
+
{
|
|
65
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
66
|
+
promise = newPromise;
|
|
67
|
+
keepAlive = shared_from_this();
|
|
68
|
+
if (pendingAction.has_value()) {
|
|
69
|
+
actionToSchedule = std::move(pendingAction);
|
|
70
|
+
pendingAction.reset();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (actionToSchedule.has_value()) {
|
|
75
|
+
schedule(std::move(*actionToSchedule));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
void AsyncTaskHandle::State::schedule(Action action) {
|
|
80
|
+
auto runnerRef = runner.lock();
|
|
81
|
+
if (!runnerRef) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
auto promiseRef = currentPromise();
|
|
86
|
+
if (!promiseRef) {
|
|
87
|
+
runnerRef->onTaskSettled(keepPumping);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
auto dispatcherRef = runnerRef->dispatcher();
|
|
92
|
+
if (!dispatcherRef) {
|
|
93
|
+
runnerRef->onTaskSettled(keepPumping);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
dispatcherRef->post([self = shared_from_this(), action = std::move(action),
|
|
98
|
+
runnerRef, promiseRef](jsi::Runtime &runtime) mutable {
|
|
99
|
+
runnerRef->onTaskSettled(self->keepPumping);
|
|
100
|
+
action(runtime, *promiseRef);
|
|
101
|
+
std::lock_guard<std::mutex> lock(self->mutex);
|
|
102
|
+
self->keepAlive.reset();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
AsyncTaskHandle::ResolveFunction
|
|
107
|
+
AsyncTaskHandle::State::createResolveFunction() {
|
|
108
|
+
auto weakSelf = std::weak_ptr<State>(shared_from_this());
|
|
109
|
+
return [weakSelf](ValueFactory factory) {
|
|
110
|
+
if (auto self = weakSelf.lock()) {
|
|
111
|
+
ValueFactory resolvedFactory =
|
|
112
|
+
factory ? std::move(factory) : [](jsi::Runtime &runtime) {
|
|
113
|
+
return jsi::Value::undefined();
|
|
114
|
+
};
|
|
115
|
+
self->settle(
|
|
116
|
+
[factory = std::move(resolvedFactory)](
|
|
117
|
+
jsi::Runtime &runtime, margelo::Promise &promise) mutable {
|
|
118
|
+
auto value = factory(runtime);
|
|
119
|
+
promise.resolve(std::move(value));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
AsyncTaskHandle::RejectFunction AsyncTaskHandle::State::createRejectFunction() {
|
|
126
|
+
auto weakSelf = std::weak_ptr<State>(shared_from_this());
|
|
127
|
+
return [weakSelf](std::string reason) {
|
|
128
|
+
if (auto self = weakSelf.lock()) {
|
|
129
|
+
self->settle([reason = std::move(reason)](jsi::Runtime & /*runtime*/,
|
|
130
|
+
margelo::Promise &promise) {
|
|
131
|
+
promise.reject(reason);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
std::shared_ptr<margelo::Promise> AsyncTaskHandle::State::currentPromise() {
|
|
138
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
139
|
+
return promise;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// MARK: - AsyncTaskHandle
|
|
143
|
+
|
|
144
|
+
AsyncTaskHandle::AsyncTaskHandle() = default;
|
|
145
|
+
|
|
146
|
+
AsyncTaskHandle::AsyncTaskHandle(std::shared_ptr<State> state)
|
|
147
|
+
: _state(std::move(state)) {}
|
|
148
|
+
|
|
149
|
+
bool AsyncTaskHandle::valid() const { return _state != nullptr; }
|
|
150
|
+
|
|
151
|
+
AsyncTaskHandle
|
|
152
|
+
AsyncTaskHandle::create(const std::shared_ptr<AsyncRunner> &runner,
|
|
153
|
+
bool keepPumping) {
|
|
154
|
+
auto state = std::make_shared<State>(runner, keepPumping);
|
|
155
|
+
state->keepAlive = state;
|
|
156
|
+
return AsyncTaskHandle(std::move(state));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
AsyncTaskHandle::ResolveFunction
|
|
160
|
+
AsyncTaskHandle::createResolveFunction() const {
|
|
161
|
+
if (!_state) {
|
|
162
|
+
return [](ValueFactory) {};
|
|
163
|
+
}
|
|
164
|
+
return _state->createResolveFunction();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
AsyncTaskHandle::RejectFunction AsyncTaskHandle::createRejectFunction() const {
|
|
168
|
+
if (!_state) {
|
|
169
|
+
return [](std::string) {};
|
|
170
|
+
}
|
|
171
|
+
return _state->createRejectFunction();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
void AsyncTaskHandle::attachPromise(
|
|
175
|
+
const std::shared_ptr<margelo::Promise> &promise) const {
|
|
176
|
+
if (_state) {
|
|
177
|
+
_state->attachPromise(promise);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
} // namespace rnwgpu::async
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <functional>
|
|
4
|
+
#include <memory>
|
|
5
|
+
#include <mutex>
|
|
6
|
+
#include <optional>
|
|
7
|
+
#include <string>
|
|
8
|
+
|
|
9
|
+
#include <jsi/jsi.h>
|
|
10
|
+
|
|
11
|
+
#include "AsyncDispatcher.h"
|
|
12
|
+
|
|
13
|
+
namespace margelo {
|
|
14
|
+
class Promise;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
namespace rnwgpu::async {
|
|
18
|
+
|
|
19
|
+
class AsyncRunner;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Represents a pending asynchronous WebGPU operation that can be converted into
|
|
23
|
+
* a JavaScript Promise.
|
|
24
|
+
*/
|
|
25
|
+
class AsyncTaskHandle {
|
|
26
|
+
public:
|
|
27
|
+
struct State;
|
|
28
|
+
|
|
29
|
+
using ValueFactory =
|
|
30
|
+
std::function<facebook::jsi::Value(facebook::jsi::Runtime &)>;
|
|
31
|
+
using ResolveFunction = std::function<void(ValueFactory)>;
|
|
32
|
+
using RejectFunction = std::function<void(std::string)>;
|
|
33
|
+
|
|
34
|
+
AsyncTaskHandle();
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Internal constructor used by AsyncRunner.
|
|
38
|
+
*/
|
|
39
|
+
explicit AsyncTaskHandle(std::shared_ptr<State> state);
|
|
40
|
+
|
|
41
|
+
bool valid() const;
|
|
42
|
+
|
|
43
|
+
ResolveFunction createResolveFunction() const;
|
|
44
|
+
RejectFunction createRejectFunction() const;
|
|
45
|
+
|
|
46
|
+
void attachPromise(const std::shared_ptr<margelo::Promise> &promise) const;
|
|
47
|
+
|
|
48
|
+
static AsyncTaskHandle create(const std::shared_ptr<AsyncRunner> &runner,
|
|
49
|
+
bool keepPumping);
|
|
50
|
+
|
|
51
|
+
private:
|
|
52
|
+
std::shared_ptr<State> _state;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
} // namespace rnwgpu::async
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#include "JSIMicrotaskDispatcher.h"
|
|
2
|
+
|
|
3
|
+
#include <utility>
|
|
4
|
+
|
|
5
|
+
namespace rnwgpu::async {
|
|
6
|
+
|
|
7
|
+
JSIMicrotaskDispatcher::JSIMicrotaskDispatcher(jsi::Runtime &runtime)
|
|
8
|
+
: _runtime(runtime) {}
|
|
9
|
+
|
|
10
|
+
void JSIMicrotaskDispatcher::post(Work work) {
|
|
11
|
+
auto microtask = jsi::Function::createFromHostFunction(
|
|
12
|
+
_runtime, jsi::PropNameID::forAscii(_runtime, "AsyncMicrotask"), 0,
|
|
13
|
+
[work = std::move(work)](
|
|
14
|
+
jsi::Runtime &runtime, const jsi::Value & /*thisValue*/,
|
|
15
|
+
const jsi::Value * /*args*/, size_t /*count*/) -> jsi::Value {
|
|
16
|
+
work(runtime);
|
|
17
|
+
return jsi::Value::undefined();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
_runtime.queueMicrotask(std::move(microtask));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
} // namespace rnwgpu::async
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "AsyncDispatcher.h"
|
|
4
|
+
|
|
5
|
+
namespace rnwgpu::async {
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Dispatcher implementation backed by `jsi::Runtime::queueMicrotask`.
|
|
9
|
+
*/
|
|
10
|
+
class JSIMicrotaskDispatcher final
|
|
11
|
+
: public AsyncDispatcher,
|
|
12
|
+
public std::enable_shared_from_this<JSIMicrotaskDispatcher> {
|
|
13
|
+
public:
|
|
14
|
+
explicit JSIMicrotaskDispatcher(jsi::Runtime &runtime);
|
|
15
|
+
|
|
16
|
+
void post(Work work) override;
|
|
17
|
+
|
|
18
|
+
private:
|
|
19
|
+
jsi::Runtime &_runtime;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
} // namespace rnwgpu::async
|