From 4bedbe987d7d2ed025054e2025e4dbd5224cfac4 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Tue, 2 Jun 2026 06:50:33 +0200 Subject: [PATCH] :wrench: --- apps/example/src/VisionCamera/VisionCamera.tsx | 18 +++++++++++++----- packages/webgpu/cpp/rnwgpu/PlatformContext.h | 8 -------- packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp | 5 ----- packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp | 14 -------------- packages/webgpu/cpp/rnwgpu/api/GPUDevice.h | 9 --------- packages/webgpu/src/index.tsx | 4 ---- 6 files changed, 13 insertions(+), 45 deletions(-) diff --git a/apps/example/src/VisionCamera/VisionCamera.tsx b/apps/example/src/VisionCamera/VisionCamera.tsx index 6b5152eb4..c4adcfaa0 100644 --- a/apps/example/src/VisionCamera/VisionCamera.tsx +++ b/apps/example/src/VisionCamera/VisionCamera.tsx @@ -160,6 +160,13 @@ const CameraView = () => { }, []); const device = gpu?.device ?? null; const adapter = gpu?.adapter ?? null; + // Capture the RNWebGPU singleton into a local so the frame-processor worklet + // closes over it. RNWebGPU is a registered, boxable WebGPU NativeObject, so + // the Worklets custom serializer ships it across the worklet boundary the same + // way it does `device` (it is NOT installed as a global on worklet runtimes). + // This is what lets us call the interop factory off RNWebGPU, where the native + // platform context already lives, instead of off `device`. + const rnwgpu = RNWebGPU; const devices = useCameraDevices(); // Pick back camera if available, otherwise front, otherwise anything. The // iOS simulator returns an empty list since there are no cameras, in which @@ -443,11 +450,12 @@ const CameraView = () => { try { let videoFrame; try { - // Call createVideoFrameFromNativeBuffer on the device, not on the - // RNWebGPU global — `device` is already box-able across worklet - // runtimes via the WebGPU custom serializer (proven by the - // Reanimated demo); RNWebGPU is a main-runtime-only global. - videoFrame = device.createVideoFrameFromNativeBuffer( + // Call createVideoFrameFromNativeBuffer on the captured RNWebGPU + // singleton (see `rnwgpu` above). It rides into this worklet via the + // WebGPU custom serializer, same as `device`, so the factory can live + // on RNWebGPU where the native platform context already is — no + // GPUDevice-level workaround and no PlatformContext global singleton. + videoFrame = rnwgpu.createVideoFrameFromNativeBuffer( nativeBuffer.pointer, ); } catch (e) { diff --git a/packages/webgpu/cpp/rnwgpu/PlatformContext.h b/packages/webgpu/cpp/rnwgpu/PlatformContext.h index cdab10da4..fec049256 100644 --- a/packages/webgpu/cpp/rnwgpu/PlatformContext.h +++ b/packages/webgpu/cpp/rnwgpu/PlatformContext.h @@ -70,14 +70,6 @@ class PlatformContext { PlatformContext() = default; virtual ~PlatformContext() = default; - // Singleton-style accessor so leaf classes (e.g. GPUDevice) can reach the - // platform context without threading it through every constructor. Set by - // RNWebGPUManager at startup. - static std::shared_ptr &global() { - static std::shared_ptr instance; - return instance; - } - virtual wgpu::Surface makeSurface(wgpu::Instance instance, void *surface, int width, int height) = 0; virtual ImageData createImageBitmap(std::string blobId, double offset, diff --git a/packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp b/packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp index 6b8a28cb3..f874c14e6 100644 --- a/packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp +++ b/packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp @@ -63,11 +63,6 @@ RNWebGPUManager::RNWebGPUManager( // Register main runtime for RuntimeAwareCache BaseRuntimeAwareCache::setMainJsRuntime(_jsRuntime); - // Expose the platform context for leaf classes that need it (e.g. - // GPUDevice::createVideoFrameFromNativeBuffer) without threading it through - // every constructor. - PlatformContext::global() = _platformContext; - auto gpu = std::make_shared(*_jsRuntime); auto rnWebGPU = std::make_shared(gpu, _platformContext, _jsCallInvoker); diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp b/packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp index 18df1d07b..4d6c92ffa 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp +++ b/packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp @@ -8,7 +8,6 @@ #include "Convertors.h" #include "JSIConverter.h" -#include "PlatformContext.h" #include "GPUFeatures.h" #include "GPUInternalError.h" @@ -242,19 +241,6 @@ std::shared_ptr GPUDevice::importExternalTexture( return GPUExternalTexture::Create(_instance, std::move(descriptor)); } -std::shared_ptr -GPUDevice::createVideoFrameFromNativeBuffer(uint64_t pointer) { - auto platformContext = PlatformContext::global(); - if (!platformContext) { - throw std::runtime_error( - "GPUDevice::createVideoFrameFromNativeBuffer(): PlatformContext is " - "not initialized"); - } - auto handle = - platformContext->wrapNativeBuffer(reinterpret_cast(pointer)); - return std::make_shared(std::move(handle)); -} - std::shared_ptr GPUDevice::importSharedTextureMemory( std::shared_ptr descriptor) { if (!descriptor || descriptor->handle == nullptr) { diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h b/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h index 28add6e0c..2ab1ddd14 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h +++ b/packages/webgpu/cpp/rnwgpu/api/GPUDevice.h @@ -53,7 +53,6 @@ #include "GPUTexture.h" #include "GPUTextureDescriptor.h" #include "GPUUncapturedErrorEvent.h" -#include "VideoFrame.h" namespace rnwgpu { @@ -121,12 +120,6 @@ class GPUDevice : public NativeObject { std::shared_ptr descriptor); std::shared_ptr importSharedTextureMemory( std::shared_ptr descriptor); - // Wrap a CVPixelBufferRef / AHardwareBuffer* pointer (as a BigInt) into a - // VideoFrame. Mirrors RNWebGPU.createVideoFrameFromNativeBuffer but is - // reachable from worklet runtimes since GPUDevice is already serialized - // across the worklet boundary via the WebGPU custom serializer. - std::shared_ptr - createVideoFrameFromNativeBuffer(uint64_t pointer); std::shared_ptr createBindGroupLayout( std::shared_ptr descriptor); std::shared_ptr @@ -182,8 +175,6 @@ class GPUDevice : public NativeObject { &GPUDevice::importExternalTexture); installMethod(runtime, prototype, "importSharedTextureMemory", &GPUDevice::importSharedTextureMemory); - installMethod(runtime, prototype, "createVideoFrameFromNativeBuffer", - &GPUDevice::createVideoFrameFromNativeBuffer); installMethod(runtime, prototype, "createBindGroupLayout", &GPUDevice::createBindGroupLayout); installMethod(runtime, prototype, "createPipelineLayout", diff --git a/packages/webgpu/src/index.tsx b/packages/webgpu/src/index.tsx index 3e5e819ac..6ac631a48 100644 --- a/packages/webgpu/src/index.tsx +++ b/packages/webgpu/src/index.tsx @@ -54,10 +54,6 @@ declare global { importSharedTextureMemory( descriptor: GPUSharedTextureMemoryDescriptor, ): GPUSharedTextureMemory; - // Wrap a NativeBuffer.pointer into a NativeVideoFrame. Reachable from - // worklet runtimes (e.g. Vision Camera frame processors) because GPUDevice - // is serialized across worklet boundaries via the WebGPU custom serializer. - createVideoFrameFromNativeBuffer(pointer: bigint): NativeVideoFrame; } // Non-spec extension: camera frames arrive in the sensor's native