react-native-webgpu 0.26.0 → 0.27.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 (64) hide show
  1. package/android/CMakeLists.txt +2 -0
  2. package/cpp/rnwgpu/ArrayBuffer.h +47 -2
  3. package/cpp/rnwgpu/DawnEventPump.cpp +227 -0
  4. package/cpp/rnwgpu/DawnEventPump.h +173 -0
  5. package/cpp/rnwgpu/WebGPURuntime.cpp +6 -8
  6. package/cpp/rnwgpu/api/GPU.cpp +1 -1
  7. package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
  8. package/cpp/rnwgpu/api/GPUBuffer.cpp +100 -27
  9. package/cpp/rnwgpu/api/GPUBuffer.h +55 -6
  10. package/cpp/rnwgpu/api/GPUDevice.cpp +57 -55
  11. package/cpp/rnwgpu/api/GPUDevice.h +6 -0
  12. package/cpp/rnwgpu/api/GPUQueue.cpp +7 -2
  13. package/cpp/rnwgpu/api/GPUQueue.h +8 -2
  14. package/cpp/rnwgpu/api/GPUShaderModule.cpp +1 -1
  15. package/cpp/rnwgpu/api/RNWebGPU.h +3 -3
  16. package/cpp/rnwgpu/async/AsyncTaskHandle.h +3 -2
  17. package/cpp/rnwgpu/async/DeviceTicker.cpp +105 -0
  18. package/cpp/rnwgpu/async/DeviceTicker.h +71 -0
  19. package/cpp/rnwgpu/async/RuntimeContext.cpp +56 -70
  20. package/cpp/rnwgpu/async/RuntimeContext.h +37 -18
  21. package/lib/commonjs/NativeBufferView.js.map +1 -1
  22. package/lib/commonjs/NativeBufferViewNativeComponent.web.js +114 -0
  23. package/lib/commonjs/NativeBufferViewNativeComponent.web.js.map +1 -0
  24. package/lib/commonjs/WebPolyfillGPUModule.js +79 -0
  25. package/lib/commonjs/WebPolyfillGPUModule.js.map +1 -1
  26. package/lib/commonjs/main/index.web.js +11 -0
  27. package/lib/commonjs/main/index.web.js.map +1 -1
  28. package/lib/commonjs/utils.js +29 -0
  29. package/lib/commonjs/utils.js.map +1 -1
  30. package/lib/module/NativeBufferView.js.map +1 -1
  31. package/lib/module/NativeBufferViewNativeComponent.web.js +107 -0
  32. package/lib/module/NativeBufferViewNativeComponent.web.js.map +1 -0
  33. package/lib/module/WebPolyfillGPUModule.js +80 -1
  34. package/lib/module/WebPolyfillGPUModule.js.map +1 -1
  35. package/lib/module/main/index.web.js +1 -0
  36. package/lib/module/main/index.web.js.map +1 -1
  37. package/lib/module/utils.js +27 -0
  38. package/lib/module/utils.js.map +1 -1
  39. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.web.d.ts +4 -0
  40. package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
  41. package/lib/typescript/lib/commonjs/utils.d.ts +2 -0
  42. package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -1
  43. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.web.d.ts +5 -0
  44. package/lib/typescript/lib/module/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
  45. package/lib/typescript/lib/module/main/index.web.d.ts +1 -0
  46. package/lib/typescript/lib/module/utils.d.ts +2 -0
  47. package/lib/typescript/lib/module/utils.d.ts.map +1 -1
  48. package/lib/typescript/src/NativeBufferView.d.ts +6 -1
  49. package/lib/typescript/src/NativeBufferView.d.ts.map +1 -1
  50. package/lib/typescript/src/NativeBufferViewNativeComponent.web.d.ts +14 -0
  51. package/lib/typescript/src/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
  52. package/lib/typescript/src/index.d.ts +1 -1
  53. package/lib/typescript/src/index.d.ts.map +1 -1
  54. package/lib/typescript/src/main/index.web.d.ts +1 -0
  55. package/lib/typescript/src/main/index.web.d.ts.map +1 -1
  56. package/lib/typescript/src/utils.d.ts +2 -0
  57. package/lib/typescript/src/utils.d.ts.map +1 -1
  58. package/package.json +1 -1
  59. package/src/NativeBufferView.tsx +6 -1
  60. package/src/NativeBufferViewNativeComponent.web.ts +119 -0
  61. package/src/WebPolyfillGPUModule.ts +75 -1
  62. package/src/index.tsx +2 -1
  63. package/src/main/index.web.tsx +1 -0
  64. package/src/utils.ts +25 -0
@@ -0,0 +1,71 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <mutex>
5
+
6
+ #include "webgpu/webgpu_cpp.h"
7
+
8
+ #include "DawnEventPump.h"
9
+
10
+ namespace rnwgpu::async {
11
+
12
+ class RuntimeContext;
13
+
14
+ /**
15
+ * Has one device asked about its work (see DawnEventPump for why and when):
16
+ * what a poll of that device is, and which thread makes it. Made with the
17
+ * device's wrapper and shared with what starts asynchronous work on it (its
18
+ * queue, its buffers), which keeps the device alive with them, as in Dawn's
19
+ * Node binding.
20
+ *
21
+ * A poll is a tick of the device (dawn::native::DeviceTick), which never
22
+ * waits:
23
+ *
24
+ * - On the pump's thread when Dawn locks the device itself
25
+ * (ImplicitDeviceSynchronization, the default).
26
+ * - On the thread of the runtime the device was made on otherwise, the only
27
+ * one that may use it: the pump posts the tick there, one at a time.
28
+ *
29
+ * Which devices are polled:
30
+ *
31
+ * - While an operation is awaited (operationStarted): any device whose queue
32
+ * events do not complete by themselves, this library's or an imported one.
33
+ * Dawn tells with the device's spontaneous_queue_events toggle, which only
34
+ * Metal sets today: nothing polls for a promise there.
35
+ * - After a submit (workSubmitted): the devices this library made, on every
36
+ * backend. An imported device is its owner's to keep tidy.
37
+ */
38
+ class DeviceTicker : public std::enable_shared_from_this<DeviceTicker> {
39
+ public:
40
+ // `context`: the runtime the device's wrapper was made on. `owned`: whether
41
+ // this library made the device (requestDevice), rather than imported it.
42
+ DeviceTicker(wgpu::Device device, std::weak_ptr<RuntimeContext> context,
43
+ bool owned);
44
+
45
+ // An operation someone awaits is in flight on the device, its Dawn callback
46
+ // not run yet. The device is polled until the returned token goes; null
47
+ // when nothing needs to poll for it. Throws when a poll is needed and no
48
+ // thread could be had to make it. Any thread that may use the device.
49
+ [[nodiscard]] std::shared_ptr<void> operationStarted();
50
+
51
+ // queue.submit(): once nothing else is submitted for a while, the device
52
+ // is polled until Dawn has nothing left on it. Any thread.
53
+ void workSubmitted();
54
+
55
+ private:
56
+ // A poll of the device, made once: the pump copies it when it does not
57
+ // know the device yet.
58
+ const DawnEventPump::Poll &poll();
59
+
60
+ wgpu::Device _device;
61
+ std::weak_ptr<RuntimeContext> _context;
62
+ const bool _owned;
63
+ // Dawn locks the device itself: any thread may tick it.
64
+ const bool _synchronized;
65
+ // The device's queue events complete without anyone asking.
66
+ const bool _spontaneous;
67
+ std::once_flag _pollOnce;
68
+ DawnEventPump::Poll _poll;
69
+ };
70
+
71
+ } // namespace rnwgpu::async
@@ -1,22 +1,17 @@
1
1
  #include "RuntimeContext.h"
2
2
 
3
- #include <condition_variable>
4
- #include <cstdint>
5
- #include <deque>
6
3
  #include <memory>
4
+ #include <mutex>
7
5
  #include <stdexcept>
8
- #include <thread>
6
+ #include <string>
9
7
  #include <unordered_map>
10
8
  #include <utility>
11
9
  #include <vector>
12
10
 
13
- #if defined(__APPLE__) || defined(__ANDROID__)
14
- #include <pthread.h>
15
- #endif
16
-
17
11
  #include <ReactCommon/CallInvoker.h>
18
12
 
19
13
  #include "AsyncTaskHandle.h"
14
+ #include "DeviceTicker.h"
20
15
  #include "WGPULogger.h"
21
16
 
22
17
  namespace rnwgpu::async {
@@ -28,8 +23,8 @@ struct RuntimeData {
28
23
  constexpr const char *TAG = "RuntimeContext";
29
24
 
30
25
  // The main JS runtime and its CallInvoker, registered on install (again
31
- // after a reload) on the JS thread, and read from any thread: the waiter
32
- // thread posts through them.
26
+ // after a reload) on the JS thread, and read from any thread: whichever
27
+ // thread Dawn completes an operation on posts through them.
33
28
  std::mutex sMainMutex;
34
29
  jsi::Runtime *sMainRuntime = nullptr;
35
30
  std::shared_ptr<facebook::react::CallInvoker> sMainInvoker;
@@ -56,65 +51,42 @@ void logException(const char *what, const std::exception_ptr &error) {
56
51
  }
57
52
  }
58
53
 
59
- /**
60
- * The thread waiting on the futures of one instance: one future at a time,
61
- * in the order they were handed over, in Instance::WaitAny, which sleeps
62
- * until the GPU or Dawn's worker is done and then runs the callback. Never
63
- * destroyed: the instance it retains is shared by every runtime, and a
64
- * future may be outstanding during process teardown.
65
- */
66
- class Waiter {
54
+ // An operation's hold on the polling of its device (see DeviceTicker), which
55
+ // ends when its Dawn callback has settled the promise, on whichever thread
56
+ // that is, and at the latest when the callback goes.
57
+ class Polling {
67
58
  public:
68
- static Waiter &forInstance(const wgpu::Instance &instance) {
69
- static std::mutex registryMutex;
70
- static std::unordered_map<WGPUInstance, Waiter *> registry;
71
- std::lock_guard<std::mutex> lock(registryMutex);
72
- auto &slot = registry[instance.Get()];
73
- if (slot == nullptr) {
74
- slot = new Waiter(instance);
59
+ // Has the device polled, unless the operation is over already.
60
+ void start(const std::shared_ptr<DeviceTicker> &ticker) {
61
+ std::shared_ptr<void> token;
62
+ {
63
+ std::lock_guard<std::mutex> lock(_mutex);
64
+ if (_finished) {
65
+ return;
66
+ }
67
+ }
68
+ // Outside the lock: the pump is asked, and the callback may run
69
+ // meanwhile.
70
+ token = ticker->operationStarted();
71
+ std::lock_guard<std::mutex> lock(_mutex);
72
+ if (!_finished) {
73
+ _token = std::move(token);
75
74
  }
76
- return *slot;
77
75
  }
78
76
 
79
- void enqueue(wgpu::Future future) {
77
+ void finish() {
78
+ std::shared_ptr<void> token;
80
79
  {
81
80
  std::lock_guard<std::mutex> lock(_mutex);
82
- _futures.push_back(future);
81
+ _finished = true;
82
+ token.swap(_token);
83
83
  }
84
- _condition.notify_one();
85
84
  }
86
85
 
87
86
  private:
88
- explicit Waiter(wgpu::Instance instance) : _instance(std::move(instance)) {
89
- std::thread([this]() { run(); }).detach();
90
- }
91
-
92
- void run() {
93
- #if defined(__APPLE__)
94
- pthread_setname_np("RNWebGPU Waiter");
95
- #elif defined(__ANDROID__)
96
- pthread_setname_np(pthread_self(), "RNWebGPU Waiter");
97
- #endif
98
- for (;;) {
99
- wgpu::Future future;
100
- {
101
- std::unique_lock<std::mutex> lock(_mutex);
102
- _condition.wait(lock, [this]() { return !_futures.empty(); });
103
- future = _futures.front();
104
- _futures.pop_front();
105
- }
106
- auto status = _instance.WaitAny(future, UINT64_MAX);
107
- if (status != wgpu::WaitStatus::Success) {
108
- Logger::logToConsole("[%s] WaitAny failed with status %d", TAG,
109
- static_cast<int>(status));
110
- }
111
- }
112
- }
113
-
114
- wgpu::Instance _instance;
115
87
  std::mutex _mutex;
116
- std::condition_variable _condition;
117
- std::deque<wgpu::Future> _futures;
88
+ bool _finished = false;
89
+ std::shared_ptr<void> _token;
118
90
  };
119
91
  } // namespace
120
92
 
@@ -201,7 +173,9 @@ bool RuntimeContext::canSettle() const {
201
173
  return false;
202
174
  }
203
175
 
204
- AsyncTaskHandle RuntimeContext::postTask(const TaskCallback &callback) {
176
+ AsyncTaskHandle
177
+ RuntimeContext::postTask(const TaskCallback &callback,
178
+ const std::shared_ptr<DeviceTicker> &ticker) {
205
179
  if (!canSettle()) {
206
180
  throw std::runtime_error("Nothing can settle a promise on this runtime: "
207
181
  "react-native-webgpu is not installed on it.");
@@ -221,10 +195,25 @@ AsyncTaskHandle RuntimeContext::postTask(const TaskCallback &callback) {
221
195
 
222
196
  auto resolve = handle.createResolveFunction();
223
197
  auto reject = handle.createRejectFunction();
198
+ std::shared_ptr<Polling> polling;
199
+ if (ticker != nullptr) {
200
+ // The two functions end the polling too: Dawn's callback calls one of
201
+ // them, and nothing is left to learn about the operation then.
202
+ polling = std::make_shared<Polling>();
203
+ resolve = [polling, resolve = std::move(resolve)](
204
+ AsyncTaskHandle::ValueFactory factory) {
205
+ resolve(std::move(factory));
206
+ polling->finish();
207
+ };
208
+ reject = [polling, reject = std::move(reject)](std::string reason) {
209
+ reject(std::move(reason));
210
+ polling->finish();
211
+ };
212
+ }
224
213
  try {
225
214
  auto future = callback(resolve, reject);
226
- if (future.has_value()) {
227
- awaitFuture(*future);
215
+ if (future.has_value() && !complete(*future) && polling != nullptr) {
216
+ polling->start(ticker);
228
217
  }
229
218
  } catch (const std::exception &exception) {
230
219
  reject(exception.what());
@@ -234,20 +223,17 @@ AsyncTaskHandle RuntimeContext::postTask(const TaskCallback &callback) {
234
223
  return handle;
235
224
  }
236
225
 
237
- void RuntimeContext::awaitFuture(wgpu::Future future) {
238
- // Complete already (requestAdapter, popErrorScope, a lost device): the
239
- // callback runs here, and nothing waits.
240
- if (_instance.WaitAny(future, 0) == wgpu::WaitStatus::Success) {
241
- return;
242
- }
243
- Waiter::forInstance(_instance).enqueue(future);
226
+ bool RuntimeContext::complete(wgpu::Future future) {
227
+ // The zero timeout never waits. A future Dawn no longer knows is one
228
+ // whose callback has run, here or on another thread: Success too.
229
+ return _instance.WaitAny(future, 0) == wgpu::WaitStatus::Success;
244
230
  }
245
231
 
246
232
  void RuntimeContext::onTaskSettled() {
247
233
  _outstanding.fetch_sub(1, std::memory_order_acq_rel);
248
234
  }
249
235
 
250
- void RuntimeContext::post(Job job) {
236
+ void RuntimeContext::post(Job job, Wake wake) {
251
237
  switch (_kind) {
252
238
  case Kind::Main: {
253
239
  auto invoker = mainInvokerSlot();
@@ -263,7 +249,7 @@ void RuntimeContext::post(Job job) {
263
249
  std::lock_guard<std::mutex> lock(_mailboxMutex);
264
250
  _mailbox.push_back(std::move(job));
265
251
  }
266
- if (_kind == Kind::UI) {
252
+ if (_kind == Kind::UI && wake == Wake::Yes) {
267
253
  wakeUI();
268
254
  }
269
255
  return;
@@ -23,6 +23,8 @@ class CallInvoker;
23
23
 
24
24
  namespace rnwgpu::async {
25
25
 
26
+ class DeviceTicker;
27
+
26
28
  /**
27
29
  * How asynchronous WebGPU operations get back to the JS runtime that
28
30
  * requested them. One per (JS runtime, Dawn instance), made on first use and
@@ -31,17 +33,22 @@ namespace rnwgpu::async {
31
33
  * a device passed to importDevice() can live on another instance, which then
32
34
  * gets a context of its own.
33
35
  *
34
- * Waiting: Dawn operations are registered with CallbackMode::WaitAnyOnly, and
35
- * their futures handed to awaitFuture(). One already complete runs its
36
- * callback right there; the others go to a thread per instance that sleeps
37
- * in Instance::WaitAny until the GPU (or Dawn's worker) is done, then runs
38
- * the callback. Nothing polls meanwhile: neither the JS thread nor anyone
39
- * else calls ProcessEvents.
36
+ * Completion: nothing waits in Dawn (see DawnEventPump for why). Dawn
37
+ * operations are registered with CallbackMode::AllowSpontaneous, so that Dawn
38
+ * runs their callback as soon as it learns that they are done, on whichever
39
+ * thread that is: here in postTask() when the operation is complete already
40
+ * or once its commands are flushed, a Dawn worker (a pipeline compiled), the
41
+ * thread Metal reports a command buffer's completion on, or any thread that
42
+ * ticks the device, which a submit does. Where nothing reports the GPU's
43
+ * completions (Vulkan), the operation's device is ticked until the callback
44
+ * has run (see DeviceTicker).
40
45
  *
41
- * Settling: a callback runs on the waiter thread (or wherever a non-Dawn
46
+ * Settling: a callback runs on any of those threads, possibly inside a call
47
+ * into Dawn and with Dawn's device lock held (or wherever a non-Dawn
42
48
  * operation finishes, see GPUQueue::drawElementImageToTexture), so it never
43
- * touches JSI; it hands a settle action to post(), which runs it on the
44
- * runtime's own thread. How depends on the runtime:
49
+ * touches JSI nor Dawn; it hands a settle action to post(), which runs it on
50
+ * the runtime's own thread. Promises settle in the order their operations
51
+ * complete. How depends on the runtime:
45
52
  *
46
53
  * - The main React Native runtime: its CallInvoker (registerMainRuntime).
47
54
  * - Any other runtime (the Reanimated UI runtime, createWorkletRuntime, a
@@ -76,7 +83,7 @@ namespace rnwgpu::async {
76
83
  *
77
84
  * Threading contract: postTask() runs on the runtime's thread (it is the
78
85
  * entry point of every async method, called with the calling runtime, see
79
- * GPUBuffer::mapAsync); awaitFuture() and post() run on any thread.
86
+ * GPUBuffer::mapAsync); post() runs on any thread.
80
87
  */
81
88
  class RuntimeContext : public std::enable_shared_from_this<RuntimeContext> {
82
89
  public:
@@ -116,21 +123,27 @@ public:
116
123
 
117
124
  // Starts an asynchronous operation whose promise settles on this runtime:
118
125
  // `callback` gets the resolve and reject functions, starts the work, and
119
- // returns the Dawn future to wait for, if the work is Dawn's (it is awaited
120
- // like awaitFuture does). Called on the runtime's thread. Throws when
121
- // nothing could settle the promise (see canSettle).
122
- AsyncTaskHandle postTask(const TaskCallback &callback);
126
+ // returns its Dawn future, if the work is Dawn's. Dawn is asked about the
127
+ // future once, here (see complete), and `ticker`, the one of the
128
+ // operation's device, has the device asked from then on until one of the
129
+ // two functions is called. It may be left out for work that is complete as
130
+ // soon as it is started (requestAdapter, popErrorScope) and for work that
131
+ // is not Dawn's. Called on the runtime's thread. Throws when nothing could
132
+ // settle the promise (see canSettle).
133
+ AsyncTaskHandle postTask(const TaskCallback &callback,
134
+ const std::shared_ptr<DeviceTicker> &ticker = {});
123
135
 
124
136
  // Whether a job posted here reaches the runtime at all: false on the main
125
137
  // runtime before its CallInvoker is registered.
126
138
  bool canSettle() const;
127
139
 
128
- // Has the callback of `future` run: now, when it is complete already, else
129
- // on the instance's waiter thread once it is. Any thread.
130
- void awaitFuture(wgpu::Future future);
140
+ // Whether a posted job is worth waking the runtime for, ahead of what it
141
+ // does by itself. Only the UI runtime knows the difference (see wakeUI):
142
+ // the main runtime is always reached at once, the others only poll.
143
+ enum class Wake { Yes, No };
131
144
 
132
145
  // Runs `job` on this runtime's thread, later, with the runtime. Any thread.
133
- void post(Job job);
146
+ void post(Job job, Wake wake = Wake::Yes);
134
147
 
135
148
  // Invoked by a settle action once it has run, on the runtime's thread.
136
149
  void onTaskSettled();
@@ -141,6 +154,12 @@ private:
141
154
  static jsi::UUID runtimeDataUUID();
142
155
  static Kind kindOf(jsi::Runtime &runtime);
143
156
 
157
+ // Asks Dawn about `future` without waiting, and returns whether it is
158
+ // complete, its callback run. Dawn submits the commands the future waits
159
+ // for on the way (the clear of a buffer mapped for the first time, say),
160
+ // which nothing else would. On the runtime's thread.
161
+ bool complete(wgpu::Future future);
162
+
144
163
  // False when no tick can run on the runtime: it has no setTimeout, or
145
164
  // calling it threw.
146
165
  bool armTick(jsi::Runtime &runtime);
@@ -1 +1 @@
1
- {"version":3,"names":["_react","_interopRequireWildcard","require","_NativeBufferViewNativeComponent","_interopRequireDefault","e","__esModule","default","t","WeakMap","r","n","o","i","f","__proto__","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","_extends","assign","bind","arguments","length","apply","NativeBufferView","resizeMode","onPixelSizeChange","ref","props","viewId","useState","RNWebGPU","allocateNativeBufferViewId","useImperativeHandle","present","frame","done","presentNativeBuffer","useLayoutEffect","retainNativeBufferView","releaseNativeBufferView","onNativePixelSizeChange","useCallback","event","width","height","nativeEvent","createElement","exports","useNativeBufferSize","debounceMs","size","setSize","current","useRef","timer","next","clearTimeout","settle","previous","setTimeout","useEffect"],"sourceRoot":"../../src","sources":["NativeBufferView.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAUA,IAAAC,gCAAA,GAAAC,sBAAA,CAAAF,OAAA;AAAuE,SAAAE,uBAAAC,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAAA,SAAAJ,wBAAAI,CAAA,EAAAG,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAR,uBAAA,YAAAA,CAAAI,CAAA,EAAAG,CAAA,SAAAA,CAAA,IAAAH,CAAA,IAAAA,CAAA,CAAAC,UAAA,SAAAD,CAAA,MAAAO,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAR,OAAA,EAAAF,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAS,CAAA,MAAAF,CAAA,GAAAJ,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAE,CAAA,CAAAI,GAAA,CAAAX,CAAA,UAAAO,CAAA,CAAAK,GAAA,CAAAZ,CAAA,GAAAO,CAAA,CAAAM,GAAA,CAAAb,CAAA,EAAAS,CAAA,gBAAAN,CAAA,IAAAH,CAAA,gBAAAG,CAAA,OAAAW,cAAA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AAK,CAACF,OAAO,KAAK,IAAI,EAAE;MAC1BI,YAAY,CAACF,KAAK,CAACF,OAAO,CAAC;MAC3BE,KAAK,CAACF,OAAO,GAAG,IAAI;IACtB;IACA,MAAMK,MAAM,GAAGA,CAAA,KAAM;MACnBH,KAAK,CAACF,OAAO,GAAG,IAAI;MACpB,MAAMM,QAAQ,GAAGN,OAAO,CAACA,OAAO;MAChC,IACEM,QAAQ,KAAK,IAAI,IACjBA,QAAQ,CAACf,KAAK,KAAKY,IAAI,CAACZ,KAAK,IAC7Be,QAAQ,CAACd,MAAM,KAAKW,IAAI,CAACX,MAAM,EAC/B;QACAQ,OAAO,CAACA,OAAO,GAAGG,IAAI;QACtBJ,OAAO,CAACI,IAAI,CAAC;MACf;IACF,CAAC;IACD,IAAIH,OAAO,CAACA,OAAO,KAAK,IAAI,IAAIH,UAAU,IAAI,CAAC,EAAE;MAC/CQ,MAAM,CAAC,CAAC;IACV,CAAC,MAAM;MACLH,KAAK,CAACF,OAAO,GAAGO,UAAU,CAACF,MAAM,EAAER,UAAU,CAAC;IAChD;EACF,CAAC,EACD,CAACA,UAAU,CACb,CAAC;EAED,IAAAW,gBAAS,EACP,MAAM,MAAM;IACV,IAAIN,KAAK,CAACF,OAAO,KAAK,IAAI,EAAE;MAC1BI,YAAY,CAACF,KAAK,CAACF,OAAO,CAAC;IAC7B;EACF,CAAC,EACD,EACF,CAAC;EAED,OAAO;IAAEF,IAAI;IAAEzB;EAAkB,CAAC;AACpC,CAAC;AAACsB,OAAA,CAAAC,mBAAA,GAAAA,mBAAA","ignoreList":[]}
1
+ {"version":3,"names":["_react","_interopRequireWildcard","require","_NativeBufferViewNativeComponent","_interopRequireDefault","e","__esModule","default","t","WeakMap","r","n","o","i","f","__proto__","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","_extends","assign","bind","arguments","length","apply","NativeBufferView","resizeMode","onPixelSizeChange","ref","props","viewId","useState","RNWebGPU","allocateNativeBufferViewId","useImperativeHandle","present","frame","done","presentNativeBuffer","useLayoutEffect","retainNativeBufferView","releaseNativeBufferView","onNativePixelSizeChange","useCallback","event","width","height","nativeEvent","createElement","exports","useNativeBufferSize","debounceMs","size","setSize","current","useRef","timer","next","clearTimeout","settle","previous","setTimeout","useEffect"],"sourceRoot":"../../src","sources":["NativeBufferView.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAUA,IAAAC,gCAAA,GAAAC,sBAAA,CAAAF,OAAA;AAAuE,SAAAE,uBAAAC,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAAA,SAAAJ,wBAAAI,CAAA,EAAAG,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAR,uBAAA,YAAAA,CAAAI,CAAA,EAAAG,CAAA,SAAAA,CAAA,IAAAH,CAAA,IAAAA,CAAA,CAAAC,UAAA,SAAAD,CAAA,MAAAO,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAR,OAAA,EAAAF,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAS,CAAA,MAAAF,CAAA,GAAAJ,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAE,CAAA,CAAAI,GAAA,CAAAX,CAAA,UAAAO,CAAA,CAAAK,GAAA,CAAAZ,CAAA,GAAAO,CAAA,CAAAM,GAAA,CAAAb,CAAA,EAAAS,CAAA,gBAAAN,CAAA,IAAAH,CAAA,gBAAAG,CAAA,OAAAW,cAAA,CAAAC,IAAA,CAAAf,CAAA,EAAAG,CAAA,OAAAK,CAAA,IAAAD,CAAA,GAAAS,MAAA,CAAAC,cAAA,KAAAD,MAAA,CAAAE,wBAAA,CAAAlB,CAAA,EAAAG,CAAA,OAAAK,CAAA,CAAAI,GAAA,IAAAJ,CAAA,CAAAK,GAAA,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,MAAMM,QAAQ,GAAGN,OAAO,CAACA,OAAO;MAChC,IACEM,QAAQ,KAAK,IAAI,IACjBA,QAAQ,CAACf,KAAK,KAAKY,IAAI,CAACZ,KAAK,IAC7Be,QAAQ,CAACd,MAAM,KAAKW,IAAI,CAACX,MAAM,EAC/B;QACAQ,OAAO,CAACA,OAAO,GAAGG,IAAI;QACtBJ,OAAO,CAACI,IAAI,CAAC;MACf;IACF,CAAC;IACD,IAAIH,OAAO,CAACA,OAAO,KAAK,IAAI,IAAIH,UAAU,IAAI,CAAC,EAAE;MAC/CQ,MAAM,CAAC,CAAC;IACV,CAAC,MAAM;MACLH,KAAK,CAACF,OAAO,GAAGO,UAAU,CAACF,MAAM,EAAER,UAAU,CAAC;IAChD;EACF,CAAC,EACD,CAACA,UAAU,CACb,CAAC;EAED,IAAAW,gBAAS,EACP,MAAM,MAAM;IACV,IAAIN,KAAK,CAACF,OAAO,KAAK,IAAI,EAAE;MAC1BI,YAAY,CAACF,KAAK,CAACF,OAAO,CAAC;IAC7B;EACF,CAAC,EACD,EACF,CAAC;EAED,OAAO;IAAEF,IAAI;IAAEzB;EAAkB,CAAC;AACpC,CAAC;AAACsB,OAAA,CAAAC,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = NativeBufferViewNativeComponent;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ var _reactNative = require("react-native");
9
+ var _utils = require("./utils");
10
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
11
+ // The size of `entry` in physical pixels. devicePixelContentBoxSize is the
12
+ // size the browser snapped to the pixel grid; without it (Safari), the CSS
13
+ // size times the pixel ratio is the closest there is.
14
+ const pixelSizeOf = entry => {
15
+ const box = entry.devicePixelContentBoxSize?.[0];
16
+ if (box) {
17
+ return {
18
+ width: box.inlineSize,
19
+ height: box.blockSize
20
+ };
21
+ }
22
+ const dpr = window.devicePixelRatio || 1;
23
+ return {
24
+ width: Math.round(entry.contentRect.width * dpr),
25
+ height: Math.round(entry.contentRect.height * dpr)
26
+ };
27
+ };
28
+
29
+ // The web counterpart of the native view: a container that takes the props of
30
+ // a View, and a canvas that holds a copy of the last buffer presented
31
+ // (RNWebGPU.presentNativeBuffer in WebPolyfillGPUModule.ts draws into it).
32
+ // eslint-disable-next-line import/no-default-export
33
+ function NativeBufferViewNativeComponent(props) {
34
+ const {
35
+ viewId,
36
+ resizeMode,
37
+ onPixelSizeChange,
38
+ style,
39
+ ...rest
40
+ } = props;
41
+ const container = (0, _react.useRef)(null);
42
+ const canvas = (0, _react.useRef)(null);
43
+ const onPixelSizeChangeRef = (0, _react.useRef)(onPixelSizeChange);
44
+ onPixelSizeChangeRef.current = onPixelSizeChange;
45
+
46
+ // A layout effect, so the mode is in place before the first present.
47
+ (0, _react.useLayoutEffect)(() => {
48
+ if (canvas.current) {
49
+ canvas.current.dataset.resizeMode = resizeMode ?? "stretch";
50
+ (0, _utils.layoutNativeBufferViewCanvas)(canvas.current);
51
+ }
52
+ }, [resizeMode]);
53
+ (0, _react.useEffect)(() => {
54
+ // On react-native-web, the ref of a View is its DOM element.
55
+ const element = container.current;
56
+ if (!element || typeof ResizeObserver === "undefined") {
57
+ return;
58
+ }
59
+ let width = 0;
60
+ let height = 0;
61
+ const observer = new ResizeObserver(entries => {
62
+ const entry = entries[entries.length - 1];
63
+ if (!entry) {
64
+ return;
65
+ }
66
+ // A change of pixel ratio changes what "not scaled" means.
67
+ if (canvas.current) {
68
+ (0, _utils.layoutNativeBufferViewCanvas)(canvas.current);
69
+ }
70
+ const size = pixelSizeOf(entry);
71
+ if (size.width <= 0 || size.height <= 0 || size.width === width && size.height === height) {
72
+ return;
73
+ }
74
+ ({
75
+ width,
76
+ height
77
+ } = size);
78
+ onPixelSizeChangeRef.current?.({
79
+ nativeEvent: size
80
+ });
81
+ });
82
+ try {
83
+ observer.observe(element, {
84
+ box: "device-pixel-content-box"
85
+ });
86
+ } catch {
87
+ // No device-pixel-content-box (Safari).
88
+ observer.observe(element);
89
+ }
90
+ return () => observer.disconnect();
91
+ }, []);
92
+ return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
93
+ ...rest,
94
+ ref: container,
95
+ style: [style, styles.container]
96
+ }, /*#__PURE__*/_react.default.createElement("canvas", {
97
+ ref: canvas,
98
+ id: (0, _utils.nativeBufferViewIdToId)(viewId),
99
+ style: canvasStyle
100
+ }));
101
+ }
102
+ const canvasStyle = {
103
+ position: "absolute",
104
+ left: 0,
105
+ top: 0,
106
+ // The container handles the events, like the native view.
107
+ pointerEvents: "none"
108
+ };
109
+ const styles = _reactNative.StyleSheet.create({
110
+ container: {
111
+ overflow: "hidden"
112
+ }
113
+ });
114
+ //# sourceMappingURL=NativeBufferViewNativeComponent.web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_utils","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","pixelSizeOf","entry","box","devicePixelContentBoxSize","width","inlineSize","height","blockSize","dpr","window","devicePixelRatio","Math","round","contentRect","NativeBufferViewNativeComponent","props","viewId","resizeMode","onPixelSizeChange","style","rest","container","useRef","canvas","onPixelSizeChangeRef","current","useLayoutEffect","dataset","layoutNativeBufferViewCanvas","useEffect","element","ResizeObserver","observer","entries","length","size","nativeEvent","observe","disconnect","React","createElement","View","ref","styles","id","nativeBufferViewIdToId","canvasStyle","position","left","top","pointerEvents","StyleSheet","create","overflow"],"sourceRoot":"../../src","sources":["NativeBufferViewNativeComponent.web.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AAGA,IAAAE,MAAA,GAAAF,OAAA;AAA+E,SAAAD,wBAAAI,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAN,uBAAA,YAAAA,CAAAI,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,SAAAQ,CAAA,MAAAF,CAAA,GAAAL,CAAA,GAAAG,CAAA,GAAAD,CAAA,QAAAG,CAAA,CAAAK,GAAA,CAAAX,CAAA,UAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,GAAAM,CAAA,CAAAO,GAAA,CAAAb,CAAA,EAAAQ,CAAA,gBAAAP,CAAA,IAAAD,CAAA,gBAAAC,CAAA,OAAAa,cAAA,CAAAC,IAAA,CAAAf,CAAA,EAAAC,CAAA,OAAAM,CAAA,IAAAD,CAAA,GAAAU,MAAA,CAAAC,cAAA,KAAAD,MAAA,CAAAE,wBAAA,CAAAlB,CAAA,EAAAC,CAAA,OAAAM,CAAA,CAAAK,GAAA,IAAAL,CAAA,CAAAM,GAAA,IAAAP,CAAA,CAAAE,CAAA,EAAAP,CAAA,EAAAM,CAAA,IAAAC,CAAA,CAAAP,CAAA,IAAAD,CAAA,CAAAC,CAAA,WAAAO,CAAA,KAAAR,CAAA,EAAAC,CAAA;AAgB/E;AACA;AACA;AACA,MAAMkB,WA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C,QAAQ,EAAE;IAC5BE,GAAG,EAAEnB,MAAM;IACXqB,EAAE,EAAE,IAAAC,6BAAsB,EAAC7B,MAAM,CAAC;IAClCG,KAAK,EAAE2B;EACT,CAAC,CACH,CAAC;AACH;AAEA,MAAMA,WAAW,GAAG;EAClBC,QAAQ,EAAE,UAAU;EACpBC,IAAI,EAAE,CAAC;EACPC,GAAG,EAAE,CAAC;EACN;EACAC,aAAa,EAAE;AACjB,CAAU;AAEV,MAAMP,MAAM,GAAGQ,uBAAU,CAACC,MAAM,CAAC;EAC/B/B,SAAS,EAAE;IACTgC,QAAQ,EAAE;EACZ;AACF,CAAC,CAAC","ignoreList":[]}
@@ -47,6 +47,79 @@ function makeWebGPUCanvasContext(contextId, width, height) {
47
47
  });
48
48
  }
49
49
  let nextContextId = 1;
50
+ let nextNativeBufferViewId = 1;
51
+
52
+ // The size of a canvas image source in pixels, as drawImage() sees it.
53
+ function sizeOfImageSource(source) {
54
+ if ("displayWidth" in source) {
55
+ // A VideoFrame
56
+ return {
57
+ width: source.displayWidth,
58
+ height: source.displayHeight
59
+ };
60
+ }
61
+ if (source instanceof HTMLVideoElement) {
62
+ return {
63
+ width: source.videoWidth,
64
+ height: source.videoHeight
65
+ };
66
+ }
67
+ if (source instanceof HTMLImageElement) {
68
+ return {
69
+ width: source.naturalWidth,
70
+ height: source.naturalHeight
71
+ };
72
+ }
73
+ if (source instanceof SVGImageElement) {
74
+ return {
75
+ width: source.width.baseVal.value,
76
+ height: source.height.baseVal.value
77
+ };
78
+ }
79
+ return {
80
+ width: source.width,
81
+ height: source.height
82
+ };
83
+ }
84
+
85
+ // The web counterpart of NativeBufferViewRegistry::present. On the web, the
86
+ // native buffer is a canvas image source, as in drawImage(): an ImageBitmap,
87
+ // an OffscreenCanvas, a canvas, a VideoFrame, or an image or video element.
88
+ // The view keeps a copy of its pixels, so the source can be closed, presented
89
+ // to other views or drawn again right after the call. The fences are not
90
+ // needed: the browser orders the copy after the GPU work that wrote the
91
+ // source.
92
+ function presentNativeBuffer(viewId, frame) {
93
+ const source = frame;
94
+ const {
95
+ width,
96
+ height
97
+ } = source !== null && typeof source === "object" ? sizeOfImageSource(source) : {
98
+ width: 0,
99
+ height: 0
100
+ };
101
+ if (source === null || !(width > 0) || !(height > 0)) {
102
+ throw new Error("presentNativeBuffer: on the web, the frame is a canvas image source " + "(an ImageBitmap, a canvas, a VideoFrame, an image or a video) that " + "has a size");
103
+ }
104
+ const canvas = document.getElementById((0, _utils.nativeBufferViewIdToId)(viewId));
105
+ if (!canvas) {
106
+ // The view is gone: dropped, as on native.
107
+ return;
108
+ }
109
+ // Setting the size clears the canvas, even to the same value.
110
+ if (canvas.width !== width || canvas.height !== height) {
111
+ canvas.width = width;
112
+ canvas.height = height;
113
+ }
114
+ const context = canvas.getContext("2d");
115
+ if (!context) {
116
+ return;
117
+ }
118
+ // Replace the pixels, alpha included, rather than blend over the last ones.
119
+ context.globalCompositeOperation = "copy";
120
+ context.drawImage(source, 0, 0, width, height);
121
+ (0, _utils.layoutNativeBufferViewCanvas)(canvas);
122
+ }
50
123
 
51
124
  // @ts-expect-error - polyfill for RNWebGPU native module
52
125
  window.RNWebGPU = {
@@ -57,6 +130,12 @@ window.RNWebGPU = {
57
130
  MakeWebGPUCanvasContext: makeWebGPUCanvasContext,
58
131
  // On web the browser owns the canvas/context lifecycle; nothing to retire.
59
132
  destroyContext: _contextId => {},
133
+ // NativeBufferView: the canvas of the view is the registry entry, and the
134
+ // DOM owns it.
135
+ allocateNativeBufferViewId: () => nextNativeBufferViewId++,
136
+ retainNativeBufferView: _viewId => {},
137
+ releaseNativeBufferView: _viewId => {},
138
+ presentNativeBuffer,
60
139
  fabric
61
140
  };
62
141
  //# sourceMappingURL=WebPolyfillGPUModule.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_utils","require","fabric","getNativeSurface","contextId","canvas","document","getElementById","contextIdToId","clientHeight","height","clientWidth","width","surface","BigInt","addEventListener","bind","removeEventListener","dispatchEvent","setPointerCapture","releasePointerCapture","makeWebGPUCanvasContext","dpr","window","devicePixelRatio","pixelWidth","toString","pixelHeight","getAttribute","setAttribute","context","getContext","Object","assign","present","nextContextId","RNWebGPU","allocateContextId","MakeWebGPUCanvasContext","destroyContext","_contextId"],"sourceRoot":"../../src","sources":["WebPolyfillGPUModule.ts"],"mappings":";;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,MAAMC,MAAM,GAAG,IAAI;AAEnB,SAASC,gBAAgBA,CAACC,SAAiB,EAAE;EAC3C,MAAMC,MAAM,GAAGC,QAAQ,CAACC,cAAc,CACpC,IAAAC,oBAAa,EAACJ,SAAS,CACzB,CAAsB;;EAEtB;EACA;EACA,MAAM;IAAEK,YAAY,EAAEC,MAAM;IAAEC,WAAW,EAAEC;EAAM,CAAC,GAAGP,MAAM;EAE3D,OAAO;IACLQ,OAAO,EAAEC,MAAM,CAACV,SAAS,CAAC;IAC1BM,MAAM;IACNE,KAAK;IACLH,YAAY,EAAEC,MAAM;IACpBC,WAAW,EAAEC,KAAK;IAClB;IACAG,gBAAgB,EAAEV,MAAM,CAACU,gBAAgB,CAACC,IAAI,CAACX,MAAM,CAAC;IACtDY,mBAAmB,EAAEZ,MAAM,CAACY,mBAAmB,CAACD,IAAI,CAACX,MAAM,CAAC;IAC5Da,aAAa,EAAEb,MAAM,CAACa,aAAa,CAACF,IAAI,CAACX,MAAM,CAAC;IAChDc,iBAAiB,EAAEd,MAAM,CAACc,iBAAiB,CAACH,IAAI,CAACX,MAAM,CAAC;IACxDe,qBAAqB,EAAEf,MAAM,CAACe,qBAAqB,CAACJ,IAAI,CAACX,MAAM;EACjE,CAAC;AACH;AAEA,SAASgB,uBAAuBA,CAC9BjB,SAAiB,EACjBQ,KAAa,EACbF,MAAc,EACd;EACA,MAAML,MAAM,GAAGC,QAAQ,CAACC,cAAc,CACpC,IAAAC,oBAAa,EAACJ,SAAS,CACzB,CAAsB;EAEtB,MAAMkB,GAAG,GAAGC,MAAM,CAACC,gBAAgB,IAAI,CAAC;EACxC,MAAMC,UAAU,GAAG,CAACb,KAAK,GAAGU,GAAG,EAAEI,QAAQ,CAAC,CAAC;EAC3C,MAAMC,WAAW,GAAG,CAACjB,MAAM,GAAGY,GAAG,EAAEI,QAAQ,CAAC,CAAC;EAE7C,IACErB,MAAM,CAACuB,YAAY,CAAC,OAAO,CAAC,KAAKH,UAAU,IAC3CpB,MAAM,CAACuB,YAAY,CAAC,QAAQ,CAAC,KAAKD,WAAW,EAC7C;IACAtB,MAAM,CAACwB,YAAY,CAAC,OAAO,EAAEJ,UAAU,CAAC;IACxCpB,MAAM,CAACwB,YAAY,CAAC,QAAQ,EAAEF,WAAW,CAAC;EAC5C;EAEA,MAAMG,OAAO,GAAGzB,MAAM,CAAC0B,UAAU,CAAC,QAAQ,CAAC;EAC3C;EACA;EACA,IAAI,CAACD,OAAO,EAAE;IACZ,OAAO,IAAI;EACb;EACA;EACA;EACA,OAAOE,MAAM,CAACC,MAAM,CAACH,OAAO,EAAE;IAAEI,OAAO,EAAEA,CAAA,KAAM,CAAC;EAAE,CAAC,CAAC;AACtD;AAEA,IAAIC,aAAa,GAAG,CAAC;;AAErB;AACAZ,MAAM,CAACa,QAAQ,GAAG;EAChBC,iBAAiB,EAAEA,CAAA,KAAMF,aAAa,EAAE;EACxChC,gBAAgB;EAChB;EACA;EACAmC,uBAAuB,EACrBjB,uBAAkE;EACpE;EACAkB,cAAc,EAAGC,UAAkB,IAAK,CAAC,CAAC;EAC1CtC;AACF,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["_utils","require","fabric","getNativeSurface","contextId","canvas","document","getElementById","contextIdToId","clientHeight","height","clientWidth","width","surface","BigInt","addEventListener","bind","removeEventListener","dispatchEvent","setPointerCapture","releasePointerCapture","makeWebGPUCanvasContext","dpr","window","devicePixelRatio","pixelWidth","toString","pixelHeight","getAttribute","setAttribute","context","getContext","Object","assign","present","nextContextId","nextNativeBufferViewId","sizeOfImageSource","source","displayWidth","displayHeight","HTMLVideoElement","videoWidth","videoHeight","HTMLImageElement","naturalWidth","naturalHeight","SVGImageElement","baseVal","value","presentNativeBuffer","viewId","frame","Error","nativeBufferViewIdToId","globalCompositeOperation","drawImage","layoutNativeBufferViewCanvas","RNWebGPU","allocateContextId","MakeWebGPUCanvasContext","destroyContext","_contextId","allocateNativeBufferViewId","retainNativeBufferView","_viewId","releaseNativeBufferView"],"sourceRoot":"../../src","sources":["WebPolyfillGPUModule.ts"],"mappings":";;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAMA,MAAMC,MAAM,GAAG,IAAI;AAEnB,SAASC,gBAAgBA,CAACC,SAAiB,EAAE;EAC3C,MAAMC,MAAM,GAAGC,QAAQ,CAACC,cAAc,CACpC,IAAAC,oBAAa,EAACJ,SAAS,CACzB,CAAsB;;EAEtB;EACA;EACA,MAAM;IAAEK,YAAY,EAAEC,MAAM;IAAEC,WAAW,EAAEC;EAAM,CAAC,GAAGP,MAAM;EAE3D,OAAO;IACLQ,OAAO,EAAEC,MAAM,CAACV,SAAS,CAAC;IAC1BM,MAAM;IACNE,KAAK;IACLH,YAAY,EAAEC,MAAM;IACpBC,WAAW,EAAEC,KAAK;IAClB;IACAG,gBAAgB,EAAEV,MAAM,CAACU,gBAAgB,CAACC,IAAI,CAACX,MAAM,CAAC;IACtDY,mBAAmB,EAAEZ,MAAM,CAACY,mBAAmB,CAACD,IAAI,CAACX,MAAM,CAAC;IAC5Da,aAAa,EAAEb,MAAM,CAACa,aAAa,CAACF,IAAI,CAACX,MAAM,CAAC;IAChDc,iBAAiB,EAAEd,MAAM,CAACc,iBAAiB,CAACH,IAAI,CAACX,MAAM,CAAC;IACxDe,qBAAqB,EAAEf,MAAM,CAACe,qBAAqB,CAACJ,IAAI,CAACX,MAAM;EACjE,CAAC;AACH;AAEA,SAASgB,uBAAuBA,CAC9BjB,SAAiB,EACjBQ,KAAa,EACbF,MAAc,EACd;EACA,MAAML,MAAM,GAAGC,QAAQ,CAACC,cAAc,CACpC,IAAAC,oBAAa,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AO,GAAGzB,MAAM,CAAC0B,UAAU,CAAC,IAAI,CAAC;EACvC,IAAI,CAACD,OAAO,EAAE;IACZ;EACF;EACA;EACAA,OAAO,CAACyB,wBAAwB,GAAG,MAAM;EACzCzB,OAAO,CAAC0B,SAAS,CAAClB,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE1B,KAAK,EAAEF,MAAM,CAAC;EAC9C,IAAA+C,mCAA4B,EAACpD,MAAM,CAAC;AACtC;;AAEA;AACAkB,MAAM,CAACmC,QAAQ,GAAG;EAChBC,iBAAiB,EAAEA,CAAA,KAAMxB,aAAa,EAAE;EACxChC,gBAAgB;EAChB;EACA;EACAyD,uBAAuB,EACrBvC,uBAAkE;EACpE;EACAwC,cAAc,EAAGC,UAAkB,IAAK,CAAC,CAAC;EAC1C;EACA;EACAC,0BAA0B,EAAEA,CAAA,KAAM3B,sBAAsB,EAAE;EAC1D4B,sBAAsB,EAAGC,OAAe,IAAK,CAAC,CAAC;EAC/CC,uBAAuB,EAAGD,OAAe,IAAK,CAAC,CAAC;EAChDf,mBAAmB;EACnBhD;AACF,CAAC","ignoreList":[]}
@@ -15,6 +15,17 @@ Object.keys(_Canvas).forEach(function (key) {
15
15
  }
16
16
  });
17
17
  });
18
+ var _NativeBufferView = require("../NativeBufferView");
19
+ Object.keys(_NativeBufferView).forEach(function (key) {
20
+ if (key === "default" || key === "__esModule") return;
21
+ if (key in exports && exports[key] === _NativeBufferView[key]) return;
22
+ Object.defineProperty(exports, key, {
23
+ enumerable: true,
24
+ get: function () {
25
+ return _NativeBufferView[key];
26
+ }
27
+ });
28
+ });
18
29
  var _Offscreen = require("../Offscreen");
19
30
  Object.keys(_Offscreen).forEach(function (key) {
20
31
  if (key === "default" || key === "__esModule") return;