react-native-wgpu 0.3.0 → 0.3.2

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.
@@ -46,8 +46,7 @@ add_library(${PACKAGE_NAME} SHARED
46
46
  ../cpp/rnwgpu/RNWebGPUManager.cpp
47
47
  ../cpp/jsi/RNFPromise.cpp
48
48
  ../cpp/jsi/RNFHybridObject.cpp
49
- ../cpp/jsi/RNFRuntimeCache.cpp
50
- ../cpp/jsi/RNFWorkletRuntimeRegistry.cpp
49
+ ../cpp/jsi/RNFRuntimeState.cpp
51
50
  ../cpp/rnwgpu/async/AsyncRunner.cpp
52
51
  ../cpp/rnwgpu/async/AsyncTaskHandle.cpp
53
52
  ../cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp
@@ -1,5 +1,5 @@
1
1
  #import "MetalView.h"
2
- #import "webgpu_cpp.h"
2
+ #import "webgpu/webgpu_cpp.h"
3
3
 
4
4
  @implementation MetalView {
5
5
  BOOL _isConfigured;
@@ -129,6 +129,7 @@ void HybridObject::ensureInitialized(facebook::jsi::Runtime& runtime) {
129
129
  if (!_didLoadMethods) {
130
130
  [[unlikely]];
131
131
  _creationRuntime = &runtime;
132
+ _runtimeState = rnwgpu::RNFRuntimeState::get(runtime);
132
133
  // lazy-load all exposed methods
133
134
  loadHybridMethods();
134
135
  _didLoadMethods = true;
@@ -136,11 +137,14 @@ void HybridObject::ensureInitialized(facebook::jsi::Runtime& runtime) {
136
137
  }
137
138
 
138
139
  bool HybridObject::isRuntimeAlive() {
139
- if (_creationRuntime == nullptr) {
140
- [[unlikely]];
141
- return false;
140
+ return !_runtimeState.expired();
141
+ }
142
+
143
+ facebook::jsi::Runtime* HybridObject::getCreationRuntime() const {
144
+ if (_runtimeState.expired()) {
145
+ return nullptr;
142
146
  }
143
- return RNFWorkletRuntimeRegistry::isRuntimeAlive(_creationRuntime);
147
+ return _creationRuntime;
144
148
  }
145
149
 
146
150
  } // namespace margelo
@@ -5,7 +5,7 @@
5
5
  #pragma once
6
6
 
7
7
  #include "WGPULogger.h"
8
- #include "RNFWorkletRuntimeRegistry.h"
8
+ #include "RNFRuntimeState.h"
9
9
  #include <functional>
10
10
  #include <jsi/jsi.h>
11
11
  #include <memory>
@@ -91,8 +91,9 @@ private:
91
91
 
92
92
  protected:
93
93
  const char* _name = TAG;
94
- // Store a pointer to the runtime. Needed for checking if the runtime is still active, see WorkletRuntimeRegistry.
94
+ // Store a pointer to the runtime for convenience; ensure it is only used while the runtime state is alive.
95
95
  jsi::Runtime* _creationRuntime = nullptr;
96
+ std::weak_ptr<rnwgpu::RNFRuntimeState> _runtimeState;
96
97
 
97
98
  private:
98
99
  inline void ensureInitialized(facebook::jsi::Runtime& runtime);
@@ -128,6 +129,10 @@ private:
128
129
  };
129
130
  }
130
131
 
132
+ protected:
133
+ facebook::jsi::Runtime* getCreationRuntime() const;
134
+ std::weak_ptr<rnwgpu::RNFRuntimeState> getRuntimeStateWeak() const { return _runtimeState; }
135
+
131
136
  protected:
132
137
  template <typename Derived, typename ReturnType, typename... Args>
133
138
  void registerHybridMethod(std::string name, ReturnType (Derived::*method)(Args...), Derived* derivedInstance, bool override = false) {
@@ -22,7 +22,6 @@
22
22
  #include "RNFEnumMapper.h"
23
23
  #include "RNFJSIHelper.h"
24
24
  #include "RNFPromise.h"
25
- #include "RNFWorkletRuntimeRegistry.h"
26
25
 
27
26
  #include "Unions.h"
28
27
 
@@ -225,52 +224,6 @@ template <> struct JSIConverter<rnwgpu::async::AsyncTaskHandle> {
225
224
  }
226
225
  };
227
226
 
228
-
229
- // [](Args...) -> T {} <> (Args...) => T
230
- template <typename ReturnType, typename... Args> struct JSIConverter<std::function<ReturnType(Args...)>> {
231
- static std::function<ReturnType(Args...)> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
232
- jsi::Function function = arg.asObject(runtime).asFunction(runtime);
233
-
234
- std::shared_ptr<jsi::Function> sharedFunction = JSIHelper::createSharedJsiFunction(runtime, std::move(function));
235
- return [&runtime, sharedFunction, outOfBound](Args... args) -> ReturnType {
236
- jsi::Value result = sharedFunction->call(runtime, JSIConverter<std::decay_t<Args>>::toJSI(runtime, args)...);
237
- if constexpr (std::is_same_v<ReturnType, void>) {
238
- // it is a void function (returns undefined)
239
- return;
240
- } else {
241
- // it returns a custom type, parse it from the JSI value.
242
- return JSIConverter<ReturnType>::fromJSI(runtime, std::move(result), outOfBound);
243
- }
244
- };
245
- }
246
-
247
- template <size_t... Is>
248
- static jsi::Value callHybridFunction(const std::function<ReturnType(Args...)>& function, jsi::Runtime& runtime, const jsi::Value* args,
249
- std::index_sequence<Is...>, size_t count) {
250
- if constexpr (std::is_same_v<ReturnType, void>) {
251
- // it is a void function (will return undefined in JS)
252
- function(JSIConverter<std::decay_t<Args>>::fromJSI(runtime, args[Is], Is >= count)...);
253
- return jsi::Value::undefined();
254
- } else {
255
- // it is a custom type, parse it to a JS value
256
- ReturnType result = function(JSIConverter<std::decay_t<Args>>::fromJSI(runtime, args[Is], Is >= count)...);
257
- return JSIConverter<ReturnType>::toJSI(runtime, result);
258
- }
259
- }
260
- static jsi::Value toJSI(jsi::Runtime& runtime, const std::function<ReturnType(Args...)>& function) {
261
- jsi::HostFunctionType jsFunction = [function = std::move(function)](jsi::Runtime& runtime, const jsi::Value& thisValue,
262
- const jsi::Value* args, size_t count) -> jsi::Value {
263
- if (count != sizeof...(Args)) {
264
- [[unlikely]];
265
- throw jsi::JSError(runtime, "Function expected " + std::to_string(sizeof...(Args)) + " arguments, but received " +
266
- std::to_string(count) + "!");
267
- }
268
- return callHybridFunction(function, runtime, args, std::index_sequence_for<Args...>{}, count);
269
- };
270
- return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "hostFunction"), sizeof...(Args), jsFunction);
271
- }
272
- };
273
-
274
227
  // std::map<std::string, T> <> Record<string, T>
275
228
  template <typename ValueType> struct JSIConverter<std::map<std::string, ValueType>> {
276
229
  static std::map<std::string, ValueType> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg, bool outOfBound) {
@@ -4,7 +4,7 @@
4
4
 
5
5
  #pragma once
6
6
 
7
- #include "RNFWorkletRuntimeRegistry.h"
7
+ #include "RNFRuntimeState.h"
8
8
  #include <jsi/jsi.h>
9
9
  #include <memory>
10
10
  #include <utility>
@@ -20,8 +20,10 @@ public:
20
20
  * Every jsi::Function you intend to share or hold should be wrapped using this function.
21
21
  */
22
22
  static std::shared_ptr<jsi::Function> createSharedJsiFunction(jsi::Runtime& runtime, jsi::Function&& function) {
23
- std::shared_ptr<jsi::Function> sharedFunction(new jsi::Function(std::move(function)), [&runtime](jsi::Function* ptr) {
24
- if (RNFWorkletRuntimeRegistry::isRuntimeAlive(&runtime)) {
23
+ auto runtimeState = rnwgpu::RNFRuntimeState::get(runtime);
24
+ std::weak_ptr<rnwgpu::RNFRuntimeState> runtimeStateWeak = runtimeState;
25
+ std::shared_ptr<jsi::Function> sharedFunction(new jsi::Function(std::move(function)), [runtimeStateWeak](jsi::Function* ptr) {
26
+ if (!runtimeStateWeak.expired()) {
25
27
  // Only delete the jsi::Function when the runtime it created is still alive.
26
28
  // Otherwise leak memory. We do this on purpose, as sometimes we would keep
27
29
  // references to JSI objects past the lifetime of its runtime (e.g.,
@@ -0,0 +1,18 @@
1
+ #include "RNFRuntimeState.h"
2
+
3
+ namespace rnwgpu {
4
+
5
+ const facebook::jsi::UUID RNFRuntimeState::kRuntimeStateKey = facebook::jsi::UUID();
6
+
7
+ std::shared_ptr<RNFRuntimeState> RNFRuntimeState::get(facebook::jsi::Runtime& runtime) {
8
+ auto existing = runtime.getRuntimeData(kRuntimeStateKey);
9
+ if (existing) {
10
+ return std::static_pointer_cast<RNFRuntimeState>(existing);
11
+ }
12
+
13
+ auto state = std::shared_ptr<RNFRuntimeState>(new RNFRuntimeState());
14
+ runtime.setRuntimeData(kRuntimeStateKey, state);
15
+ return state;
16
+ }
17
+
18
+ } // namespace rnwgpu
@@ -0,0 +1,106 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ #include <memory>
6
+ #include <mutex>
7
+ #include <unordered_map>
8
+ #include <unordered_set>
9
+
10
+ namespace rnwgpu {
11
+
12
+ namespace jsi = facebook::jsi;
13
+
14
+ /**
15
+ * Runtime state management using Hermes' runtime.setRuntimeData API.
16
+ * This replaces the old RuntimeLifecycleMonitor/RuntimeAwareCache system.
17
+ */
18
+ class RNFRuntimeState {
19
+ public:
20
+ // UUID key for storing our runtime state
21
+ static const jsi::UUID kRuntimeStateKey;
22
+
23
+ /**
24
+ * Get or create the runtime state for the given runtime
25
+ */
26
+ static std::shared_ptr<RNFRuntimeState> get(jsi::Runtime& runtime);
27
+
28
+ /**
29
+ * Template cache that can store any type T per object pointer
30
+ */
31
+ template <typename T>
32
+ class ObjectCache {
33
+ public:
34
+ std::shared_ptr<T> getOrCreate(void* object) {
35
+ std::lock_guard<std::mutex> lock(mutex_);
36
+ auto it = cache_.find(object);
37
+ if (it != cache_.end()) {
38
+ return it->second;
39
+ }
40
+
41
+ auto value = std::make_shared<T>();
42
+ cache_[object] = value;
43
+ return value;
44
+ }
45
+
46
+ void remove(void* object) {
47
+ std::lock_guard<std::mutex> lock(mutex_);
48
+ cache_.erase(object);
49
+ }
50
+
51
+ void clear() {
52
+ std::lock_guard<std::mutex> lock(mutex_);
53
+ cache_.clear();
54
+ }
55
+
56
+ private:
57
+ std::mutex mutex_;
58
+ std::unordered_map<void*, std::shared_ptr<T>> cache_;
59
+ };
60
+
61
+ /**
62
+ * Get or create a cache for a specific type T
63
+ */
64
+ template <typename T>
65
+ std::shared_ptr<ObjectCache<T>> getCache() {
66
+ std::lock_guard<std::mutex> lock(mutex_);
67
+
68
+ // Use type_info as key for the cache type
69
+ const std::type_info& typeId = typeid(T);
70
+ auto it = typeCaches_.find(&typeId);
71
+
72
+ if (it != typeCaches_.end()) {
73
+ return std::static_pointer_cast<ObjectCache<T>>(it->second);
74
+ }
75
+
76
+ auto cache = std::make_shared<ObjectCache<T>>();
77
+ typeCaches_[&typeId] = cache;
78
+ return cache;
79
+ }
80
+
81
+ private:
82
+ RNFRuntimeState() = default;
83
+
84
+ std::mutex mutex_;
85
+ // Map from type_info to cache instance
86
+ std::unordered_map<const std::type_info*, std::shared_ptr<void>> typeCaches_;
87
+ };
88
+
89
+ /**
90
+ * Template helper for runtime-aware caching compatible with the old API
91
+ * This provides a migration path from RuntimeAwareCache
92
+ */
93
+ template <typename T>
94
+ class RuntimeAwareCache {
95
+ public:
96
+ T& get(jsi::Runtime& rt) {
97
+ auto state = RNFRuntimeState::get(rt);
98
+ auto cache = state->getCache<T>();
99
+
100
+ // For compatibility, we use the runtime pointer as the object key
101
+ auto ptr = cache->getOrCreate(&rt);
102
+ return *ptr;
103
+ }
104
+ };
105
+
106
+ } // namespace rnwgpu
@@ -12,7 +12,6 @@
12
12
  #include "rnwgpu/async/AsyncRunner.h"
13
13
  #include "rnwgpu/async/AsyncTaskHandle.h"
14
14
 
15
- #include "dawn/native/DawnNative.h"
16
15
  #include "webgpu/webgpu_cpp.h"
17
16
 
18
17
  #include "GPUAdapter.h"
@@ -79,16 +79,17 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
79
79
  std::string label =
80
80
  descriptor.has_value() ? descriptor.value()->label.value_or("") : "";
81
81
 
82
+ auto creationRuntime = getCreationRuntime();
82
83
  return _async->postTask(
83
84
  [this, aDescriptor, descriptor, label = std::move(label),
84
- deviceLostBinding](
85
+ deviceLostBinding, creationRuntime](
85
86
  const async::AsyncTaskHandle::ResolveFunction &resolve,
86
87
  const async::AsyncTaskHandle::RejectFunction &reject) {
87
88
  (void)descriptor;
88
89
  _instance.RequestDevice(
89
90
  &aDescriptor, wgpu::CallbackMode::AllowProcessEvents,
90
91
  [asyncRunner = _async, resolve, reject, label,
91
- creationRuntime = _creationRuntime, deviceLostBinding](
92
+ creationRuntime, deviceLostBinding](
92
93
  wgpu::RequestDeviceStatus status, wgpu::Device device,
93
94
  wgpu::StringView message) mutable {
94
95
  if (message.length) {
@@ -106,6 +107,9 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
106
107
  device.SetLoggingCallback([creationRuntime](
107
108
  wgpu::LoggingType type,
108
109
  wgpu::StringView msg) {
110
+ if (creationRuntime == nullptr) {
111
+ return;
112
+ }
109
113
  const char *logLevel = "";
110
114
  switch (type) {
111
115
  case wgpu::LoggingType::Warning: