react-native-webgpu 0.5.11 → 0.5.13

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 (57) hide show
  1. package/README.md +84 -0
  2. package/android/CMakeLists.txt +2 -0
  3. package/android/cpp/AndroidPlatformContext.h +121 -0
  4. package/apple/ApplePlatformContext.h +13 -0
  5. package/apple/ApplePlatformContext.mm +145 -0
  6. package/apple/AppleVideoPlayer.h +31 -0
  7. package/apple/AppleVideoPlayer.mm +314 -0
  8. package/cpp/rnwgpu/ArrayBuffer.h +51 -7
  9. package/cpp/rnwgpu/PlatformContext.h +81 -0
  10. package/cpp/rnwgpu/RNWebGPUManager.cpp +12 -0
  11. package/cpp/rnwgpu/api/Convertors.h +33 -11
  12. package/cpp/rnwgpu/api/GPU.cpp +27 -0
  13. package/cpp/rnwgpu/api/GPUAdapter.cpp +109 -33
  14. package/cpp/rnwgpu/api/GPUDevice.cpp +58 -5
  15. package/cpp/rnwgpu/api/GPUDevice.h +6 -0
  16. package/cpp/rnwgpu/api/GPUExternalTexture.cpp +335 -0
  17. package/cpp/rnwgpu/api/GPUExternalTexture.h +47 -2
  18. package/cpp/rnwgpu/api/GPUFeatures.h +4 -1
  19. package/cpp/rnwgpu/api/GPUShaderModule.cpp +2 -3
  20. package/cpp/rnwgpu/api/GPUSharedTextureMemory.cpp +80 -0
  21. package/cpp/rnwgpu/api/GPUSharedTextureMemory.h +71 -0
  22. package/cpp/rnwgpu/api/ImageBitmap.h +8 -0
  23. package/cpp/rnwgpu/api/RNWebGPU.h +63 -21
  24. package/cpp/rnwgpu/api/RnFeatures.h +53 -0
  25. package/cpp/rnwgpu/api/VideoFrame.h +76 -0
  26. package/cpp/rnwgpu/api/VideoPlayer.h +69 -0
  27. package/cpp/rnwgpu/api/descriptors/GPUBindGroupEntry.h +4 -1
  28. package/cpp/rnwgpu/api/descriptors/GPUDawnTogglesDescriptor.h +57 -0
  29. package/cpp/rnwgpu/api/descriptors/GPUDeviceDescriptor.h +18 -3
  30. package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +35 -33
  31. package/cpp/rnwgpu/api/descriptors/GPUSharedTextureMemoryDescriptor.h +62 -0
  32. package/cpp/rnwgpu/api/descriptors/Unions.h +10 -0
  33. package/cpp/webgpu/webgpu.h +78 -17
  34. package/cpp/webgpu/webgpu_cpp.h +1014 -1249
  35. package/cpp/webgpu/webgpu_cpp_print.h +99 -10
  36. package/lib/commonjs/Canvas.js.map +1 -1
  37. package/lib/commonjs/index.js.map +1 -1
  38. package/lib/module/Canvas.js.map +1 -1
  39. package/lib/module/index.js.map +1 -1
  40. package/lib/typescript/src/Canvas.d.ts +0 -10
  41. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  42. package/lib/typescript/src/index.d.ts +20 -1
  43. package/lib/typescript/src/index.d.ts.map +1 -1
  44. package/lib/typescript/src/types.d.ts +32 -1
  45. package/lib/typescript/src/types.d.ts.map +1 -1
  46. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  47. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  48. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  49. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  50. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +6 -6
  51. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  52. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  53. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  54. package/package.json +3 -3
  55. package/src/Canvas.tsx +0 -15
  56. package/src/index.tsx +62 -2
  57. package/src/types.ts +83 -1
package/README.md CHANGED
@@ -222,6 +222,90 @@ device.queue.copyExternalImageToTexture(
222
222
  );
223
223
  ```
224
224
 
225
+ ### Shared Texture Memory
226
+
227
+ React Native WebGPU exposes Dawn's `SharedTextureMemory` so you can import a native pixel surface (an `IOSurface`-backed `CVPixelBuffer` on iOS, an `AHardwareBuffer` on Android) as a sampleable `GPUTexture` without copying pixels through the CPU. This is the path you want for camera frames, video frames, or anything coming out of a hardware producer.
228
+
229
+ Like `importExternalTexture` on the web, this is **enabled by default**, there is nothing to request at device creation. The only thing to check is that the device supports it before importing. It always does on iOS/macOS; it can be missing on some Android drivers and emulators.
230
+
231
+ ```tsx
232
+ import type { NativeVideoFrame } from "react-native-wgpu";
233
+
234
+ const adapter = await navigator.gpu.requestAdapter();
235
+ const device = await adapter!.requestDevice();
236
+
237
+ // On by default when supported; this is the only check you need.
238
+ if (!device.features.has("rnwebgpu/native-texture" as GPUFeatureName)) {
239
+ return; // rare: some Android drivers/emulators can't import native surfaces
240
+ }
241
+
242
+ // `frame` here is a NativeVideoFrame whose .handle is the native surface
243
+ // (IOSurfaceRef / AHardwareBuffer*). NativeVideoFrames are produced by helpers
244
+ // like RNWebGPU.createVideoPlayer or RNWebGPU.createTestVideoFrame, or by
245
+ // any third-party module that hands you a compatible native pointer.
246
+ const memory = device.importSharedTextureMemory({
247
+ handle: frame.handle,
248
+ label: "video-frame",
249
+ });
250
+ const texture = memory.createTexture();
251
+
252
+ memory.beginAccess(texture, /* initialized */ true);
253
+ // ... bind `texture` into a sampler and render normally ...
254
+ memory.endAccess(texture);
255
+
256
+ texture.destroy();
257
+ frame.release();
258
+ ```
259
+
260
+ `beginAccess`/`endAccess` bracket the GPU's read window on the shared surface. Pass `initialized: true` when the producer has already written meaningful pixels (the typical video/camera case) and `false` when the next pass will fully overwrite the texture.
261
+
262
+ ### Importing External Textures
263
+
264
+ `GPUDevice.importExternalTexture` is the higher-level path for sampling a native surface. You hand it a `NativeVideoFrame` and get back a `GPUExternalTexture` that you bind as a `texture_external` and read with `textureSampleBaseClampToEdge`. It does two things for you on top of `SharedTextureMemory`:
265
+
266
+ - **Color conversion.** Camera and video surfaces are usually biplanar YUV (NV12), not RGB. An external texture carries the YUV→RGB matrix and the source/destination color-space transfer functions, so on the supported paths the sampler returns ready-to-use RGB in hardware. With raw `SharedTextureMemory` you would sample the luma/chroma planes and do that conversion by hand in WGSL. This is the main reason to prefer it for camera and video frames.
267
+ - **Lifecycle.** It owns the `SharedTextureMemory` + `createTexture` + `beginAccess`/`endAccess` sequence internally, so you just import the frame and `destroy()` the result.
268
+
269
+ It builds on the same default-on capability as Shared Texture Memory above, so feature-detect the device the same way before importing.
270
+
271
+ > **Android note:** the hardware YUV→RGB conversion is fully automatic on iOS (NV12 `IOSurface`). On Android, camera frames arrive as an _opaque_ YCbCr `AHardwareBuffer`, and Dawn's Vulkan path forces an identity (`RGB_IDENTITY`) sampler conversion, so the external sample comes back as raw `[Y, Cb, Cr]`. You still get the zero-copy import and the rotation/mirror transform, but you need to apply the YUV→RGB matrix yourself in the shader. See the `CAMERA_PRELUDE` in the [VisionCamera example](/apps/example/src/VisionCamera/shaders.ts) for a ready-made BT.709 decode.
272
+
273
+ ```tsx
274
+ const adapter = await navigator.gpu.requestAdapter();
275
+ const device = await adapter!.requestDevice();
276
+ // Feature-detect as shown above before importing on unsupported hardware.
277
+
278
+ const render = () => {
279
+ // A GPUExternalTexture expires once the queue work that used it is submitted,
280
+ // so re-import one every frame.
281
+ const externalTexture = device.importExternalTexture({
282
+ source: frame, // a NativeVideoFrame
283
+ label: "video-frame",
284
+ });
285
+
286
+ const bindGroup = device.createBindGroup({
287
+ layout: pipeline.getBindGroupLayout(0),
288
+ entries: [
289
+ { binding: 0, resource: externalTexture },
290
+ { binding: 1, resource: sampler },
291
+ ],
292
+ });
293
+
294
+ // ... encode a pass that samples `externalTexture`, then:
295
+ device.queue.submit([encoder.finish()]);
296
+
297
+ // Release the surface's access window right after the submit that sampled it.
298
+ externalTexture.destroy();
299
+ context.present();
300
+ };
301
+ ```
302
+
303
+ Camera frames arrive in the sensor's native orientation, so `importExternalTexture` also accepts non-spec `rotation` (`0` | `90` | `180` | `270`, in degrees) and `mirrored` (horizontal flip) options. Dawn bakes them into the sampling transform, so the shader sees an upright frame. They map directly onto VisionCamera's `frame.orientation` / `frame.isMirrored`.
304
+
305
+ #### Calling `destroy()`
306
+
307
+ A `GPUExternalTexture` keeps an open access window on the underlying native surface until the wrapper is destroyed. On the Web `importExternalTexture` is core and the lifetime is handled for you; here the window is tied to the JavaScript object's lifetime. Call `externalTexture.destroy()` right after the `queue.submit()` that sampled it (never before) to release the surface back to its producer immediately. `destroy()` is idempotent, and the surface is also released when the object is garbage-collected, but relying on GC can starve a producer's buffer pool (e.g. an `AVPlayer`'s recycled `IOSurface`s) and pile up GPU resources, so prefer the explicit call in a render loop.
308
+
225
309
  ### Reanimated Integration
226
310
 
227
311
  React Native WebGPU supports running WebGPU rendering on the UI thread using [React Native Reanimated](https://docs.swmansion.com/react-native-reanimated/) and [React Native Worklets](https://github.com/margelo/react-native-worklets).
@@ -37,6 +37,8 @@ add_library(${PACKAGE_NAME} SHARED
37
37
  ../cpp/rnwgpu/api/GPUCommandEncoder.cpp
38
38
  ../cpp/rnwgpu/api/GPUQuerySet.cpp
39
39
  ../cpp/rnwgpu/api/GPUTexture.cpp
40
+ ../cpp/rnwgpu/api/GPUSharedTextureMemory.cpp
41
+ ../cpp/rnwgpu/api/GPUExternalTexture.cpp
40
42
  ../cpp/rnwgpu/api/GPURenderBundleEncoder.cpp
41
43
  ../cpp/rnwgpu/api/GPURenderPassEncoder.cpp
42
44
  ../cpp/rnwgpu/api/GPURenderPipeline.cpp
@@ -1,8 +1,10 @@
1
1
  #pragma once
2
2
 
3
3
  #include <android/bitmap.h>
4
+ #include <android/hardware_buffer.h>
4
5
  #include <jni.h>
5
6
 
7
+ #include <algorithm>
6
8
  #include <functional>
7
9
  #include <memory>
8
10
  #include <string>
@@ -202,6 +204,125 @@ public:
202
204
  }
203
205
  }).detach();
204
206
  }
207
+
208
+ VideoFrameHandle loadVideoFrame(const std::string & /*path*/) override {
209
+ // TODO: implement using MediaExtractor + MediaCodec to decode the first
210
+ // frame into an AHardwareBuffer-backed Image (Android API 26+).
211
+ throw std::runtime_error(
212
+ "loadVideoFrame is not yet implemented on Android. Pass an "
213
+ "AHardwareBuffer pointer obtained elsewhere (e.g. from "
214
+ "react-native-vision-camera) directly to "
215
+ "device.importSharedTextureMemory.");
216
+ }
217
+
218
+ VideoFrameHandle createTestVideoFrame(uint32_t width,
219
+ uint32_t height) override {
220
+ // Dawn's Android backend already requires API 26+, so AHardwareBuffer_*
221
+ // symbols are guaranteed to be available at link time on supported builds.
222
+ AHardwareBuffer_Desc desc = {};
223
+ desc.width = width;
224
+ desc.height = height;
225
+ desc.layers = 1;
226
+ desc.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
227
+ desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
228
+ AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY |
229
+ AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
230
+
231
+ AHardwareBuffer *buffer = nullptr;
232
+ int err = AHardwareBuffer_allocate(&desc, &buffer);
233
+ if (err != 0 || !buffer) {
234
+ throw std::runtime_error(
235
+ "createTestVideoFrame: AHardwareBuffer_allocate failed (" +
236
+ std::to_string(err) + ")");
237
+ }
238
+
239
+ AHardwareBuffer_Desc actualDesc = {};
240
+ AHardwareBuffer_describe(buffer, &actualDesc);
241
+ const uint32_t stridePixels = actualDesc.stride;
242
+
243
+ void *vaddr = nullptr;
244
+ int rc = AHardwareBuffer_lock(buffer, AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY,
245
+ -1, nullptr, &vaddr);
246
+ if (rc != 0 || !vaddr) {
247
+ AHardwareBuffer_release(buffer);
248
+ throw std::runtime_error(
249
+ "createTestVideoFrame: AHardwareBuffer_lock failed (" +
250
+ std::to_string(rc) + ")");
251
+ }
252
+
253
+ // Same procedural pattern as ApplePlatformContext::createTestVideoFrame so
254
+ // the shared snapshot matches on both platforms. Apple writes BGRA bytes
255
+ // (CVPixelBuffer is kCVPixelFormatType_32BGRA, sampled as bgra8unorm);
256
+ // here we write RGBA bytes (AHardwareBuffer is R8G8B8A8_UNORM, sampled as
257
+ // rgba8unorm). The fragment shader sees the same logical (r, g, b, a).
258
+ uint8_t *base = static_cast<uint8_t *>(vaddr);
259
+ const size_t rowBytes = stridePixels * 4;
260
+ for (uint32_t y = 0; y < height; ++y) {
261
+ uint8_t *row = base + y * rowBytes;
262
+ for (uint32_t x = 0; x < width; ++x) {
263
+ uint8_t r = static_cast<uint8_t>((x * 255) / std::max(width - 1, 1u));
264
+ uint8_t g = static_cast<uint8_t>((y * 255) / std::max(height - 1, 1u));
265
+ uint8_t b = static_cast<uint8_t>(((x + y) & 0x20) ? 220 : 30);
266
+ row[x * 4 + 0] = r;
267
+ row[x * 4 + 1] = g;
268
+ row[x * 4 + 2] = b;
269
+ row[x * 4 + 3] = 0xFF;
270
+ }
271
+ }
272
+ AHardwareBuffer_unlock(buffer, nullptr);
273
+
274
+ VideoFrameHandle handle;
275
+ handle.handle = static_cast<void *>(buffer);
276
+ handle.width = width;
277
+ handle.height = height;
278
+ handle.deleter = [buffer]() { AHardwareBuffer_release(buffer); };
279
+ return handle;
280
+ }
281
+
282
+ std::unique_ptr<IVideoPlayer>
283
+ createVideoPlayer(const std::string & /*path*/,
284
+ VideoPixelFormat /*format*/) override {
285
+ // TODO: implement using MediaCodec -> ImageReader (AHardwareBuffer mode).
286
+ throw std::runtime_error(
287
+ "createVideoPlayer is not yet implemented on Android.");
288
+ }
289
+
290
+ std::string writeTestVideoFile() override {
291
+ // TODO: implement using MediaCodec (H.264 encoder) or MediaMuxer.
292
+ throw std::runtime_error(
293
+ "writeTestVideoFile is not yet implemented on Android.");
294
+ }
295
+
296
+ VideoFrameHandle wrapNativeBuffer(void *pointer) override {
297
+ if (!pointer) {
298
+ throw std::runtime_error("wrapNativeBuffer: pointer is null");
299
+ }
300
+ auto *buffer = static_cast<AHardwareBuffer *>(pointer);
301
+
302
+ AHardwareBuffer_Desc desc = {};
303
+ AHardwareBuffer_describe(buffer, &desc);
304
+
305
+ AHardwareBuffer_acquire(buffer);
306
+
307
+ VideoFrameHandle handle;
308
+ handle.handle = static_cast<void *>(buffer);
309
+ handle.width = desc.width;
310
+ handle.height = desc.height;
311
+ // YUV / opaque formats route through Vulkan's SamplerYcbcrConversion via
312
+ // Dawn's OpaqueYCbCrAndroidForExternalTexture path. Single-plane RGBA AHBs
313
+ // take the plain BGRA8 path (sampled as a regular 2D texture).
314
+ switch (desc.format) {
315
+ case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
316
+ case AHARDWAREBUFFER_FORMAT_YCbCr_P010:
317
+ handle.pixelFormat = VideoPixelFormat::NV12;
318
+ break;
319
+ default:
320
+ handle.pixelFormat = VideoPixelFormat::BGRA8;
321
+ break;
322
+ }
323
+ handle.deleter = [buffer]() { AHardwareBuffer_release(buffer); };
324
+ return handle;
325
+ }
205
326
  };
206
327
 
207
328
  } // namespace rnwgpu
@@ -26,6 +26,19 @@ public:
26
26
  void createImageBitmapFromDataAsync(
27
27
  std::span<const uint8_t> data, std::function<void(ImageData)> onSuccess,
28
28
  std::function<void(std::string)> onError) override;
29
+
30
+ VideoFrameHandle loadVideoFrame(const std::string &path) override;
31
+
32
+ VideoFrameHandle createTestVideoFrame(uint32_t width,
33
+ uint32_t height) override;
34
+
35
+ std::unique_ptr<IVideoPlayer>
36
+ createVideoPlayer(const std::string &path,
37
+ VideoPixelFormat format) override;
38
+
39
+ std::string writeTestVideoFile() override;
40
+
41
+ VideoFrameHandle wrapNativeBuffer(void *pointer) override;
29
42
  };
30
43
 
31
44
  } // namespace rnwgpu
@@ -2,10 +2,14 @@
2
2
 
3
3
  #include <TargetConditionals.h>
4
4
 
5
+ #import <AVFoundation/AVFoundation.h>
6
+ #import <CoreVideo/CoreVideo.h>
5
7
  #import <React/RCTBlobManager.h>
6
8
  #import <React/RCTBridge+Private.h>
7
9
  #import <ReactCommon/RCTTurboModule.h>
8
10
 
11
+ #include "AppleVideoPlayer.h"
12
+
9
13
  #include "RNWebGPUManager.h"
10
14
  #include "WebGPUModule.h"
11
15
 
@@ -154,4 +158,145 @@ void ApplePlatformContext::createImageBitmapFromDataAsync(
154
158
  });
155
159
  }
156
160
 
161
+ VideoFrameHandle
162
+ ApplePlatformContext::loadVideoFrame(const std::string &path) {
163
+ NSString *nsPath = [NSString stringWithUTF8String:path.c_str()];
164
+ NSURL *url = [nsPath hasPrefix:@"file://"]
165
+ ? [NSURL URLWithString:nsPath]
166
+ : [NSURL fileURLWithPath:nsPath];
167
+ AVURLAsset *asset = [AVURLAsset assetWithURL:url];
168
+
169
+ NSArray<AVAssetTrack *> *videoTracks =
170
+ [asset tracksWithMediaType:AVMediaTypeVideo];
171
+ if (videoTracks.count == 0) {
172
+ throw std::runtime_error("loadVideoFrame: no video track in file");
173
+ }
174
+ AVAssetTrack *videoTrack = videoTracks.firstObject;
175
+
176
+ NSError *error = nil;
177
+ AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:asset
178
+ error:&error];
179
+ if (error || !reader) {
180
+ throw std::runtime_error(
181
+ std::string("loadVideoFrame: AVAssetReader init failed: ") +
182
+ [[error localizedDescription] UTF8String]);
183
+ }
184
+
185
+ NSDictionary *outputSettings = @{
186
+ (NSString *)kCVPixelBufferPixelFormatTypeKey :
187
+ @(kCVPixelFormatType_32BGRA),
188
+ (NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{},
189
+ (NSString *)kCVPixelBufferMetalCompatibilityKey : @YES,
190
+ };
191
+ AVAssetReaderTrackOutput *output =
192
+ [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack
193
+ outputSettings:outputSettings];
194
+ output.alwaysCopiesSampleData = NO;
195
+ if (![reader canAddOutput:output]) {
196
+ throw std::runtime_error("loadVideoFrame: cannot add output");
197
+ }
198
+ [reader addOutput:output];
199
+
200
+ if (![reader startReading]) {
201
+ throw std::runtime_error(
202
+ std::string("loadVideoFrame: startReading failed: ") +
203
+ [[reader.error localizedDescription] UTF8String]);
204
+ }
205
+
206
+ CMSampleBufferRef sampleBuffer = [output copyNextSampleBuffer];
207
+ if (!sampleBuffer) {
208
+ throw std::runtime_error("loadVideoFrame: no sample buffer");
209
+ }
210
+
211
+ CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
212
+ if (!pixelBuffer) {
213
+ CFRelease(sampleBuffer);
214
+ throw std::runtime_error("loadVideoFrame: no pixel buffer");
215
+ }
216
+
217
+ IOSurfaceRef ioSurface = CVPixelBufferGetIOSurface(pixelBuffer);
218
+ if (!ioSurface) {
219
+ CFRelease(sampleBuffer);
220
+ throw std::runtime_error(
221
+ "loadVideoFrame: pixel buffer is not IOSurface-backed");
222
+ }
223
+
224
+ // Retain the IOSurface so it survives past the sample buffer's lifetime.
225
+ CFRetain(ioSurface);
226
+
227
+ VideoFrameHandle handle;
228
+ handle.handle = (void *)ioSurface;
229
+ handle.width = static_cast<uint32_t>(CVPixelBufferGetWidth(pixelBuffer));
230
+ handle.height = static_cast<uint32_t>(CVPixelBufferGetHeight(pixelBuffer));
231
+ handle.deleter = [ioSurface]() { CFRelease(ioSurface); };
232
+
233
+ CFRelease(sampleBuffer);
234
+ [reader cancelReading];
235
+
236
+ return handle;
237
+ }
238
+
239
+ std::unique_ptr<IVideoPlayer>
240
+ ApplePlatformContext::createVideoPlayer(const std::string &path,
241
+ VideoPixelFormat format) {
242
+ return createAppleVideoPlayer(path, format);
243
+ }
244
+
245
+ std::string ApplePlatformContext::writeTestVideoFile() {
246
+ return writeAppleTestVideoFile();
247
+ }
248
+
249
+ VideoFrameHandle ApplePlatformContext::wrapNativeBuffer(void *pointer) {
250
+ return wrapCVPixelBuffer(static_cast<CVPixelBufferRef>(pointer));
251
+ }
252
+
253
+ VideoFrameHandle
254
+ ApplePlatformContext::createTestVideoFrame(uint32_t width, uint32_t height) {
255
+ NSDictionary *attrs = @{
256
+ (NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{},
257
+ (NSString *)kCVPixelBufferMetalCompatibilityKey : @YES,
258
+ };
259
+ CVPixelBufferRef pixelBuffer = NULL;
260
+ CVReturn err = CVPixelBufferCreate(
261
+ kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA,
262
+ (__bridge CFDictionaryRef)attrs, &pixelBuffer);
263
+ if (err != kCVReturnSuccess || !pixelBuffer) {
264
+ throw std::runtime_error("createTestVideoFrame: CVPixelBufferCreate "
265
+ "failed");
266
+ }
267
+
268
+ CVPixelBufferLockBaseAddress(pixelBuffer, 0);
269
+ uint8_t *base =
270
+ static_cast<uint8_t *>(CVPixelBufferGetBaseAddress(pixelBuffer));
271
+ size_t rowBytes = CVPixelBufferGetBytesPerRow(pixelBuffer);
272
+ for (uint32_t y = 0; y < height; ++y) {
273
+ uint8_t *row = base + y * rowBytes;
274
+ for (uint32_t x = 0; x < width; ++x) {
275
+ // RGB gradient + diagonal stripes, in BGRA byte order.
276
+ uint8_t r = static_cast<uint8_t>((x * 255) / std::max(width - 1, 1u));
277
+ uint8_t g = static_cast<uint8_t>((y * 255) / std::max(height - 1, 1u));
278
+ uint8_t b = static_cast<uint8_t>(((x + y) & 0x20) ? 220 : 30);
279
+ row[x * 4 + 0] = b;
280
+ row[x * 4 + 1] = g;
281
+ row[x * 4 + 2] = r;
282
+ row[x * 4 + 3] = 0xFF;
283
+ }
284
+ }
285
+ CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
286
+
287
+ IOSurfaceRef ioSurface = CVPixelBufferGetIOSurface(pixelBuffer);
288
+ if (!ioSurface) {
289
+ CFRelease(pixelBuffer);
290
+ throw std::runtime_error(
291
+ "createTestVideoFrame: pixel buffer is not IOSurface-backed");
292
+ }
293
+
294
+ VideoFrameHandle handle;
295
+ handle.handle = (void *)ioSurface;
296
+ handle.width = width;
297
+ handle.height = height;
298
+ handle.deleter = [pixelBuffer]() { CFRelease(pixelBuffer); };
299
+ return handle;
300
+ }
301
+
157
302
  } // namespace rnwgpu
@@ -0,0 +1,31 @@
1
+ #pragma once
2
+
3
+ #include "PlatformContext.h"
4
+
5
+ #include <memory>
6
+ #include <string>
7
+
8
+ #ifdef __OBJC__
9
+ #import <CoreVideo/CoreVideo.h>
10
+ #endif
11
+
12
+ namespace rnwgpu {
13
+
14
+ // Factory: creates a new IVideoPlayer backed by AVPlayer +
15
+ // AVPlayerItemVideoOutput. `format` selects the surface layout.
16
+ std::unique_ptr<IVideoPlayer>
17
+ createAppleVideoPlayer(const std::string &path, VideoPixelFormat format);
18
+
19
+ // Generate a small procedurally-animated test video and write it to a
20
+ // temporary file. Returns the absolute path. Used by the SharedTextureMemory
21
+ // example so it doesn't need a bundled .mp4.
22
+ std::string writeAppleTestVideoFile();
23
+
24
+ #ifdef __OBJC__
25
+ // Build a VideoFrameHandle from an existing CVPixelBuffer. CFRetains the
26
+ // pixel buffer so the caller can release their reference immediately. Reads
27
+ // IOSurface, dimensions, pixel format, and YUV matrix off the buffer.
28
+ VideoFrameHandle wrapCVPixelBuffer(CVPixelBufferRef pixelBuffer);
29
+ #endif
30
+
31
+ } // namespace rnwgpu