2323#include " rnwgpu/api/descriptors/GPUMapMode.h"
2424#include " rnwgpu/api/descriptors/GPUShaderStage.h"
2525#include " rnwgpu/api/descriptors/GPUTextureUsage.h"
26+ #include " rnwgpu/api/WebGPUConstants.h"
27+ #include " rnwgpu/async/RuntimeContext.h"
2628#include " jsi2/Promise.h"
2729
2830#include " include/core/SkData.h"
@@ -82,6 +84,12 @@ void RNSkManager::installBindings() {
8284 jsi::Object::createFromHostObject (*_jsRuntime, _viewApi));
8385
8486#ifdef SK_GRAPHITE
87+ // Register the main runtime + its CallInvoker so spontaneous events
88+ // (device.lost / uncapturederror) on main-runtime devices can be delivered to
89+ // the JS thread without the ProcessEvents pump. Worklet-runtime devices have
90+ // no invoker (best-effort; see the RuntimeContext "Threading model" doc).
91+ rnwgpu::async::RuntimeContext::registerMainRuntime (_jsRuntime, _jsCallInvoker);
92+
8593 // Install WebGPU constructors
8694 rnwgpu::GPU::installConstructor (*_jsRuntime);
8795 rnwgpu::GPUUncapturedErrorEvent::installConstructor (*_jsRuntime);
@@ -104,18 +112,26 @@ void RNSkManager::installBindings() {
104112 std::move (navigator));
105113 }
106114
107- // Install WebGPU constant objects as plain JS objects
108- _jsRuntime->global ().setProperty (*_jsRuntime, " GPUBufferUsage" ,
109- rnwgpu::GPUBufferUsage::create (*_jsRuntime));
110- _jsRuntime->global ().setProperty (*_jsRuntime, " GPUColorWrite" ,
111- rnwgpu::GPUColorWrite::create (*_jsRuntime));
112- _jsRuntime->global ().setProperty (*_jsRuntime, " GPUMapMode" ,
113- rnwgpu::GPUMapMode::create (*_jsRuntime));
114- _jsRuntime->global ().setProperty (*_jsRuntime, " GPUShaderStage" ,
115- rnwgpu::GPUShaderStage::create (*_jsRuntime));
115+ // Install WebGPU constant objects as plain JS objects on the main runtime.
116+ rnwgpu::installWebGPUConstants (*_jsRuntime);
117+
118+ // Install a global `installWebGPU()` host function so worklet runtimes can get
119+ // the same constants. A host function captured into a worklet is serialized as
120+ // a SerializableHostFunction and re-created on the worklet runtime, so the body
121+ // runs there (its `rt` is the worklet runtime) and installs the constants on
122+ // that runtime. The constants come from the native wgpu::*Usage enums, so the
123+ // values stay a single source of truth across every runtime. Calling it on a
124+ // runtime that already has the globals is a safe, idempotent no-op.
116125 _jsRuntime->global ().setProperty (
117- *_jsRuntime, " GPUTextureUsage" ,
118- rnwgpu::GPUTextureUsage::create (*_jsRuntime));
126+ *_jsRuntime, " installWebGPU" ,
127+ jsi::Function::createFromHostFunction (
128+ *_jsRuntime, jsi::PropNameID::forAscii (*_jsRuntime, " installWebGPU" ),
129+ 0 ,
130+ [](jsi::Runtime &rt, const jsi::Value & /* thisVal*/ ,
131+ const jsi::Value * /* args*/ , size_t /* count*/ ) -> jsi::Value {
132+ rnwgpu::installWebGPUConstants (rt);
133+ return jsi::Value::undefined ();
134+ }));
119135
120136 // Install RNWebGPU global object for WebGPU Canvas support
121137 auto rnWebGPU = std::make_shared<rnwgpu::RNWebGPU>(gpu, nullptr );
0 commit comments