Skip to content

Commit f58801e

Browse files
committed
🔧
1 parent 81e4e31 commit f58801e

1 file changed

Lines changed: 30 additions & 9 deletions

File tree

packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,36 @@ jsi::Value GPUCanvasContext::getCurrentTexture(jsi::Runtime &runtime,
4848
auto texture = _surfaceInfo->getCurrentTexture();
4949

5050
auto surfaceInfo = _surfaceInfo;
51-
auto present = jsi::Function::createFromHostFunction(
52-
runtime, jsi::PropNameID::forAscii(runtime, "WebGPUPresent"), 0,
53-
[surfaceInfo](jsi::Runtime & /*rt*/, const jsi::Value & /*thisValue*/,
54-
const jsi::Value * /*args*/,
55-
size_t /*count*/) -> jsi::Value {
56-
surfaceInfo->present();
57-
return jsi::Value::undefined();
58-
});
59-
runtime.queueMicrotask(std::move(present));
51+
auto presentCb = [surfaceInfo](jsi::Runtime & /*rt*/,
52+
const jsi::Value & /*thisValue*/,
53+
const jsi::Value * /*args*/,
54+
size_t /*count*/) -> jsi::Value {
55+
surfaceInfo->present();
56+
return jsi::Value::undefined();
57+
};
58+
auto makeFn = [&]() {
59+
return jsi::Function::createFromHostFunction(
60+
runtime, jsi::PropNameID::forAscii(runtime, "WebGPUPresent"), 0,
61+
presentCb);
62+
};
63+
// Try queueMicrotask first (Hermes JS thread). If the runtime disables
64+
// microtasks (e.g. Worklets), fall back to setImmediate, then setTimeout —
65+
// both have end-of-current-task semantics with no display latency.
66+
try {
67+
runtime.queueMicrotask(makeFn());
68+
return JSIConverter<std::shared_ptr<GPUTexture>>::toJSI(
69+
runtime, std::make_shared<GPUTexture>(texture, "", false));
70+
} catch (...) {
71+
// fall through
72+
}
73+
auto global = runtime.global();
74+
if (global.hasProperty(runtime, "setImmediate")) {
75+
auto setImmediate = global.getPropertyAsFunction(runtime, "setImmediate");
76+
setImmediate.call(runtime, makeFn());
77+
} else if (global.hasProperty(runtime, "setTimeout")) {
78+
auto setTimeout = global.getPropertyAsFunction(runtime, "setTimeout");
79+
setTimeout.call(runtime, makeFn(), jsi::Value(0));
80+
}
6081

6182
// Pass reportsMemoryPressure=false to avoid triggering spurious Hermes GC
6283
// cycles every frame since the canvas texture doesn't own the buffer.

0 commit comments

Comments
 (0)