react-native-webgpu 0.9.0 → 0.10.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/Package.swift +191 -0
- package/android/CMakeLists.txt +1 -2
- package/android/src/main/java/com/webgpu/WebGPUSurfaceView.java +10 -1
- package/android/src/main/java/com/webgpu/WebGPUTextureView.java +2 -2
- package/android/src/main/java/com/webgpu/WebGPUView.java +48 -16
- package/android/src/main/java/com/webgpu/WebGPUViewManager.java +21 -4
- package/cpp/jsi/JSICache.cpp +97 -0
- package/cpp/jsi/JSICache.h +90 -0
- package/cpp/jsi/NativeObject.h +24 -86
- package/cpp/rnwgpu/RNWebGPUManager.cpp +0 -3
- package/cpp/rnwgpu/SurfaceRegistry.h +153 -3
- package/cpp/rnwgpu/api/GPUAdapter.cpp +43 -1
- package/cpp/rnwgpu/api/GPUDevice.cpp +6 -1
- package/cpp/rnwgpu/api/GPUExternalTexture.cpp +3 -3
- package/cpp/rnwgpu/api/GPUFeatures.h +2 -1
- package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +2 -2
- package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +11 -0
- package/cpp/webgpu/webgpu.h +63 -29
- package/cpp/webgpu/webgpu_cpp.h +415 -367
- package/cpp/webgpu/webgpu_cpp_print.h +21 -9
- package/cpp/webgpu/webgpu_enum_class_bitmasks.h +2 -1
- package/lib/commonjs/Canvas.js +8 -2
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/commonjs/WebGPUViewNativeComponent.web.js +4 -2
- package/lib/commonjs/WebGPUViewNativeComponent.web.js.map +1 -1
- package/lib/commonjs/mock.js +3 -1
- package/lib/commonjs/mock.js.map +1 -1
- package/lib/module/Canvas.js +8 -2
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/WebGPUViewNativeComponent.js.map +1 -1
- package/lib/module/WebGPUViewNativeComponent.web.js +4 -2
- package/lib/module/WebGPUViewNativeComponent.web.js.map +1 -1
- package/lib/module/mock.js +3 -1
- package/lib/module/mock.js.map +1 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts +3 -2
- package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/lib/commonjs/mock.d.ts +3 -2
- package/lib/typescript/lib/commonjs/mock.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts +3 -2
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/lib/module/mock.d.ts +3 -2
- package/lib/typescript/lib/module/mock.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +24 -3
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts +4 -2
- package/lib/typescript/src/WebGPUViewNativeComponent.d.ts.map +1 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts +3 -1
- package/lib/typescript/src/WebGPUViewNativeComponent.web.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/mock.d.ts +3 -7
- package/lib/typescript/src/mock.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 +12 -6
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/dawn_proc_table.h +329 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/native/DawnNative.h +367 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/native/NullBackend.h +39 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/native/OpenGLBackend.h +89 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/native/VulkanBackend.h +183 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/native/dawn_native_export.h +49 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/webgpu.h +5063 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/webgpu.ixx +358 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/webgpu_cpp.h +10401 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/webgpu_cpp_print.h +2868 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/wire/client/webgpu.h +369 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/wire/client/webgpu_cpp.h +10584 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/dawn/wire/client/webgpu_cpp_print.h +2868 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu/webgpu.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu/webgpu_cpp.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu/webgpu_cpp_chained_struct.h +57 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu/webgpu_cpp_print.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu/webgpu_enum_class_bitmasks.h +163 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu_upstream/webgpu/webgpu_cpp.h +5932 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu_upstream/webgpu/webgpu_cpp_chained_struct.h +54 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu_upstream/webgpu/webgpu_cpp_print.h +2059 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/Headers/webgpu_upstream/webgpu/webgpu_enum_class_bitmasks.h +163 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/dawn_proc_table.h +329 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/native/DawnNative.h +367 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/native/NullBackend.h +39 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/native/OpenGLBackend.h +89 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/native/VulkanBackend.h +183 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/native/dawn_native_export.h +49 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/webgpu.h +5063 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/webgpu.ixx +358 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/webgpu_cpp.h +10401 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/webgpu_cpp_print.h +2868 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/wire/client/webgpu.h +369 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/wire/client/webgpu_cpp.h +10584 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/dawn/wire/client/webgpu_cpp_print.h +2868 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu/webgpu.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu/webgpu_cpp.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu/webgpu_cpp_chained_struct.h +57 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu/webgpu_cpp_print.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu/webgpu_enum_class_bitmasks.h +163 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu_upstream/webgpu/webgpu_cpp.h +5932 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu_upstream/webgpu/webgpu_cpp_chained_struct.h +54 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu_upstream/webgpu/webgpu_cpp_print.h +2059 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/Headers/webgpu_upstream/webgpu/webgpu_enum_class_bitmasks.h +163 -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/Headers/dawn/dawn_proc_table.h +329 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/native/DawnNative.h +367 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/native/NullBackend.h +39 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/native/OpenGLBackend.h +89 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/native/VulkanBackend.h +183 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/native/dawn_native_export.h +49 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/webgpu.h +5063 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/webgpu.ixx +358 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/webgpu_cpp.h +10401 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/webgpu_cpp_print.h +2868 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/wire/client/webgpu.h +369 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/wire/client/webgpu_cpp.h +10584 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/dawn/wire/client/webgpu_cpp_print.h +2868 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu/webgpu.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu/webgpu_cpp.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu/webgpu_cpp_chained_struct.h +57 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu/webgpu_cpp_print.h +33 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu/webgpu_enum_class_bitmasks.h +163 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu_upstream/webgpu/webgpu_cpp.h +5932 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu_upstream/webgpu/webgpu_cpp_chained_struct.h +54 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu_upstream/webgpu/webgpu_cpp_print.h +2059 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/Headers/webgpu_upstream/webgpu/webgpu_enum_class_bitmasks.h +163 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
- package/package.json +6 -4
- package/react-native-webgpu.podspec +33 -9
- package/src/Canvas.tsx +43 -4
- package/src/WebGPUViewNativeComponent.ts +10 -2
- package/src/WebGPUViewNativeComponent.web.ts +12 -3
- package/src/index.tsx +7 -0
- package/src/mock.ts +12 -9
- package/cpp/jsi/RuntimeAwareCache.cpp +0 -8
- package/cpp/jsi/RuntimeAwareCache.h +0 -133
- package/cpp/jsi/RuntimeLifecycleMonitor.cpp +0 -72
- package/cpp/jsi/RuntimeLifecycleMonitor.h +0 -32
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Copyright 2017 The Dawn & Tint Authors
|
|
2
|
+
//
|
|
3
|
+
// Redistribution and use in source and binary forms, with or without
|
|
4
|
+
// modification, are permitted provided that the following conditions are met:
|
|
5
|
+
//
|
|
6
|
+
// 1. Redistributions of source code must retain the above copyright notice, this
|
|
7
|
+
// list of conditions and the following disclaimer.
|
|
8
|
+
//
|
|
9
|
+
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
10
|
+
// this list of conditions and the following disclaimer in the documentation
|
|
11
|
+
// and/or other materials provided with the distribution.
|
|
12
|
+
//
|
|
13
|
+
// 3. Neither the name of the copyright holder nor the names of its
|
|
14
|
+
// contributors may be used to endorse or promote products derived from
|
|
15
|
+
// this software without specific prior written permission.
|
|
16
|
+
//
|
|
17
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
18
|
+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19
|
+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20
|
+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
21
|
+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
22
|
+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
23
|
+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
24
|
+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
25
|
+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
|
|
28
|
+
#ifndef WEBGPU_ENUM_CLASS_BITMASKS_H_
|
|
29
|
+
#define WEBGPU_ENUM_CLASS_BITMASKS_H_
|
|
30
|
+
|
|
31
|
+
#include <type_traits>
|
|
32
|
+
|
|
33
|
+
// The operators in wgpu:: namespace need be introduced into other namespaces with
|
|
34
|
+
// using-declarations for C++ Argument Dependent Lookup to work.
|
|
35
|
+
#define WGPU_IMPORT_BITMASK_OPERATORS \
|
|
36
|
+
using wgpu::operator|; \
|
|
37
|
+
using wgpu::operator&; \
|
|
38
|
+
using wgpu::operator^; \
|
|
39
|
+
using wgpu::operator~; \
|
|
40
|
+
using wgpu::operator&=; \
|
|
41
|
+
using wgpu::operator|=; \
|
|
42
|
+
using wgpu::operator^=; \
|
|
43
|
+
using wgpu::HasZeroOrOneBits;
|
|
44
|
+
|
|
45
|
+
namespace wgpu {
|
|
46
|
+
|
|
47
|
+
template <typename T>
|
|
48
|
+
struct IsWGPUBitmask {
|
|
49
|
+
static constexpr bool enable = false;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
template <typename T, typename Enable = void>
|
|
53
|
+
struct LowerBitmask {
|
|
54
|
+
static constexpr bool enable = false;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
template <typename T>
|
|
58
|
+
struct LowerBitmask<T, typename std::enable_if<IsWGPUBitmask<T>::enable>::type> {
|
|
59
|
+
static constexpr bool enable = true;
|
|
60
|
+
using type = T;
|
|
61
|
+
constexpr static T Lower(T t) { return t; }
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
template <typename T>
|
|
65
|
+
struct BoolConvertible {
|
|
66
|
+
using Integral = typename std::underlying_type<T>::type;
|
|
67
|
+
|
|
68
|
+
explicit constexpr BoolConvertible(Integral value) : value(value) {}
|
|
69
|
+
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
70
|
+
constexpr operator bool() const { return value != 0; }
|
|
71
|
+
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
72
|
+
constexpr operator T() const { return static_cast<T>(value); }
|
|
73
|
+
|
|
74
|
+
Integral value;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
template <typename T>
|
|
78
|
+
struct LowerBitmask<BoolConvertible<T>> {
|
|
79
|
+
static constexpr bool enable = true;
|
|
80
|
+
using type = T;
|
|
81
|
+
static constexpr type Lower(BoolConvertible<T> t) { return t; }
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
template <
|
|
85
|
+
typename T1,
|
|
86
|
+
typename T2,
|
|
87
|
+
typename = typename std::enable_if<LowerBitmask<T1>::enable && LowerBitmask<T2>::enable>::type>
|
|
88
|
+
constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator|(T1 left, T2 right) {
|
|
89
|
+
using T = typename LowerBitmask<T1>::type;
|
|
90
|
+
using Integral = typename std::underlying_type<T>::type;
|
|
91
|
+
return BoolConvertible<T>(static_cast<Integral>(LowerBitmask<T1>::Lower(left)) |
|
|
92
|
+
static_cast<Integral>(LowerBitmask<T2>::Lower(right)));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
template <
|
|
96
|
+
typename T1,
|
|
97
|
+
typename T2,
|
|
98
|
+
typename = typename std::enable_if<LowerBitmask<T1>::enable && LowerBitmask<T2>::enable>::type>
|
|
99
|
+
constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator&(T1 left, T2 right) {
|
|
100
|
+
using T = typename LowerBitmask<T1>::type;
|
|
101
|
+
using Integral = typename std::underlying_type<T>::type;
|
|
102
|
+
return BoolConvertible<T>(static_cast<Integral>(LowerBitmask<T1>::Lower(left)) &
|
|
103
|
+
static_cast<Integral>(LowerBitmask<T2>::Lower(right)));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
template <
|
|
107
|
+
typename T1,
|
|
108
|
+
typename T2,
|
|
109
|
+
typename = typename std::enable_if<LowerBitmask<T1>::enable && LowerBitmask<T2>::enable>::type>
|
|
110
|
+
constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator^(T1 left, T2 right) {
|
|
111
|
+
using T = typename LowerBitmask<T1>::type;
|
|
112
|
+
using Integral = typename std::underlying_type<T>::type;
|
|
113
|
+
return BoolConvertible<T>(static_cast<Integral>(LowerBitmask<T1>::Lower(left)) ^
|
|
114
|
+
static_cast<Integral>(LowerBitmask<T2>::Lower(right)));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
template <typename T1>
|
|
118
|
+
constexpr BoolConvertible<typename LowerBitmask<T1>::type> operator~(T1 t) {
|
|
119
|
+
using T = typename LowerBitmask<T1>::type;
|
|
120
|
+
using Integral = typename std::underlying_type<T>::type;
|
|
121
|
+
return BoolConvertible<T>(
|
|
122
|
+
static_cast<Integral>(~static_cast<Integral>(LowerBitmask<T1>::Lower(t))));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
template <
|
|
126
|
+
typename T,
|
|
127
|
+
typename T2,
|
|
128
|
+
typename = typename std::enable_if<IsWGPUBitmask<T>::enable && LowerBitmask<T2>::enable>::type>
|
|
129
|
+
constexpr T& operator&=(T& l, T2 right) {
|
|
130
|
+
T r = LowerBitmask<T2>::Lower(right);
|
|
131
|
+
l = l & r;
|
|
132
|
+
return l;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
template <
|
|
136
|
+
typename T,
|
|
137
|
+
typename T2,
|
|
138
|
+
typename = typename std::enable_if<IsWGPUBitmask<T>::enable && LowerBitmask<T2>::enable>::type>
|
|
139
|
+
constexpr T& operator|=(T& l, T2 right) {
|
|
140
|
+
T r = LowerBitmask<T2>::Lower(right);
|
|
141
|
+
l = l | r;
|
|
142
|
+
return l;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
template <
|
|
146
|
+
typename T,
|
|
147
|
+
typename T2,
|
|
148
|
+
typename = typename std::enable_if<IsWGPUBitmask<T>::enable && LowerBitmask<T2>::enable>::type>
|
|
149
|
+
constexpr T& operator^=(T& l, T2 right) {
|
|
150
|
+
T r = LowerBitmask<T2>::Lower(right);
|
|
151
|
+
l = l ^ r;
|
|
152
|
+
return l;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
template <typename T>
|
|
156
|
+
constexpr bool HasZeroOrOneBits(T value) {
|
|
157
|
+
using Integral = typename std::underlying_type<T>::type;
|
|
158
|
+
return (static_cast<Integral>(value) & (static_cast<Integral>(value) - 1)) == 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
} // namespace wgpu
|
|
162
|
+
|
|
163
|
+
#endif // WEBGPU_ENUM_CLASS_BITMASKS_H_
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-webgpu",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "React Native WebGPU",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -20,11 +20,12 @@
|
|
|
20
20
|
"apple/**",
|
|
21
21
|
"libs/**",
|
|
22
22
|
"*.podspec",
|
|
23
|
+
"Package.swift",
|
|
23
24
|
"app.plugin.js",
|
|
24
25
|
"plugin/build/**"
|
|
25
26
|
],
|
|
26
|
-
"dawn": "chrome-
|
|
27
|
-
"dawnCommit": "
|
|
27
|
+
"dawn": "chrome-m154",
|
|
28
|
+
"dawnCommit": "3d786993a7ded64c4ebb4884b9b079db9ad0e580",
|
|
28
29
|
"scripts": {
|
|
29
30
|
"test": "NODE_OPTIONS='--experimental-require-module' jest -i",
|
|
30
31
|
"test:ref": "REFERENCE=true NODE_OPTIONS='--experimental-require-module' jest -i",
|
|
@@ -42,7 +43,8 @@
|
|
|
42
43
|
"clang-format-android": "find android/cpp/ -iname \"*.h\" -o -iname \"*.m\" -o -iname \"*.cpp\" | xargs clang-format -i",
|
|
43
44
|
"clang-format-common": "find cpp/rnwgpu -iname \"*.h\" -o -iname \"*.m\" -o -iname \"*.cpp\" | xargs clang-format -i",
|
|
44
45
|
"cpplint": "cpplint --linelength=230 --filter=-legal/copyright,-whitespace/indent,-whitespace/comments,-whitespace/ending_newline,-build/include_order,-runtime/references,-readability/todo,-whitespace/blank_line,-whitespace/todo,-runtime/int,-build/c++11,-whitespace/parens --exclude=example --exclude=android/.cxx --exclude=cpp/webgpu --exclude=cpp/dawn --exclude=ios --exclude=android/build --exclude=node_modules --recursive .",
|
|
45
|
-
"install-dawn": "tsx scripts/install-dawn.ts"
|
|
46
|
+
"install-dawn": "tsx scripts/install-dawn.ts",
|
|
47
|
+
"generate-package-swift": "tsx scripts/generate-package-swift.ts"
|
|
46
48
|
},
|
|
47
49
|
"keywords": [
|
|
48
50
|
"react-native",
|
|
@@ -14,17 +14,41 @@ Pod::Spec.new do |s|
|
|
|
14
14
|
s.platforms = { :ios => min_ios_version_supported, :osx => "10.15", :visionos => "1.0" }
|
|
15
15
|
s.source = { :git => "https://github.com/wcandillon/react-native-webgpu.git", :tag => "#{s.version}" }
|
|
16
16
|
|
|
17
|
-
s
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
# RN's autolinking and codegen both key off this podspec being present and
|
|
18
|
+
# supporting the platform (see react-native.config.js docs: there is no way
|
|
19
|
+
# to disable CocoaPods linking for a dependency without also disabling
|
|
20
|
+
# codegen for it). So when the app links react-native-webgpu via the local
|
|
21
|
+
# Swift Package (packages/webgpu/Package.swift) instead of CocoaPods, this
|
|
22
|
+
# pod still needs to exist to keep the RNWgpuViewSpec codegen output
|
|
23
|
+
# flowing — it just shouldn't compile or link anything itself, or the app
|
|
24
|
+
# would get the native module twice. Set RNWGPU_USE_SPM=1 before
|
|
25
|
+
# `pod install` for that case.
|
|
26
|
+
if ENV['RNWGPU_USE_SPM']
|
|
27
|
+
# The Swift package resolves React Native's headers from the
|
|
28
|
+
# static-library Pods layout (Pods/Headers/Public). With `use_frameworks!`
|
|
29
|
+
# CocoaPods keeps headers inside each framework instead, so none of those
|
|
30
|
+
# paths exist and the package fails to compile with header-not-found
|
|
31
|
+
# errors that don't point back here. Refuse the combination up front.
|
|
32
|
+
# The React Native template drives `use_frameworks!` from USE_FRAMEWORKS.
|
|
33
|
+
if ENV['USE_FRAMEWORKS']
|
|
34
|
+
raise "react-native-webgpu: RNWGPU_USE_SPM needs the static-library Pods layout. " \
|
|
35
|
+
"Unset USE_FRAMEWORKS (or drop use_frameworks! from the Podfile) to link " \
|
|
36
|
+
"react-native-webgpu through its Swift package."
|
|
37
|
+
end
|
|
38
|
+
s.source_files = "apple/RNWGUIKit.h"
|
|
39
|
+
else
|
|
40
|
+
s.source_files = [
|
|
41
|
+
"apple/**/*.{h,c,cc,cpp,m,mm,swift}",
|
|
42
|
+
"cpp/**/*.{h,cpp}"
|
|
43
|
+
]
|
|
21
44
|
|
|
22
|
-
|
|
45
|
+
s.vendored_frameworks = 'libs/apple/libwebgpu_dawn.xcframework'
|
|
23
46
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
47
|
+
# The VideoPlayer API uses AVFoundation / CoreMedia, and shared-texture
|
|
48
|
+
# surfaces use CoreVideo (CVPixelBuffer). Link them so their symbols resolve.
|
|
49
|
+
# ImageIO provides CGImageSource, the image decoder behind createImageBitmap.
|
|
50
|
+
s.frameworks = "AVFoundation", "CoreMedia", "CoreVideo", "ImageIO"
|
|
51
|
+
end
|
|
28
52
|
|
|
29
53
|
s.pod_target_xcconfig = {
|
|
30
54
|
'HEADER_SEARCH_PATHS' => '$(PODS_TARGET_SRCROOT)/cpp',
|
package/src/Canvas.tsx
CHANGED
|
@@ -45,12 +45,49 @@ export interface CanvasRef {
|
|
|
45
45
|
getNativeSurface: () => NativeCanvas;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
export type AndroidSurfaceType = "SurfaceView" | "TextureView";
|
|
49
|
+
|
|
50
|
+
export interface AndroidCanvasProps {
|
|
51
|
+
/**
|
|
52
|
+
* Backing view. Defaults to `SurfaceView` when the canvas is opaque and
|
|
53
|
+
* `TextureView` otherwise, which is the only pairing that composites
|
|
54
|
+
* correctly in React Native stacking order without further flags.
|
|
55
|
+
*/
|
|
56
|
+
surfaceType?: AndroidSurfaceType;
|
|
57
|
+
/**
|
|
58
|
+
* SurfaceView only: composite above every React Native view in the window,
|
|
59
|
+
* ignoring `zIndex`. Ignored for TextureView. Defaults to false.
|
|
60
|
+
*/
|
|
61
|
+
zOrderOnTop?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface CanvasProps extends ViewProps {
|
|
65
|
+
/**
|
|
66
|
+
* Defaults to true. Set to false to alpha-composite the canvas over the
|
|
67
|
+
* views behind it (pair it with `alphaMode: "premultiplied"` and an alpha-0
|
|
68
|
+
* clear color). Android and web only; on iOS `alphaMode` alone controls it.
|
|
69
|
+
*/
|
|
70
|
+
opaque?: boolean;
|
|
71
|
+
/** Android-only rendering options. Ignored on iOS and web. */
|
|
72
|
+
android?: AndroidCanvasProps;
|
|
50
73
|
ref?: React.Ref<CanvasRef>;
|
|
51
74
|
}
|
|
52
75
|
|
|
53
|
-
|
|
76
|
+
// Anything else reaching the native component would hit the generated
|
|
77
|
+
// string-enum parser, which aborts on unknown values.
|
|
78
|
+
const resolveSurfaceType = (
|
|
79
|
+
surfaceType: AndroidSurfaceType | undefined,
|
|
80
|
+
): "auto" | AndroidSurfaceType =>
|
|
81
|
+
surfaceType === "SurfaceView" || surfaceType === "TextureView"
|
|
82
|
+
? surfaceType
|
|
83
|
+
: "auto";
|
|
84
|
+
|
|
85
|
+
export const Canvas = ({
|
|
86
|
+
opaque = true,
|
|
87
|
+
android,
|
|
88
|
+
ref,
|
|
89
|
+
...props
|
|
90
|
+
}: CanvasProps) => {
|
|
54
91
|
const viewRef = useRef(null);
|
|
55
92
|
const [contextId, _] = useState(() => generateContextId());
|
|
56
93
|
// Retire the native registry entry for this contextId on unmount. When a
|
|
@@ -95,7 +132,9 @@ export const Canvas = ({ transparent, ref, ...props }: CanvasProps) => {
|
|
|
95
132
|
<WebGPUNativeView
|
|
96
133
|
style={{ flex: 1 }}
|
|
97
134
|
contextId={contextId}
|
|
98
|
-
|
|
135
|
+
opaque={opaque}
|
|
136
|
+
androidSurfaceType={resolveSurfaceType(android?.surfaceType)}
|
|
137
|
+
androidZOrderOnTop={!!android?.zOrderOnTop}
|
|
99
138
|
/>
|
|
100
139
|
</View>
|
|
101
140
|
);
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { codegenNativeComponent } from "react-native";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
Int32,
|
|
4
|
+
WithDefault,
|
|
5
|
+
} from "react-native/Libraries/Types/CodegenTypes";
|
|
3
6
|
import type { ViewProps } from "react-native";
|
|
4
7
|
|
|
5
8
|
export interface NativeProps extends ViewProps {
|
|
6
9
|
contextId: Int32;
|
|
7
|
-
|
|
10
|
+
opaque?: WithDefault<boolean, true>;
|
|
11
|
+
androidSurfaceType?: WithDefault<
|
|
12
|
+
"auto" | "SurfaceView" | "TextureView",
|
|
13
|
+
"auto"
|
|
14
|
+
>;
|
|
15
|
+
androidZOrderOnTop?: WithDefault<boolean, false>;
|
|
8
16
|
}
|
|
9
17
|
|
|
10
18
|
// eslint-disable-next-line import/no-default-export
|
|
@@ -7,14 +7,23 @@ import { contextIdToId } from "./utils";
|
|
|
7
7
|
|
|
8
8
|
export interface NativeProps extends ViewProps {
|
|
9
9
|
contextId: Int32;
|
|
10
|
-
|
|
10
|
+
opaque?: boolean;
|
|
11
|
+
androidSurfaceType?: "auto" | "SurfaceView" | "TextureView";
|
|
12
|
+
androidZOrderOnTop?: boolean;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
// eslint-disable-next-line import/no-default-export
|
|
14
16
|
export default function WebGPUViewNativeComponent(
|
|
15
17
|
props: NativeProps,
|
|
16
18
|
): React.JSX.Element {
|
|
17
|
-
const {
|
|
19
|
+
const {
|
|
20
|
+
contextId,
|
|
21
|
+
style,
|
|
22
|
+
opaque = true,
|
|
23
|
+
androidSurfaceType: _androidSurfaceType,
|
|
24
|
+
androidZOrderOnTop: _androidZOrderOnTop,
|
|
25
|
+
...rest
|
|
26
|
+
} = props;
|
|
18
27
|
|
|
19
28
|
// MakeWebGPUCanvasContext sets the initial drawing-buffer size. Subsequent
|
|
20
29
|
// resizes belong to the renderer so it can recreate depth/MSAA attachments
|
|
@@ -25,7 +34,7 @@ export default function WebGPUViewNativeComponent(
|
|
|
25
34
|
style: {
|
|
26
35
|
...styles.view,
|
|
27
36
|
...styles.flex1,
|
|
28
|
-
...(
|
|
37
|
+
...(opaque ? { backgroundColor: "white" } : {}),
|
|
29
38
|
...(typeof style === "object" ? style : {}),
|
|
30
39
|
},
|
|
31
40
|
});
|
package/src/index.tsx
CHANGED
|
@@ -91,6 +91,13 @@ declare global {
|
|
|
91
91
|
// device creation: adapter.requestDevice({ dawnToggles: { ... } }).
|
|
92
92
|
interface GPUDeviceDescriptor {
|
|
93
93
|
dawnToggles?: GPUDawnTogglesDescriptor;
|
|
94
|
+
// Non-spec RN extension. Dawn's "implicit-device-synchronization" feature
|
|
95
|
+
// is requested by default so one device can be handed from the JS thread
|
|
96
|
+
// to a worklet runtime; it locks device and child-object calls with a
|
|
97
|
+
// per-device mutex (command encoding excluded: never share an encoder
|
|
98
|
+
// across threads). Setting this to false disables the locking; only do so
|
|
99
|
+
// for a device that will never be touched from a worklet runtime.
|
|
100
|
+
implicitDeviceSynchronization?: boolean;
|
|
94
101
|
}
|
|
95
102
|
// Non-spec extension: camera frames arrive in the sensor's native
|
|
96
103
|
// orientation, which differs between iOS and Android. `rotation` (degrees,
|
package/src/mock.ts
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
import React from "react";
|
|
3
3
|
import type { ReactNode } from "react";
|
|
4
4
|
import { View } from "react-native";
|
|
5
|
-
import type { ViewProps } from "react-native";
|
|
6
5
|
|
|
7
|
-
import type { NativeCanvas, RNCanvasContext } from "./Canvas";
|
|
6
|
+
import type { CanvasProps, NativeCanvas, RNCanvasContext } from "./Canvas";
|
|
8
7
|
|
|
9
8
|
// Jest mock for `react-native-webgpu`.
|
|
10
9
|
//
|
|
@@ -23,15 +22,19 @@ export * from "./formats";
|
|
|
23
22
|
const NOT_AVAILABLE_UNDER_JEST =
|
|
24
23
|
"is not available under Jest - react-native-webgpu has no native implementation in tests (see react-native-webgpu/jestSetup.js)";
|
|
25
24
|
|
|
26
|
-
export type {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
export type {
|
|
26
|
+
AndroidCanvasProps,
|
|
27
|
+
AndroidSurfaceType,
|
|
28
|
+
CanvasProps,
|
|
29
|
+
CanvasRef,
|
|
30
|
+
NativeCanvas,
|
|
31
|
+
RNCanvasContext,
|
|
32
|
+
} from "./Canvas";
|
|
32
33
|
|
|
34
|
+
// Strip the same props the real Canvas keeps off its outer View.
|
|
33
35
|
export const Canvas = ({
|
|
34
|
-
|
|
36
|
+
opaque: _opaque,
|
|
37
|
+
android: _android,
|
|
35
38
|
ref,
|
|
36
39
|
...props
|
|
37
40
|
}: CanvasProps) => {
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <jsi/jsi.h>
|
|
4
|
-
|
|
5
|
-
#include <atomic>
|
|
6
|
-
#include <cassert>
|
|
7
|
-
#include <cstdint>
|
|
8
|
-
#include <memory>
|
|
9
|
-
#include <mutex>
|
|
10
|
-
#include <unordered_map>
|
|
11
|
-
#include <utility>
|
|
12
|
-
|
|
13
|
-
#include "RuntimeLifecycleMonitor.h"
|
|
14
|
-
|
|
15
|
-
namespace rnwgpu {
|
|
16
|
-
|
|
17
|
-
namespace jsi = facebook::jsi;
|
|
18
|
-
|
|
19
|
-
class BaseRuntimeAwareCache {
|
|
20
|
-
public:
|
|
21
|
-
static void setMainJsRuntime(jsi::Runtime *rt) {
|
|
22
|
-
// Atomic: hot reload rewrites this from the JS thread while worklet
|
|
23
|
-
// runtime threads read it concurrently.
|
|
24
|
-
_mainRuntime.store(rt, std::memory_order_release);
|
|
25
|
-
// Every (re)installation of the native module starts a new generation.
|
|
26
|
-
// Callers that hold process-wide state derived from the main runtime
|
|
27
|
-
// (see StaticRuntimeAwareCache) must key their invalidation on this
|
|
28
|
-
// counter and NOT on the runtime pointer: when the JS runtime is
|
|
29
|
-
// destroyed and recreated in-process (expo-updates OTA apply,
|
|
30
|
-
// DevSettings.reload) the new jsi::Runtime is frequently allocated at
|
|
31
|
-
// the exact address of the freed one, so a pointer comparison cannot
|
|
32
|
-
// tell a reload from the steady state.
|
|
33
|
-
_mainRuntimeGeneration.fetch_add(1, std::memory_order_acq_rel);
|
|
34
|
-
}
|
|
35
|
-
static uint64_t getMainJsRuntimeGeneration() {
|
|
36
|
-
return _mainRuntimeGeneration.load(std::memory_order_acquire);
|
|
37
|
-
}
|
|
38
|
-
static jsi::Runtime *getMainJsRuntime() {
|
|
39
|
-
auto *rt = _mainRuntime.load(std::memory_order_acquire);
|
|
40
|
-
assert(rt != nullptr &&
|
|
41
|
-
"Expected main Javascript runtime to be set in the "
|
|
42
|
-
"BaseRuntimeAwareCache class.");
|
|
43
|
-
|
|
44
|
-
return rt;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private:
|
|
48
|
-
static std::atomic<jsi::Runtime *> _mainRuntime;
|
|
49
|
-
static std::atomic<uint64_t> _mainRuntimeGeneration;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Provides a way to keep data specific to a jsi::Runtime instance that gets
|
|
54
|
-
* cleaned up when that runtime is destroyed. This is necessary because JSI does
|
|
55
|
-
* not allow for its associated objects to be retained past the runtime
|
|
56
|
-
* lifetime. If an object (e.g. jsi::Values or jsi::Function instances) is kept
|
|
57
|
-
* after the runtime is torn down, its destructor (once it is destroyed
|
|
58
|
-
* eventually) will result in a crash (JSI objects keep a pointer to memory
|
|
59
|
-
* managed by the runtime, accessing that portion of the memory after runtime is
|
|
60
|
-
* deleted is the root cause of that crash).
|
|
61
|
-
*
|
|
62
|
-
* In order to provide an efficient implementation that does not add an overhead
|
|
63
|
-
* for the cases when only a single runtime is used, which is the primary
|
|
64
|
-
* usecase, the following assumption has been made: Only for secondary runtimes
|
|
65
|
-
* we track destruction and clean up the store associated with that runtime. For
|
|
66
|
-
* the first runtime we assume that the object holding the store is destroyed
|
|
67
|
-
* prior to the destruction of that runtime.
|
|
68
|
-
*
|
|
69
|
-
* The above assumption makes it work without any overhead when only single
|
|
70
|
-
* runtime is in use. Specifically, we don't perform any additional operations
|
|
71
|
-
* related to tracking runtime lifecycle when only a single runtime is used.
|
|
72
|
-
*/
|
|
73
|
-
template <typename T>
|
|
74
|
-
class RuntimeAwareCache : public BaseRuntimeAwareCache,
|
|
75
|
-
public RuntimeLifecycleListener {
|
|
76
|
-
|
|
77
|
-
public:
|
|
78
|
-
void onRuntimeDestroyed(jsi::Runtime *rt) override {
|
|
79
|
-
if (getMainJsRuntime() != rt) {
|
|
80
|
-
// We are removing a secondary runtime. This is invoked by
|
|
81
|
-
// RuntimeLifecycleMonitor on the destroyed runtime's thread, which may
|
|
82
|
-
// run concurrently with get() on another runtime's thread.
|
|
83
|
-
std::lock_guard<std::mutex> lock(_secondaryCachesMutex);
|
|
84
|
-
_secondaryRuntimeCaches.erase(rt);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
~RuntimeAwareCache() {
|
|
89
|
-
for (auto &cache : _secondaryRuntimeCaches) {
|
|
90
|
-
RuntimeLifecycleMonitor::removeListener(
|
|
91
|
-
*static_cast<jsi::Runtime *>(cache.first), this);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
T &get(jsi::Runtime &rt) {
|
|
96
|
-
// We check if we're accessing the main runtime - this is the happy path
|
|
97
|
-
// to avoid us having to lookup by runtime for caches that only has a single
|
|
98
|
-
// runtime
|
|
99
|
-
if (getMainJsRuntime() == &rt) {
|
|
100
|
-
return _primaryCache;
|
|
101
|
-
} else {
|
|
102
|
-
// Guard the secondary map: it can be mutated concurrently by get()
|
|
103
|
-
// on a worklet runtime's thread and by onRuntimeDestroyed() when
|
|
104
|
-
// another secondary runtime is torn down. The main-runtime path above
|
|
105
|
-
// stays lock-free. References into the map remain valid after the
|
|
106
|
-
// lock is released (unordered_map never invalidates references on
|
|
107
|
-
// insert/erase of other keys, and this runtime's entry can only be
|
|
108
|
-
// erased from this runtime's own thread).
|
|
109
|
-
std::lock_guard<std::mutex> lock(_secondaryCachesMutex);
|
|
110
|
-
if (_secondaryRuntimeCaches.count(&rt) == 0) {
|
|
111
|
-
// we only add listener when the secondary runtime is used, this assumes
|
|
112
|
-
// that the secondary runtime is terminated first. This lets us avoid
|
|
113
|
-
// additional complexity for the majority of cases when objects are not
|
|
114
|
-
// shared between runtimes. Otherwise we'd have to register all objecrts
|
|
115
|
-
// with the RuntimeMonitor as opposed to only registering ones that are
|
|
116
|
-
// used in secondary runtime. Note that we can't register listener here
|
|
117
|
-
// with the primary runtime as it may run on a separate thread.
|
|
118
|
-
RuntimeLifecycleMonitor::addListener(rt, this);
|
|
119
|
-
|
|
120
|
-
T cache;
|
|
121
|
-
_secondaryRuntimeCaches.emplace(&rt, std::move(cache));
|
|
122
|
-
}
|
|
123
|
-
return _secondaryRuntimeCaches.at(&rt);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private:
|
|
128
|
-
std::mutex _secondaryCachesMutex;
|
|
129
|
-
std::unordered_map<void *, T> _secondaryRuntimeCaches;
|
|
130
|
-
T _primaryCache;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
} // namespace rnwgpu
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
#include "RuntimeLifecycleMonitor.h"
|
|
2
|
-
|
|
3
|
-
#include <mutex>
|
|
4
|
-
#include <unordered_map>
|
|
5
|
-
#include <unordered_set>
|
|
6
|
-
#include <utility>
|
|
7
|
-
|
|
8
|
-
namespace rnwgpu {
|
|
9
|
-
|
|
10
|
-
static std::unordered_map<jsi::Runtime *,
|
|
11
|
-
std::unordered_set<RuntimeLifecycleListener *>>
|
|
12
|
-
listeners;
|
|
13
|
-
static std::mutex listenersMutex;
|
|
14
|
-
|
|
15
|
-
struct RuntimeLifecycleMonitorObject : public jsi::HostObject {
|
|
16
|
-
jsi::Runtime *_rt;
|
|
17
|
-
explicit RuntimeLifecycleMonitorObject(jsi::Runtime *rt) : _rt(rt) {}
|
|
18
|
-
~RuntimeLifecycleMonitorObject() {
|
|
19
|
-
std::unordered_set<RuntimeLifecycleListener *> listenersCopy;
|
|
20
|
-
{
|
|
21
|
-
std::lock_guard<std::mutex> lock(listenersMutex);
|
|
22
|
-
auto listenersSet = listeners.find(_rt);
|
|
23
|
-
if (listenersSet != listeners.end()) {
|
|
24
|
-
listenersCopy = listenersSet->second;
|
|
25
|
-
listeners.erase(listenersSet);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
for (auto listener : listenersCopy) {
|
|
29
|
-
listener->onRuntimeDestroyed(_rt);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
void RuntimeLifecycleMonitor::addListener(jsi::Runtime &rt,
|
|
35
|
-
RuntimeLifecycleListener *listener) {
|
|
36
|
-
bool shouldInstallMonitor = false;
|
|
37
|
-
{
|
|
38
|
-
std::lock_guard<std::mutex> lock(listenersMutex);
|
|
39
|
-
auto listenersSet = listeners.find(&rt);
|
|
40
|
-
if (listenersSet == listeners.end()) {
|
|
41
|
-
std::unordered_set<RuntimeLifecycleListener *> newSet;
|
|
42
|
-
newSet.insert(listener);
|
|
43
|
-
listeners.emplace(&rt, std::move(newSet));
|
|
44
|
-
shouldInstallMonitor = true;
|
|
45
|
-
} else {
|
|
46
|
-
listenersSet->second.insert(listener);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
if (shouldInstallMonitor) {
|
|
50
|
-
// We install a global host object in the provided runtime, this way we can
|
|
51
|
-
// use that host object destructor to get notified when the runtime is being
|
|
52
|
-
// terminated. We use a unique name for the object as it gets saved with the
|
|
53
|
-
// runtime's global object.
|
|
54
|
-
rt.global().setProperty(
|
|
55
|
-
rt, "__rnwgpu_rt_lifecycle_monitor",
|
|
56
|
-
jsi::Object::createFromHostObject(
|
|
57
|
-
rt, std::make_shared<RuntimeLifecycleMonitorObject>(&rt)));
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
void RuntimeLifecycleMonitor::removeListener(
|
|
62
|
-
jsi::Runtime &rt, RuntimeLifecycleListener *listener) {
|
|
63
|
-
std::lock_guard<std::mutex> lock(listenersMutex);
|
|
64
|
-
auto listenersSet = listeners.find(&rt);
|
|
65
|
-
if (listenersSet == listeners.end()) {
|
|
66
|
-
// nothing to do here
|
|
67
|
-
} else {
|
|
68
|
-
listenersSet->second.erase(listener);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
} // namespace rnwgpu
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <jsi/jsi.h>
|
|
4
|
-
|
|
5
|
-
#include <memory>
|
|
6
|
-
|
|
7
|
-
namespace rnwgpu {
|
|
8
|
-
|
|
9
|
-
namespace jsi = facebook::jsi;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Listener interface that allows for getting notified when a jsi::Runtime
|
|
13
|
-
* instance is destroyed.
|
|
14
|
-
*/
|
|
15
|
-
struct RuntimeLifecycleListener {
|
|
16
|
-
virtual ~RuntimeLifecycleListener() {}
|
|
17
|
-
virtual void onRuntimeDestroyed(jsi::Runtime *) = 0;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* This class provides an API via static methods for registering and
|
|
22
|
-
* unregistering runtime lifecycle listeners. The listeners can be used to
|
|
23
|
-
* cleanup any data that references a given jsi::Runtime instance before it gets
|
|
24
|
-
* destroyed.
|
|
25
|
-
*/
|
|
26
|
-
struct RuntimeLifecycleMonitor {
|
|
27
|
-
static void addListener(jsi::Runtime &rt, RuntimeLifecycleListener *listener);
|
|
28
|
-
static void removeListener(jsi::Runtime &rt,
|
|
29
|
-
RuntimeLifecycleListener *listener);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
} // namespace rnwgpu
|