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.
@@ -1,57 +0,0 @@
1
- //
2
- // Created by Marc Rousavy on 22.02.24.
3
- //
4
- #include <jsi/jsi.h>
5
-
6
- #include <memory>
7
- #include <unordered_map>
8
- #include <unordered_set>
9
- #include <utility>
10
-
11
- #include "RNFRuntimeCache.h"
12
-
13
- namespace margelo {
14
-
15
- static std::unordered_map<jsi::Runtime*, std::unordered_set<RuntimeLifecycleListener*>> listeners;
16
-
17
- struct RuntimeLifecycleMonitorObject : public jsi::HostObject {
18
- jsi::Runtime* _rt;
19
- explicit RuntimeLifecycleMonitorObject(jsi::Runtime* rt) : _rt(rt) {}
20
- ~RuntimeLifecycleMonitorObject() {
21
- auto listenersSet = listeners.find(_rt);
22
- if (listenersSet != listeners.end()) {
23
- for (auto listener : listenersSet->second) {
24
- listener->onRuntimeDestroyed(_rt);
25
- }
26
- listeners.erase(listenersSet);
27
- }
28
- }
29
- };
30
-
31
- void RuntimeLifecycleMonitor::addListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener) {
32
- auto listenersSet = listeners.find(&rt);
33
- if (listenersSet == listeners.end()) {
34
- // We install a global host object in the provided runtime, this way we can
35
- // use that host object destructor to get notified when the runtime is being
36
- // terminated. We use a unique name for the object as it gets saved with the
37
- // runtime's global object.
38
- rt.global().setProperty(rt, "__rnfl_rt_lifecycle_monitor",
39
- jsi::Object::createFromHostObject(rt, std::make_shared<RuntimeLifecycleMonitorObject>(&rt)));
40
- std::unordered_set<RuntimeLifecycleListener*> newSet;
41
- newSet.insert(listener);
42
- listeners.emplace(&rt, std::move(newSet));
43
- } else {
44
- listenersSet->second.insert(listener);
45
- }
46
- }
47
-
48
- void RuntimeLifecycleMonitor::removeListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener) {
49
- auto listenersSet = listeners.find(&rt);
50
- if (listenersSet == listeners.end()) {
51
- // nothing to do here
52
- } else {
53
- listenersSet->second.erase(listener);
54
- }
55
- }
56
-
57
- } // namespace margelo
@@ -1,79 +0,0 @@
1
- //
2
- // Created by Marc Rousavy on 22.02.24.
3
- //
4
- #pragma once
5
-
6
- #include <jsi/jsi.h>
7
-
8
- #include <memory>
9
- #include <unordered_map>
10
- #include <unordered_set>
11
- #include <utility>
12
-
13
- namespace margelo {
14
-
15
- namespace jsi = facebook::jsi;
16
-
17
- /**
18
- * Listener interface that allows for getting notified when a jsi::Runtime
19
- * instance is destroyed.
20
- */
21
- struct RuntimeLifecycleListener {
22
- virtual ~RuntimeLifecycleListener() {}
23
- virtual void onRuntimeDestroyed(jsi::Runtime*) = 0;
24
- };
25
-
26
- /**
27
- * This class provides an API via static methods for registering and
28
- * unregistering runtime lifecycle listeners. The listeners can be used to
29
- * cleanup any data that references a given jsi::Runtime instance before it gets
30
- * destroyed.
31
- */
32
- struct RuntimeLifecycleMonitor {
33
- static void addListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener);
34
- static void removeListener(jsi::Runtime& rt, RuntimeLifecycleListener* listener);
35
- };
36
-
37
- /**
38
- * Provides a way to keep data specific to a jsi::Runtime instance that gets
39
- * cleaned up when that runtime is destroyed. This is necessary because JSI does
40
- * not allow for its associated objects to be retained past the runtime
41
- * lifetime. If an object (e.g. jsi::Values or jsi::Function instances) is kept
42
- * after the runtime is torn down, its destructor (once it is destroyed
43
- * eventually) will result in a crash (JSI objects keep a pointer to memory
44
- * managed by the runtime, accessing that portion of the memory after runtime is
45
- * deleted is the root cause of that crash).
46
- */
47
- template <typename T> class RuntimeAwareCache : public RuntimeLifecycleListener {
48
-
49
- public:
50
- void onRuntimeDestroyed(jsi::Runtime* rt) override {
51
- // A runtime has been destroyed, so destroy the related cache.
52
- _runtimeCaches.erase(rt);
53
- }
54
-
55
- ~RuntimeAwareCache() {
56
- for (auto& cache : _runtimeCaches) {
57
- // remove all `onRuntimeDestroyed` listeners.
58
- RuntimeLifecycleMonitor::removeListener(*cache.first, this);
59
- }
60
- }
61
-
62
- T& get(jsi::Runtime& rt) {
63
- if (_runtimeCaches.count(&rt) == 0) {
64
- // This is the first time this Runtime has been accessed.
65
- // We set up a `onRuntimeDestroyed` listener for it and
66
- // initialize the cache map.
67
- RuntimeLifecycleMonitor::addListener(rt, this);
68
-
69
- T cache;
70
- _runtimeCaches.emplace(&rt, std::move(cache));
71
- }
72
- return _runtimeCaches.at(&rt);
73
- }
74
-
75
- private:
76
- std::unordered_map<jsi::Runtime*, T> _runtimeCaches;
77
- };
78
-
79
- } // namespace margelo
@@ -1,43 +0,0 @@
1
- //
2
- // Created by Marc Rousavy on 22.02.24.
3
- //
4
- #pragma once
5
-
6
- #include "RNFWorkletRuntimeRegistry.h"
7
-
8
- #include <jsi/jsi.h>
9
-
10
- #include <memory>
11
-
12
- namespace margelo {
13
-
14
- // From:
15
- // https://github.com/software-mansion/react-native-reanimated/blob/6cb1a66f1a68cac8079de2b6b305d22359847e51/Common/cpp/ReanimatedRuntime/WorkletRuntimeCollector.h
16
- class WorkletRuntimeCollector : public jsi::HostObject {
17
- // When worklet runtime is created, we inject an instance of this class as a
18
- // `jsi::HostObject` into the global object. When worklet runtime is
19
- // terminated, the object is garbage-collected, which runs the C++ destructor.
20
- // In the destructor, we unregister the worklet runtime from the registry.
21
-
22
- public:
23
- explicit WorkletRuntimeCollector(jsi::Runtime& runtime) : _runtime(runtime) {
24
- Logger::log("WorkletRuntimeCollector", "Registering WorkletRuntime %p", &runtime);
25
- RNFWorkletRuntimeRegistry::registerRuntime(_runtime);
26
- }
27
-
28
- ~WorkletRuntimeCollector() {
29
- Logger::log("WorkletRuntimeCollector", "Unregistering WorkletRuntime %p", &_runtime);
30
- RNFWorkletRuntimeRegistry::unregisterRuntime(_runtime);
31
- }
32
-
33
- static void install(jsi::Runtime& rt) {
34
- auto collector = std::make_shared<WorkletRuntimeCollector>(rt);
35
- auto object = jsi::Object::createFromHostObject(rt, collector);
36
- rt.global().setProperty(rt, "__workletRuntimeCollector", object);
37
- }
38
-
39
- private:
40
- jsi::Runtime& _runtime;
41
- };
42
-
43
- } // namespace margelo
@@ -1,12 +0,0 @@
1
- //
2
- // Created by Marc Rousavy on 22.02.24.
3
- //
4
- #include "RNFWorkletRuntimeRegistry.h"
5
- #include <set>
6
-
7
- namespace margelo {
8
-
9
- std::set<jsi::Runtime*> RNFWorkletRuntimeRegistry::registry_{};
10
- std::mutex RNFWorkletRuntimeRegistry::mutex_{};
11
-
12
- } // namespace margelo
@@ -1,44 +0,0 @@
1
- //
2
- // Created by Marc Rousavy on 22.02.24.
3
- //
4
- #pragma once
5
-
6
- #include <jsi/jsi.h>
7
-
8
- #include <mutex>
9
- #include <set>
10
-
11
- namespace jsi = facebook::jsi;
12
-
13
- namespace margelo {
14
-
15
- // From:
16
- // https://github.com/software-mansion/react-native-reanimated/blob/6cb1a66f1a68cac8079de2b6b305d22359847e51/Common/cpp/ReanimatedRuntime/WorkletRuntimeRegistry.h
17
- class RNFWorkletRuntimeRegistry {
18
- private:
19
- static std::set<jsi::Runtime*> registry_;
20
- static std::mutex mutex_; // Protects `registry_`.
21
-
22
- RNFWorkletRuntimeRegistry() {} // private ctor
23
-
24
- static void registerRuntime(jsi::Runtime& runtime) {
25
- std::lock_guard<std::mutex> lock(mutex_);
26
- registry_.insert(&runtime);
27
- }
28
-
29
- static void unregisterRuntime(jsi::Runtime& runtime) {
30
- std::lock_guard<std::mutex> lock(mutex_);
31
- registry_.erase(&runtime);
32
- }
33
-
34
- friend class WorkletRuntimeCollector;
35
-
36
- public:
37
- static bool isRuntimeAlive(jsi::Runtime* runtime) {
38
- assert(runtime != nullptr);
39
- std::lock_guard<std::mutex> lock(mutex_);
40
- return registry_.find(runtime) != registry_.end();
41
- }
42
- };
43
-
44
- } // namespace margelo