From 6c21300ac955567ea42251954b34749748a85afb Mon Sep 17 00:00:00 2001
From: Andrei Alecu
Date: Thu, 26 Nov 2026 10:34:05 +0200
Subject: [PATCH 1/2] feat: add VideoFrame.nativeBufferColorSpace, on
react-native-webgpu 0.26.0
The Yarn patches to react-native-webgpu and react-native-skia go away:
- react-native-skia 3.11.1 ships wcandillon/react-native-skia#4510 (Dawn's
AHardwareBuffer size check skipped on Android). The example moves to 3.13.1.
- react-native-webgpu 0.26.0 ships wcandillon/react-native-webgpu#642:
createNativeBufferFromPointer takes a colorSpace. The released option names
color spaces as Dawn does ("rec709", range: "narrow"), not as WebCodecs does
("bt709", fullRange), and throws on the WebCodecs names.
- The iOS 26 EDR part of the patch was not needed
(wcandillon/react-native-webgpu#643 was closed).
VideoFrame.nativeBufferColorSpace is the frame's colorSpace in those names,
for createNativeBufferFromPointer(frame.pixelBuffer, { colorSpace }). Without
it, Android reads BT.709 video with the BT.601 matrix. The README's patch
instructions give way to the minimum versions.
---
...ct-native-skia-npm-3.10.0-c316c6c886.patch | 66 ------
...-native-webgpu-npm-0.23.0-50f3d74c47.patch | 224 ------------------
README.md | 41 +---
docs/guides.md | 14 +-
docs/hdr.md | 4 +-
example/ios/Podfile.lock | 12 +-
example/package.json | 4 +-
example/src/Compress/compress.ts | 2 +-
example/src/VideoExport/export.ts | 4 +-
package.json | 6 +-
src/__tests__/WebCodecs.spec.ts | 4 +-
src/__tests__/unit/colorSpace.test.ts | 75 ++++++
src/index.ts | 1 +
src/lib/colorSpace.ts | 81 +++++++
src/lib/video.ts | 20 ++
yarn.lock | 112 +--------
16 files changed, 218 insertions(+), 452 deletions(-)
delete mode 100644 .yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch
delete mode 100644 .yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch
create mode 100644 src/__tests__/unit/colorSpace.test.ts
create mode 100644 src/lib/colorSpace.ts
diff --git a/.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch b/.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch
deleted file mode 100644
index e985ee2..0000000
--- a/.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch
+++ /dev/null
@@ -1,66 +0,0 @@
-diff --git a/cpp/rnskia/RNDawnUtils.h b/cpp/rnskia/RNDawnUtils.h
-index 67d92cc36f1267fbc6962fbe1e9ab679f71faa25..73ee32feb8ade664399cb66057d9a821788566b0 100644
---- a/cpp/rnskia/RNDawnUtils.h
-+++ b/cpp/rnskia/RNDawnUtils.h
-@@ -13,6 +13,10 @@
-
- #include "webgpu/webgpu_cpp.h"
-
-+#ifdef __ANDROID__
-+#include
-+#endif
-+
- #include "dawn/native/DawnNative.h"
-
- #include "include/core/SkColorSpace.h"
-@@ -170,6 +174,25 @@ inline bool isSwiftShader(const wgpu::Adapter &adapter) {
- toString(info.device).find("SwiftShader") != std::string::npos;
- }
-
-+#ifdef __ANDROID__
-+// Whether the app runs in the Android emulator: ro.boot.qemu is "1" there
-+// (ro.kernel.qemu on older images), whatever renders its Vulkan
-+// (SwiftShader, or the host GPU through gfxstream). Read once.
-+inline bool isAndroidEmulator() {
-+ static const bool emulator = [] {
-+ for (const char *name : {"ro.boot.qemu", "ro.kernel.qemu"}) {
-+ char value[PROP_VALUE_MAX] = {};
-+ if (__system_property_get(name, value) > 0 &&
-+ std::string_view(value) == "1") {
-+ return true;
-+ }
-+ }
-+ return false;
-+ }();
-+ return emulator;
-+}
-+#endif
-+
- // Dawn's own Imagination test (gpu_info::IsImgTec): the PCI vendor ID of
- // Imagination Technologies, whose PowerVR GPUs ship in the Pixel 10 and 11.
- inline bool isImagination(const wgpu::Adapter &adapter) {
-@@ -378,14 +401,16 @@ requestDevice(dawn::native::Adapter &nativeAdapter,
- #endif
- #ifdef __ANDROID__
- // Dawn checks an imported AHardwareBuffer's allocation size against the
-- // VkImage it binds to, and the Android emulator's SwiftShader driver reports
-- // that size as 0 for every buffer. The first import (react-native-webgpu's
-- // view snapshots draw into an imported AHardwareBuffer on Android 13+) then
-- // fails as an internal error, which Dawn turns into a device loss: nothing
-- // renders for the rest of the process. Dawn itself skips the check on the
-- // Samsung, Qualcomm and Huawei drivers that misreport the size; do the same
-- // for SwiftShader. Real devices keep the check.
-- if (isSwiftShader(adapter)) {
-+ // VkImage it binds to, and the Android emulator's drivers report that size
-+ // as 0 for every buffer: SwiftShader, and the host GPU when the emulator
-+ // renders with it (gfxstream, the default on a machine with a GPU, #4508).
-+ // The first import (react-native-webgpu's view snapshots draw into an
-+ // imported AHardwareBuffer on Android 13+) then fails as an internal error,
-+ // which Dawn turns into a device loss: nothing renders for the rest of the
-+ // process. Dawn itself skips the check on the Samsung, Qualcomm and Huawei
-+ // drivers that misreport the size; do the same on the emulator. Real
-+ // devices keep the check.
-+ if (isSwiftShader(adapter) || isAndroidEmulator()) {
- toggles.push_back("ignore_imported_ahardwarebuffer_vulkan_image_size");
- }
- #endif
diff --git a/.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch b/.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch
deleted file mode 100644
index dfe6c24..0000000
--- a/.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch
+++ /dev/null
@@ -1,224 +0,0 @@
-diff --git a/apple/MetalLayerColorSpace.mm b/apple/MetalLayerColorSpace.mm
-index 0e8e7f373ab6a06365b8321e0e2e6afc1ba5c086..783fec992cd5fc707e5e1ce06ba2782584dc388c 100644
---- a/apple/MetalLayerColorSpace.mm
-+++ b/apple/MetalLayerColorSpace.mm
-@@ -50,6 +50,14 @@ void applyCAMetalLayerColorSpace(CAMetalLayer *layer,
- if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) {
- layer.wantsExtendedDynamicRangeContent = extendedRange;
- }
-+ // Since iOS and macOS 26 the flag alone no longer lights EDR: the layer
-+ // asks for it with preferredDynamicRange, and its content is shown above
-+ // SDR white only up to the headroom it declares.
-+ if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) {
-+ layer.preferredDynamicRange =
-+ extendedRange ? CADynamicRangeHigh : CADynamicRangeStandard;
-+ layer.contentsHeadroom = extendedRange ? 16.0 : 0.0;
-+ }
- #else
- (void)extendedRange;
- #endif
-diff --git a/cpp/rnwgpu/api/RNWebGPU.h b/cpp/rnwgpu/api/RNWebGPU.h
-index 10f9dfe04df15a99074f2fe8a8f3473147a5a3fd..b406afe3093985731f7a761b776ffd7edaad774e 100644
---- a/cpp/rnwgpu/api/RNWebGPU.h
-+++ b/cpp/rnwgpu/api/RNWebGPU.h
-@@ -320,9 +320,78 @@ public:
- // Wrap a CVPixelBufferRef / AHardwareBuffer* pointer (typed as void* via
- // BigInt on the JS side) into one of our VideoFrames. The native side
- // CFRetains / acquires so the caller can release immediately.
-- std::shared_ptr createNativeBufferFromPointer(void *pointer) {
-+ //
-+ // createNativeBufferFromPointer(pointer, { colorSpace? }): `colorSpace` (a
-+ // WebCodecs VideoColorSpaceInit, e.g. a VideoFrame's colorSpace) says how to
-+ // read a YUV buffer that carries no color information of its own, as an
-+ // AHardwareBuffer from a MediaCodec ImageReader. The fields it sets replace
-+ // what the buffer says.
-+ jsi::Value createNativeBufferFromPointer(jsi::Runtime &runtime,
-+ const jsi::Value & /*thisVal*/,
-+ const jsi::Value *args,
-+ size_t count) {
-+ if (count < 1 || !args[0].isBigInt()) {
-+ throw jsi::JSError(runtime, "createNativeBufferFromPointer: expected a "
-+ "pointer (BigInt)");
-+ }
-+ void *pointer = JSIConverter::fromJSI(runtime, args[0], false);
- auto handle = _platformContext->wrapNativeBuffer(pointer);
-- return std::make_shared(std::move(handle));
-+ if (count > 1 && args[1].isObject()) {
-+ auto colorSpace =
-+ args[1].getObject(runtime).getProperty(runtime, "colorSpace");
-+ if (colorSpace.isObject()) {
-+ applyVideoColorSpace(runtime, colorSpace.getObject(runtime),
-+ handle.colorSpace);
-+ }
-+ }
-+ return JSIConverter>::toJSI(
-+ runtime, std::make_shared(std::move(handle)));
-+ }
-+
-+ // A WebCodecs VideoColorSpaceInit onto the color space of a native frame.
-+ static void applyVideoColorSpace(jsi::Runtime &runtime,
-+ const jsi::Object &init,
-+ VideoFrameColorSpace &colorSpace) {
-+ auto field = [&](const char *name) -> std::string {
-+ auto value = init.getProperty(runtime, name);
-+ return value.isString() ? value.getString(runtime).utf8(runtime) : "";
-+ };
-+ const auto primaries = field("primaries");
-+ if (primaries == "bt709") {
-+ colorSpace.primaries = wgpu::ColorSpacePrimariesDawn::SRGB;
-+ } else if (primaries == "bt470bg" || primaries == "smpte170m") {
-+ colorSpace.primaries = wgpu::ColorSpacePrimariesDawn::Rec601;
-+ } else if (primaries == "bt2020") {
-+ colorSpace.primaries = wgpu::ColorSpacePrimariesDawn::Rec2020;
-+ } else if (primaries == "smpte432") {
-+ colorSpace.primaries = wgpu::ColorSpacePrimariesDawn::DisplayP3;
-+ }
-+ const auto transfer = field("transfer");
-+ if (transfer == "bt709" || transfer == "smpte170m") {
-+ colorSpace.transfer = wgpu::ColorSpaceTransferDawn::SMPTE_170M;
-+ } else if (transfer == "iec61966-2-1") {
-+ colorSpace.transfer = wgpu::ColorSpaceTransferDawn::SRGB;
-+ } else if (transfer == "linear") {
-+ colorSpace.transfer = wgpu::ColorSpaceTransferDawn::Identity;
-+ } else if (transfer == "pq") {
-+ colorSpace.transfer = wgpu::ColorSpaceTransferDawn::PQ;
-+ } else if (transfer == "hlg") {
-+ colorSpace.transfer = wgpu::ColorSpaceTransferDawn::HLG;
-+ }
-+ const auto matrix = field("matrix");
-+ if (matrix == "bt709") {
-+ colorSpace.matrix = wgpu::ColorSpaceYCbCrMatrixDawn::Rec709;
-+ } else if (matrix == "bt470bg" || matrix == "smpte170m") {
-+ colorSpace.matrix = wgpu::ColorSpaceYCbCrMatrixDawn::Rec601;
-+ } else if (matrix == "bt2020-ncl") {
-+ colorSpace.matrix = wgpu::ColorSpaceYCbCrMatrixDawn::Rec2020;
-+ }
-+ auto fullRange = init.getProperty(runtime, "fullRange");
-+ if (fullRange.isBool()) {
-+ colorSpace.range = fullRange.getBool()
-+ ? wgpu::ColorSpaceYCbCrRangeDawn::Full
-+ : wgpu::ColorSpaceYCbCrRangeDawn::Narrow;
-+ }
- }
-
- // createNativeBuffer(width, height, { format?, colorSpace? }): an empty
-diff --git a/lib/typescript/src/index.d.ts b/lib/typescript/src/index.d.ts
-index a5ad02fc69fc5c3b75cc2a5de69e3e2959bf6d24..f7c2bd59a28ad928f533416d387c0e2056b4c7d9 100644
---- a/lib/typescript/src/index.d.ts
-+++ b/lib/typescript/src/index.d.ts
-@@ -1,10 +1,10 @@
- import "./main/installNative";
--import type { GPUDrawElementImageDestination, GPUDrawElementImageSource, GPUSharedFence, GPUSharedFenceDescriptor, GPUSharedFenceState, CreateNativeBufferOptions, GPUDawnTogglesDescriptor, GPUNativeExternalTextureDescriptor, GPUSharedTextureMemory, GPUSharedTextureMemoryDescriptor, GPUCopyExternalNativeFrameSourceInfo, NativeCanvas, RNCanvasContext, VideoPlayer, NativeBuffer, NativeBufferPixelFormat } from "./types";
-+import type { GPUDrawElementImageDestination, GPUDrawElementImageSource, GPUSharedFence, GPUSharedFenceDescriptor, GPUSharedFenceState, CreateNativeBufferOptions, CreateNativeBufferFromPointerOptions, GPUDawnTogglesDescriptor, GPUNativeExternalTextureDescriptor, GPUSharedTextureMemory, GPUSharedTextureMemoryDescriptor, GPUCopyExternalNativeFrameSourceInfo, NativeCanvas, RNCanvasContext, VideoPlayer, NativeBuffer, NativeBufferPixelFormat } from "./types";
- export * from "./main";
- export * from "./constants";
- export { installWebGPU } from "./install";
- export { requestAnimationFrame, cancelAnimationFrame } from "./animationFrame";
--export type { NativeBuffer, VideoPlayer, GPUSharedFence, GPUSharedFenceDescriptor, GPUSharedFenceExportInfo, GPUSharedFenceState, GPUSharedFenceType, GPUSharedTextureMemory, GPUSharedTextureMemoryDescriptor, GPUSharedTextureMemoryEndAccessState, NativeBufferPixelFormat, CreateVideoPlayerOptions, GPUCopyExternalNativeFrameSourceInfo, GPUDawnTogglesDescriptor, GPUNativeExternalTextureDescriptor, NativeBufferColorSpace, GPUExtent2D, GPUDrawElementImageDestination, GPUDrawElementImageSource, GPUDrawElementImageSourceView, CreateNativeBufferOptions, } from "./types";
-+export type { NativeBuffer, VideoPlayer, GPUSharedFence, GPUSharedFenceDescriptor, GPUSharedFenceExportInfo, GPUSharedFenceState, GPUSharedFenceType, GPUSharedTextureMemory, GPUSharedTextureMemoryDescriptor, GPUSharedTextureMemoryEndAccessState, NativeBufferPixelFormat, CreateVideoPlayerOptions, GPUCopyExternalNativeFrameSourceInfo, GPUDawnTogglesDescriptor, GPUNativeExternalTextureDescriptor, NativeBufferColorSpace, GPUExtent2D, GPUDrawElementImageDestination, GPUDrawElementImageSource, GPUDrawElementImageSourceView, CreateNativeBufferOptions, CreateNativeBufferFromPointerOptions, } from "./types";
- declare global {
- interface Navigator {
- gpu: GPU;
-@@ -26,7 +26,7 @@ declare global {
- createImageBitmap: typeof createImageBitmap;
- loadVideoFrame: (path: string) => NativeBuffer;
- createTestVideoFrame: (width: number, height: number, pixelFormat?: NativeBufferPixelFormat) => NativeBuffer;
-- createNativeBufferFromPointer: (pointer: bigint) => NativeBuffer;
-+ createNativeBufferFromPointer: (pointer: bigint, options?: CreateNativeBufferFromPointerOptions) => NativeBuffer;
- importDevice: (pointer: bigint) => GPUDevice;
- adoptTexture: (pointer: bigint) => GPUTexture;
- createVideoPlayer: (source: string, pixelFormat?: NativeBufferPixelFormat) => VideoPlayer;
-diff --git a/lib/typescript/src/types.d.ts b/lib/typescript/src/types.d.ts
-index 8cc69fbf5c0bf3b419035b79068cdf9e8b020bb6..cf658bb14b7a28d43bceb3b445a2561bec4f2e2d 100644
---- a/lib/typescript/src/types.d.ts
-+++ b/lib/typescript/src/types.d.ts
-@@ -141,6 +141,14 @@ export interface GPUSharedTextureMemory {
- * The options of `createNativeBuffer()`. The buffer holds premultiplied
- * pixels.
- */
-+export interface CreateNativeBufferFromPointerOptions {
-+ colorSpace?: {
-+ primaries?: string | null;
-+ transfer?: string | null;
-+ matrix?: string | null;
-+ fullRange?: boolean | null;
-+ };
-+}
- export interface CreateNativeBufferOptions {
- /**
- * Defaults to the preferred canvas format of the platform:
-diff --git a/src/index.tsx b/src/index.tsx
-index 3bc1671f4071ab30d19b5182c0f1b07377c39a9d..1729b018af9f13e23e9c0fa21785070414935cf3 100644
---- a/src/index.tsx
-+++ b/src/index.tsx
-@@ -15,6 +15,7 @@ import type {
- GPUSharedFenceDescriptor,
- GPUSharedFenceState,
- CreateNativeBufferOptions,
-+ CreateNativeBufferFromPointerOptions,
- GPUDawnTogglesDescriptor,
- GPUNativeExternalTextureDescriptor,
- GPUSharedTextureMemory,
-@@ -108,7 +109,10 @@ declare global {
- // Wrap a NativeBuffer.pointer (CVPixelBufferRef on iOS / AHardwareBuffer*
- // on Android) into a NativeBuffer. Matches the shape used by libraries
- // that emit NativeBuffer (e.g. react-native-vision-camera).
-- createNativeBufferFromPointer: (pointer: bigint) => NativeBuffer;
-+ createNativeBufferFromPointer: (
-+ pointer: bigint,
-+ options?: CreateNativeBufferFromPointerOptions,
-+ ) => NativeBuffer;
- // Wrap an externally created WGPUDevice pointer (e.g. Skia's Graphite
- // device from Skia.getNativeDevice()) into a GPUDevice. Requires the
- // exporter to link this process's single Dawn copy.
-diff --git a/src/types.ts b/src/types.ts
-index b0bb67435eba33160a17f1f2def0b237002d4721..fcd66ab125f3663bf3ca345aade7e17859a11f1d 100644
---- a/src/types.ts
-+++ b/src/types.ts
-@@ -328,6 +328,21 @@ export interface GPUSharedTextureMemory {
- destroy(): void;
- }
-
-+export interface CreateNativeBufferFromPointerOptions {
-+ /**
-+ * How to read a YUV buffer that carries no color information of its own
-+ * (an AHardwareBuffer from a MediaCodec ImageReader, for instance): a
-+ * WebCodecs `VideoColorSpaceInit`, such as a `VideoFrame`'s `colorSpace`.
-+ * The fields it sets replace what the buffer says.
-+ */
-+ colorSpace?: {
-+ primaries?: string | null;
-+ transfer?: string | null;
-+ matrix?: string | null;
-+ fullRange?: boolean | null;
-+ };
-+}
-+
- /**
- * The options of `createNativeBuffer()`. The buffer holds premultiplied
- * pixels.
-diff --git a/src/video.ts b/src/video.ts
-index 131473fda30a978d4eaf7334e782f0e78bf85268..9f70fca6310541e82470fa64ff09bdffbe3d4b5e 100644
---- a/src/video.ts
-+++ b/src/video.ts
-@@ -1,4 +1,5 @@
- import type {
-+ CreateNativeBufferFromPointerOptions,
- CreateNativeBufferOptions,
- CreateVideoPlayerOptions,
- NativeBuffer,
-@@ -51,9 +52,13 @@ export const createVideoPlayer = (
- * VisionCamera's `frame.getNativeBuffer().pointer`) into a `NativeBuffer`
- * that keeps it alive until released.
- */
--export const createNativeBufferFromPointer = (pointer: bigint): NativeBuffer =>
-+export const createNativeBufferFromPointer = (
-+ pointer: bigint,
-+ options?: CreateNativeBufferFromPointerOptions,
-+): NativeBuffer =>
- native("createNativeBufferFromPointer").createNativeBufferFromPointer(
- pointer,
-+ options,
- );
-
- /**
diff --git a/README.md b/README.md
index 94ec750..dd32708 100644
--- a/README.md
+++ b/README.md
@@ -36,9 +36,7 @@ GPU fences between them, never pixels through JavaScript.
**Status:** young (0.x). Tested on the iOS Simulator, an Android emulator, one
iPhone and, for the web build, Chromium. H.264 and AAC everywhere, HEVC on
-iOS, HDR export on iOS, Android and the web. Some paths need
-[changes not yet released](#unreleased-changes-in-other-libraries) in other
-libraries.
+iOS, HDR export on iOS, Android and the web.
## Installation
@@ -102,25 +100,13 @@ To draw frames or read decoded ones on the GPU, add
(`react-native-webgpu`), and [React Native Skia](https://shopify.github.io/react-native-skia/)
(`react-native-skia`) if you draw with Skia.
-### Temporary patches
+### Versions
-Until [three pull requests](#unreleased-changes-in-other-libraries) to
-William Candillon's react-native-webgpu and react-native-skia are released,
-some paths need them applied as patches. Exports of SDR video work without
-them. With Yarn, copy the patch files from
-[`.yarn/patches`](.yarn/patches) into your app and point `resolutions` at
-them:
-
-```json
-"resolutions": {
- "react-native-webgpu@0.23.0": "patch:react-native-webgpu@npm%3A0.23.0#./.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch",
- "react-native-skia@3.10.0": "patch:react-native-skia@npm%3A3.10.0#./.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch"
-}
-```
-
-With npm or pnpm, apply the same diffs with
-[patch-package](https://github.com/ds300/patch-package) or `pnpm patch`.
-The patches are for those exact versions, and go away once the changes ship.
+React Native WebGPU 0.26.0 or later: reading decoded video into WebGPU with
+the right colors on Android takes the `colorSpace` option of
+`createNativeBufferFromPointer` ([Read video into WebGPU](docs/guides.md#read-video-into-webgpu)).
+React Native Skia 3.11.1 or later, if Skia draws into pool surfaces on the
+Android emulator.
## Codecs
@@ -276,19 +262,6 @@ including running it on a Mac.
A 20-second 1080p HEVC phone video, 40.3 MB, compressed to 720p H.264: 5.3 MB, in 3.2 s on the iOS Simulator. How it works.
-## Unreleased changes in other libraries
-
-Some paths need changes that are not released yet. This repository applies
-them as Yarn patches (`.yarn/patches`); an app needs the same until they
-ship (see [Temporary patches](#temporary-patches)). This is temporary,
-pending review by William Candillon, who maintains both libraries.
-
-| Change | Needed for |
-| :-- | :-- |
-| [wcandillon/react-native-webgpu#642](https://github.com/wcandillon/react-native-webgpu/pull/642) | Correct colors when reading decoded video into WebGPU on Android. |
-| [wcandillon/react-native-webgpu#643](https://github.com/wcandillon/react-native-webgpu/pull/643) | HDR preview on a WebGPU canvas on iOS 26. Exports do not need it. |
-| [wcandillon/react-native-skia#4510](https://github.com/wcandillon/react-native-skia/pull/4510) | Skia drawing into pool surfaces on the Android emulator. Real devices do not need it. |
-
## Acknowledgements
Set up from [William Candillon](https://github.com/wcandillon)'s
diff --git a/docs/guides.md b/docs/guides.md
index 1c1fc1f..0d10298 100644
--- a/docs/guides.md
+++ b/docs/guides.md
@@ -88,9 +88,7 @@ await output.finalize();
The texture must belong to Skia's own device, hence `importDevice(Skia.getNativeDevice())`.
On the Android emulator, Skia's device importing the pool's buffers needs
-[react-native-skia#4510](https://github.com/wcandillon/react-native-skia/pull/4510);
-see [Related changes](../README.md#unreleased-changes-in-other-libraries). Real devices are
-not affected.
+react-native-skia 3.11.1 or later. Real devices are not affected.
## Sound
@@ -145,7 +143,7 @@ const frame = sample.toVideoFrame() as unknown as VideoFrame;
sample.close();
const buffer = RNWebGPU.createNativeBufferFromPointer(frame.pixelBuffer, {
- colorSpace: frame.colorSpace.toJSON(),
+ colorSpace: frame.nativeBufferColorSpace,
});
device.queue.copyExternalImageToTexture(
{ source: buffer },
@@ -165,9 +163,11 @@ The frame's `colorSpace` comes from the stream (Mediabunny passes the
track's to `configure`). Android's buffers carry no color information of
their own, so pass it to `createNativeBufferFromPointer`: without it,
BT.709 video is read with the BT.601 matrix, and pure red comes out as
-(225, 0, 31). That option is
-[wcandillon/react-native-webgpu#642](https://github.com/wcandillon/react-native-webgpu/pull/642),
-not in a release yet (see [Related changes](../README.md#unreleased-changes-in-other-libraries)).
+(225, 0, 31). The option takes react-native-webgpu 0.26.0 or later, and
+names color spaces as Dawn does (`"rec709"`, `range: "narrow"`), not as
+WebCodecs does (`"bt709"`, `fullRange: false`): `frame.nativeBufferColorSpace`
+is the frame's `colorSpace` in those names. On iOS the pixel buffer has its
+own, and passing it changes nothing.
Close every frame when you are done with it. Android's decoder stalls while
JavaScript holds all of its output frames.
diff --git a/docs/hdr.md b/docs/hdr.md
index 11c3017..b743a2f 100644
--- a/docs/hdr.md
+++ b/docs/hdr.md
@@ -41,9 +41,7 @@ about 1000 nits, HLG's peak.
The same screen previews the animation in HDR before export, on a WebGPU
canvas with `toneMapping: { mode: "extended" }`
-([`HdrPreview.tsx`](../example/src/VideoExport/HdrPreview.tsx)). On
-iOS 26 and later that needs
-[wcandillon/react-native-webgpu#643](https://github.com/wcandillon/react-native-webgpu/pull/643).
+([`HdrPreview.tsx`](../example/src/VideoExport/HdrPreview.tsx)).
The iOS build also runs on an Apple silicon Mac as a "Designed for iPad" app,
and the preview shows in HDR there too, on the MacBook's XDR display (checked
by eye; the Mac export has not been measured).
diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock
index 8b839be..7821ea4 100644
--- a/example/ios/Podfile.lock
+++ b/example/ios/Podfile.lock
@@ -38,7 +38,7 @@ PODS:
- ReactCommon/turbomodule/core
- ReactNativeDependencies
- Yoga
- - NitroWebCodecs (0.0.0):
+ - NitroWebCodecs (0.1.2):
- hermes-engine
- NitroModules
- RCTRequired
@@ -1470,7 +1470,7 @@ PODS:
- ReactCommon/turbomodule/core
- ReactNativeDependencies
- Yoga
- - react-native-skia (3.10.0):
+ - react-native-skia (3.13.1):
- hermes-engine
- RCTRequired
- RCTTypeSafety
@@ -1495,7 +1495,7 @@ PODS:
- ReactCommon/turbomodule/core
- ReactNativeDependencies
- Yoga
- - react-native-webgpu (0.23.0):
+ - react-native-webgpu (0.26.0):
- hermes-engine
- RCTRequired
- RCTTypeSafety
@@ -2507,7 +2507,7 @@ SPEC CHECKSUMS:
glog: 3cf7cb46f743c4bc758ae3e6d9c15bd1b4cfd6d4
hermes-engine: 84812cfd4974d62f62521511b2f1826f4edf3544
NitroModules: f1a11047ebbb0202a7d0033d6e99ad0527d34b00
- NitroWebCodecs: e30f1a26a3a6ce35b76ae9130f8573ae51926b4d
+ NitroWebCodecs: da03c3d8d8b626911fa601545ea7e1f3895f44b6
RCT-Folly: 82921e585c0400eda46fac1068297aa9dfb035e4
RCTDeprecation: c62b9b3fbf0d26b76755f3ebd1c913a4fb469dde
RCTRequired: d0563e2484633b657976195f0543e180133afd3a
@@ -2549,8 +2549,8 @@ SPEC CHECKSUMS:
React-microtasksnativemodule: 0207d6133274a7cbfc3d0dae326e3055c8cfcf9e
React-mutationobservernativemodule: b97a67c29f724d18a92379e7390f24350acb433a
react-native-safe-area-context: 56700b839169ce88ffcec68c40447916fe512462
- react-native-skia: 8bc263ce99daef1d357c65681e5085b1d3d95157
- react-native-webgpu: bfb5c38c9e985bc1492d497108253d5848376e5f
+ react-native-skia: f14a3198e8e004c9865987258c9c4dba874a076b
+ react-native-webgpu: b363c118a58d82d5ac694b6bde22ee128d653121
React-NativeModulesApple: 8cfb2f308f48c48068c96b932e7f0e4297c23342
React-networking: 3325cf73ecbdaacab0da91260b094baf36061a56
React-oscompat: a776760fdfa152e12114f8c30d833963daac27e1
diff --git a/example/package.json b/example/package.json
index b25529f..13119ac 100644
--- a/example/package.json
+++ b/example/package.json
@@ -31,10 +31,10 @@
"react-native-reanimated": "4.7.1",
"react-native-safe-area-context": "^5.4.0",
"react-native-screens": "^4.30.1",
- "react-native-skia": "3.10.0",
+ "react-native-skia": "3.13.1",
"react-native-web": "^0.21.2",
"react-native-webcodecs": "workspace:*",
- "react-native-webgpu": "0.23.0",
+ "react-native-webgpu": "0.26.0",
"react-native-worklets": "0.13.1"
},
"devDependencies": {
diff --git a/example/src/Compress/compress.ts b/example/src/Compress/compress.ts
index 13e1f2e..407adf2 100644
--- a/example/src/Compress/compress.ts
+++ b/example/src/Compress/compress.ts
@@ -158,7 +158,7 @@ const makeScaler = (width: number, height: number, rotation: number) => {
}
const decoded = RNWebGPU.createNativeBufferFromPointer(
frame.pixelBuffer!,
- { colorSpace: frame.colorSpace.toJSON() },
+ { colorSpace: frame.nativeBufferColorSpace },
);
device.queue.copyExternalImageToTexture(
{ source: decoded },
diff --git a/example/src/VideoExport/export.ts b/example/src/VideoExport/export.ts
index b212744..548f671 100644
--- a/example/src/VideoExport/export.ts
+++ b/example/src/VideoExport/export.ts
@@ -100,8 +100,8 @@ const drawSkottie = (
// Skia draws frame `index` straight into a pool surface, on its own device:
// no copy, no readback. The encoder waits for Skia's GPU work through the
-// fence. (On the Android emulator this needs react-native-skia#4510, applied
-// here as a patch.)
+// fence. (On the Android emulator this needs react-native-skia 3.11.1 or
+// later.)
const drawFrame = (
animation: SkSkottieAnimation,
pool: Pool,
diff --git a/package.json b/package.json
index 63a0aab..8626255 100644
--- a/package.json
+++ b/package.json
@@ -88,7 +88,7 @@
"react-native": "0.88.1",
"react-native-builder-bob": "^0.23.2",
"react-native-nitro-modules": "0.37.2",
- "react-native-webgpu": "0.23.0",
+ "react-native-webgpu": "0.26.0",
"semantic-release": "^25.0.9",
"semantic-release-yarn": "^3.0.2",
"typescript": "^5.2.2",
@@ -117,9 +117,7 @@
],
"resolutions": {
"@types/react": "^19.2.0",
- "@webgpu/types": "^0.1.42",
- "react-native-webgpu@0.23.0": "patch:react-native-webgpu@npm%3A0.23.0#./.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch",
- "react-native-skia@3.10.0": "patch:react-native-skia@npm%3A3.10.0#./.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch"
+ "@webgpu/types": "^0.1.42"
},
"packageManager": "yarn@3.6.4"
}
diff --git a/src/__tests__/WebCodecs.spec.ts b/src/__tests__/WebCodecs.spec.ts
index 6a8cc81..bf6f037 100644
--- a/src/__tests__/WebCodecs.spec.ts
+++ b/src/__tests__/WebCodecs.spec.ts
@@ -1160,7 +1160,7 @@ describe("react-native-webcodecs + Mediabunny", () => {
// whose color space comes from the frame).
const buffer = RNWebGPU.createNativeBufferFromPointer(
frame.pixelBuffer,
- { colorSpace: frame.colorSpace.toJSON() },
+ { colorSpace: frame.nativeBufferColorSpace },
);
const { format, timestamp } = frame;
const size = 128;
@@ -4030,7 +4030,7 @@ describe("react-native-webcodecs + Mediabunny", () => {
const height = frame.displayHeight;
const buffer = RNWebGPU.createNativeBufferFromPointer(
frame.pixelBuffer,
- { colorSpace: frame.colorSpace.toJSON() },
+ { colorSpace: frame.nativeBufferColorSpace },
);
const texture = device.createTexture({
size: [width, height],
diff --git a/src/__tests__/unit/colorSpace.test.ts b/src/__tests__/unit/colorSpace.test.ts
new file mode 100644
index 0000000..dbe830e
--- /dev/null
+++ b/src/__tests__/unit/colorSpace.test.ts
@@ -0,0 +1,75 @@
+import { toNativeBufferColorSpace } from "../../lib/colorSpace";
+
+// What react-native-webgpu's createNativeBufferFromPointer takes, from the
+// color spaces decoders put on their frames.
+describe("toNativeBufferColorSpace", () => {
+ it("names BT.709 limited range as Dawn does", () => {
+ expect(
+ toNativeBufferColorSpace({
+ primaries: "bt709",
+ transfer: "bt709",
+ matrix: "bt709",
+ fullRange: false,
+ }),
+ ).toEqual({
+ primaries: "srgb",
+ transfer: "smpte-170m",
+ matrix: "rec709",
+ range: "narrow",
+ });
+ });
+
+ it("names BT.601 full range", () => {
+ expect(
+ toNativeBufferColorSpace({
+ primaries: "smpte170m",
+ transfer: "smpte170m",
+ matrix: "smpte170m",
+ fullRange: true,
+ }),
+ ).toEqual({
+ primaries: "rec601",
+ transfer: "smpte-170m",
+ matrix: "rec601",
+ range: "full",
+ });
+ });
+
+ it("names HLG and PQ in BT.2020", () => {
+ expect(
+ toNativeBufferColorSpace({
+ primaries: "bt2020",
+ transfer: "hlg",
+ matrix: "bt2020-ncl",
+ }),
+ ).toEqual({ primaries: "rec2020", transfer: "hlg", matrix: "rec2020" });
+ expect(toNativeBufferColorSpace({ transfer: "pq" })).toEqual({
+ transfer: "pq",
+ });
+ });
+
+ it("names sRGB, Display P3 and linear", () => {
+ expect(
+ toNativeBufferColorSpace({
+ primaries: "smpte432",
+ transfer: "iec61966-2-1",
+ matrix: "bt470bg",
+ }),
+ ).toEqual({ primaries: "display-p3", transfer: "srgb", matrix: "rec601" });
+ expect(toNativeBufferColorSpace({ transfer: "linear" })).toEqual({
+ transfer: "identity",
+ });
+ });
+
+ it("leaves out what is unset or has no name, so the buffer's own stay", () => {
+ expect(
+ toNativeBufferColorSpace({
+ primaries: null,
+ transfer: "gamma22curve",
+ matrix: "toString",
+ fullRange: null,
+ }),
+ ).toEqual({});
+ expect(toNativeBufferColorSpace({})).toEqual({});
+ });
+});
diff --git a/src/index.ts b/src/index.ts
index d562d22..ef017e9 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -19,6 +19,7 @@ import {
export * from "./lib/audio";
export * from "./lib/background";
export * from "./lib/files";
+export type { NativeBufferColorSpace } from "./lib/colorSpace";
export type { WebCodecsError } from "./lib/native";
export * from "./lib/video";
diff --git a/src/lib/colorSpace.ts b/src/lib/colorSpace.ts
new file mode 100644
index 0000000..e770c70
--- /dev/null
+++ b/src/lib/colorSpace.ts
@@ -0,0 +1,81 @@
+/**
+ * A color space as react-native-webgpu names it, Dawn's names: what its
+ * `createNativeBufferFromPointer(pointer, { colorSpace })` takes (its
+ * `NativeBufferColorSpace`). Not in WebCodecs.
+ */
+export interface NativeBufferColorSpace {
+ matrix?: "identity" | "rec601" | "rec709" | "rec2020";
+ range?: "identity" | "narrow" | "full";
+ primaries?: "srgb" | "rec709" | "rec601" | "rec2020" | "display-p3";
+ transfer?:
+ | "identity"
+ | "srgb"
+ | "display-p3"
+ | "smpte-170m"
+ | "hlg"
+ | "pq"
+ | "bt-1886";
+}
+
+/** @internal The members of a WebCodecs color space it reads. */
+export interface WebCodecsColorSpace {
+ primaries?: string | null;
+ transfer?: string | null;
+ matrix?: string | null;
+ fullRange?: boolean | null;
+}
+
+const PRIMARIES: Record = {
+ bt709: "srgb",
+ bt470bg: "rec601",
+ smpte170m: "rec601",
+ bt2020: "rec2020",
+ smpte432: "display-p3",
+};
+
+const TRANSFER: Record = {
+ bt709: "smpte-170m",
+ smpte170m: "smpte-170m",
+ "iec61966-2-1": "srgb",
+ linear: "identity",
+ pq: "pq",
+ hlg: "hlg",
+};
+
+const MATRIX: Record = {
+ bt709: "rec709",
+ bt470bg: "rec601",
+ smpte170m: "rec601",
+ "bt2020-ncl": "rec2020",
+};
+
+const lookup = (table: Record, name?: string | null) =>
+ name != null && Object.prototype.hasOwnProperty.call(table, name)
+ ? table[name]
+ : undefined;
+
+/**
+ * @internal A WebCodecs color space in Dawn's names. Members it has no name
+ * for are left out, so the buffer's own stay.
+ */
+export const toNativeBufferColorSpace = (
+ colorSpace: WebCodecsColorSpace,
+): NativeBufferColorSpace => {
+ const result: NativeBufferColorSpace = {};
+ const primaries = lookup(PRIMARIES, colorSpace.primaries);
+ if (primaries) {
+ result.primaries = primaries;
+ }
+ const transfer = lookup(TRANSFER, colorSpace.transfer);
+ if (transfer) {
+ result.transfer = transfer;
+ }
+ const matrix = lookup(MATRIX, colorSpace.matrix);
+ if (matrix) {
+ result.matrix = matrix;
+ }
+ if (typeof colorSpace.fullRange === "boolean") {
+ result.range = colorSpace.fullRange ? "full" : "narrow";
+ }
+ return result;
+};
diff --git a/src/lib/video.ts b/src/lib/video.ts
index c1c86e8..e2f411b 100644
--- a/src/lib/video.ts
+++ b/src/lib/video.ts
@@ -8,6 +8,8 @@ import type {
} from "../specs/WebCodecs.nitro";
import { avcCodedSize } from "./avc";
+import type { NativeBufferColorSpace } from "./colorSpace";
+import { toNativeBufferColorSpace } from "./colorSpace";
import {
cloneConfig,
CodecBase,
@@ -301,6 +303,24 @@ export class VideoFrame {
return this._surface ? this._surface.pixelBuffer : null;
}
+ /**
+ * Not in WebCodecs: `colorSpace` as react-native-webgpu's
+ * createNativeBufferFromPointer takes it, next to `pixelBuffer`:
+ *
+ * ```ts
+ * RNWebGPU.createNativeBufferFromPointer(frame.pixelBuffer, {
+ * colorSpace: frame.nativeBufferColorSpace,
+ * });
+ * ```
+ *
+ * A decoder's buffers on Android carry no color information of their own:
+ * without it, BT.709 video is read with the BT.601 matrix. On Apple
+ * platforms the pixel buffer has its own, and this repeats them.
+ */
+ get nativeBufferColorSpace(): NativeBufferColorSpace {
+ return toNativeBufferColorSpace(this.colorSpace);
+ }
+
/** @internal */
_nativeSurface(): NativeSurface {
if (!this._surface) {
diff --git a/yarn.lock b/yarn.lock
index 3e31aa1..91962ff 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4042,11 +4042,11 @@ __metadata:
react-native-reanimated: 4.7.1
react-native-safe-area-context: ^5.4.0
react-native-screens: ^4.30.1
- react-native-skia: 3.10.0
+ react-native-skia: 3.13.1
react-native-test-app: 5.4.17
react-native-web: ^0.21.2
react-native-webcodecs: "workspace:*"
- react-native-webgpu: 0.23.0
+ react-native-webgpu: 0.26.0
react-native-worklets: 0.13.1
react-test-renderer: 19.3.0
typescript: ~5.6.3
@@ -11352,73 +11352,11 @@ __metadata:
languageName: node
linkType: hard
-"react-native-skia-graphite-android@npm:158.0.0":
- version: 158.0.0
- resolution: "react-native-skia-graphite-android@npm:158.0.0"
- checksum: b8306f52ce35761df064f4f0103f977802fc48cfe43d3b354ece441a34c7d4841ce3626186d9ffb53e6f1d62ba4d10845d71d87b9aa4c923228d4c410390d3c0
- languageName: node
- linkType: hard
-
-"react-native-skia-graphite-apple-ios@npm:158.0.0":
- version: 158.0.0
- resolution: "react-native-skia-graphite-apple-ios@npm:158.0.0"
- checksum: be8cea926a20756cf48088cdc75ca9b4b4b4c328e4cd7ad846c6d31d4fbdf6114dbfa00d546b83541a86534160e2bf3ae53bc1d378dfee57e7d44e09d7a0542b
- languageName: node
- linkType: hard
-
-"react-native-skia-graphite-apple-macos@npm:158.0.0":
- version: 158.0.0
- resolution: "react-native-skia-graphite-apple-macos@npm:158.0.0"
- checksum: 61b14ad117919ce1215c1e44ba5f478cd97ebabead39df71a069e06992e6385fb1d85406fcc898c5d189e665e899ccd02b816cd34a389607ea765a7f47bc129a
- languageName: node
- linkType: hard
-
-"react-native-skia-graphite-apple-tvos@npm:158.0.0":
- version: 158.0.0
- resolution: "react-native-skia-graphite-apple-tvos@npm:158.0.0"
- checksum: eb4191148323277e0a4c5c0ef9fc38afc8b55ff629badf7c7c23cddb2c6fb9ef328e976c3758c7637e76ab0122155365cebad9fad0587aefc06a0fa3898711b9
- languageName: node
- linkType: hard
-
-"react-native-skia@npm:3.10.0":
- version: 3.10.0
- resolution: "react-native-skia@npm:3.10.0"
- dependencies:
- canvaskit-wasm: 0.41.1
- react-native-skia-graphite-android: 158.0.0
- react-native-skia-graphite-apple-ios: 158.0.0
- react-native-skia-graphite-apple-macos: 158.0.0
- react-native-skia-graphite-apple-tvos: 158.0.0
- react-native-webgpu-dawn: 158.0.0
- react-reconciler: 0.33.0
- peerDependencies:
- react: ">=19.0"
- react-native: ">=0.79"
- react-native-reanimated: ">=4.0.0"
- react-native-worklets: ">=0.7.0"
- peerDependenciesMeta:
- react-native:
- optional: true
- react-native-reanimated:
- optional: true
- react-native-worklets:
- optional: true
- bin:
- setup-skia-web: scripts/setup-canvaskit.js
- checksum: 1827e9b8af63d0d5c2b08dbc1348f449d6e3590e765efdd517c32f90b15e1673cd760c13a35b645827c91ceb4c8c2d00ad34ca551b7a72c1ee4dfd1a1ce00305
- languageName: node
- linkType: hard
-
-"react-native-skia@patch:react-native-skia@npm%3A3.10.0#./.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch::locator=react-native-webcodecs%40workspace%3A.":
- version: 3.10.0
- resolution: "react-native-skia@patch:react-native-skia@npm%3A3.10.0#./.yarn/patches/react-native-skia-npm-3.10.0-c316c6c886.patch::version=3.10.0&hash=bbabb5&locator=react-native-webcodecs%40workspace%3A."
+"react-native-skia@npm:3.13.1":
+ version: 3.13.1
+ resolution: "react-native-skia@npm:3.13.1"
dependencies:
canvaskit-wasm: 0.41.1
- react-native-skia-graphite-android: 158.0.0
- react-native-skia-graphite-apple-ios: 158.0.0
- react-native-skia-graphite-apple-macos: 158.0.0
- react-native-skia-graphite-apple-tvos: 158.0.0
- react-native-webgpu-dawn: 158.0.0
react-reconciler: 0.33.0
peerDependencies:
react: ">=19.0"
@@ -11434,7 +11372,7 @@ __metadata:
optional: true
bin:
setup-skia-web: scripts/setup-canvaskit.js
- checksum: 72008b5140b48e59f245f7d3243401d7958586193bebd98d68984a4fca58fefa8758c7e3046f434c7c903a2ba4bc134db1a16a29c550c1d79e81a042857ab192
+ checksum: 952ad3cc426972ba96423fb0fa98b46933f382fdcf878c8baf80af2c3036862ef0cd974ab341cf5090d320322db2d24fe4fa7f8dbdfef5849b0391f132cea18b
languageName: node
linkType: hard
@@ -11523,7 +11461,7 @@ __metadata:
react-native: 0.88.1
react-native-builder-bob: ^0.23.2
react-native-nitro-modules: 0.37.2
- react-native-webgpu: 0.23.0
+ react-native-webgpu: 0.26.0
semantic-release: ^25.0.9
semantic-release-yarn: ^3.0.2
typescript: ^5.2.2
@@ -11534,37 +11472,9 @@ __metadata:
languageName: unknown
linkType: soft
-"react-native-webgpu-dawn@npm:158.0.0":
- version: 158.0.0
- resolution: "react-native-webgpu-dawn@npm:158.0.0"
- checksum: 91affe20aa3a65cb2f33861c501cf56bb2d59b148fa159f4dc8bbdf232e995db42ae499ea86f131cb107106b55eaa747ec42f36565db7e43a823a9d4f64ec9dd
- languageName: node
- linkType: hard
-
-"react-native-webgpu@npm:0.23.0":
- version: 0.23.0
- resolution: "react-native-webgpu@npm:0.23.0"
- dependencies:
- react-native-webgpu-dawn: 158.0.0
- peerDependencies:
- react: "*"
- react-native: ">=0.81.0"
- react-native-reanimated: ">=4.2.1"
- react-native-worklets: ">=0.7.2"
- peerDependenciesMeta:
- react-native-reanimated:
- optional: true
- react-native-worklets:
- optional: true
- checksum: 25615918489dc45f7c189696b153749f3d1a234eea493f5b086837ca899cc7294938a03c0ac77cf798a15458496213af6a4a3cb3bd6fa5c89e54bf5ce1e6dcc9
- languageName: node
- linkType: hard
-
-"react-native-webgpu@patch:react-native-webgpu@npm%3A0.23.0#./.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch::locator=react-native-webcodecs%40workspace%3A.":
- version: 0.23.0
- resolution: "react-native-webgpu@patch:react-native-webgpu@npm%3A0.23.0#./.yarn/patches/react-native-webgpu-npm-0.23.0-50f3d74c47.patch::version=0.23.0&hash=5f2fe8&locator=react-native-webcodecs%40workspace%3A."
- dependencies:
- react-native-webgpu-dawn: 158.0.0
+"react-native-webgpu@npm:0.26.0":
+ version: 0.26.0
+ resolution: "react-native-webgpu@npm:0.26.0"
peerDependencies:
react: "*"
react-native: ">=0.81.0"
@@ -11575,7 +11485,7 @@ __metadata:
optional: true
react-native-worklets:
optional: true
- checksum: 7c64ce764886731a9cc54bd5e94f53a5ac4ff371d5c47633189b924126e2d7f26bf03ce6fd1627ad81fd11c240ff1b6f80998baef366a3f989e4b2edd504ac85
+ checksum: bf98c0f8d0554093e34ff0cde7cece1107eaf4d4ee4baa8a2bd26482c033d8523ea34bb728db830575315fbd18c09eba8a9871120ea3ae3da6703b8d0652d7a8
languageName: node
linkType: hard
From 165ad7a0b18d21c96837846954d9f5f05abf97bd Mon Sep 17 00:00:00 2001
From: Andrei Alecu
Date: Thu, 26 Nov 2026 10:42:21 +0200
Subject: [PATCH 2/2] refactor: simplify the color space mapping and drop patch
leftovers
- One ColorSpaceInit type, shared by video.ts and the mapping.
- The tables are Maps: no own-property guard, and the result is built in
one expression.
- A type-level check in the unit test fails tsc if NativeBufferColorSpace
drifts from react-native-webgpu's.
- .gitignore and the CI cache key no longer name .yarn/patches.
- Shorter getter docs and README version list; the explanation stays in the
guide.
---
.github/actions/setup/action.yml | 2 +-
.gitignore | 1 -
README.md | 9 ++-
src/__tests__/unit/colorSpace.test.ts | 10 ++++
src/lib/colorSpace.ts | 82 ++++++++++++---------------
src/lib/video.ts | 30 +++-------
6 files changed, 60 insertions(+), 74 deletions(-)
diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml
index 9bd2464..7546ee1 100644
--- a/.github/actions/setup/action.yml
+++ b/.github/actions/setup/action.yml
@@ -29,7 +29,7 @@ runs:
.yarn/install-state.gz
node_modules
example/node_modules
- key: yarn-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.nvmrc', '.yarnrc.yml', '.yarn/releases/**', '.yarn/patches/**', 'yarn.lock', 'package.json', 'example/package.json') }}
+ key: yarn-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.nvmrc', '.yarnrc.yml', '.yarn/releases/**', 'yarn.lock', 'package.json', 'example/package.json') }}
- name: Install dependencies
run: yarn install --immutable
diff --git a/.gitignore b/.gitignore
index 248ea70..056eec1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,5 @@
# Yarn
.yarn/*
-!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
diff --git a/README.md b/README.md
index dd32708..4a0df2f 100644
--- a/README.md
+++ b/README.md
@@ -102,11 +102,10 @@ To draw frames or read decoded ones on the GPU, add
### Versions
-React Native WebGPU 0.26.0 or later: reading decoded video into WebGPU with
-the right colors on Android takes the `colorSpace` option of
-`createNativeBufferFromPointer` ([Read video into WebGPU](docs/guides.md#read-video-into-webgpu)).
-React Native Skia 3.11.1 or later, if Skia draws into pool surfaces on the
-Android emulator.
+- React Native WebGPU 0.26.0 or later, for decoded video in the right colors
+ on Android ([Read video into WebGPU](docs/guides.md#read-video-into-webgpu)).
+- React Native Skia 3.11.1 or later, for Skia drawing into pool surfaces on
+ the Android emulator.
## Codecs
diff --git a/src/__tests__/unit/colorSpace.test.ts b/src/__tests__/unit/colorSpace.test.ts
index dbe830e..44f6965 100644
--- a/src/__tests__/unit/colorSpace.test.ts
+++ b/src/__tests__/unit/colorSpace.test.ts
@@ -1,3 +1,6 @@
+import type { NativeBufferColorSpace as WebGPUNativeBufferColorSpace } from "react-native-webgpu";
+
+import type { NativeBufferColorSpace } from "../../lib/colorSpace";
import { toNativeBufferColorSpace } from "../../lib/colorSpace";
// What react-native-webgpu's createNativeBufferFromPointer takes, from the
@@ -73,3 +76,10 @@ describe("toNativeBufferColorSpace", () => {
expect(toNativeBufferColorSpace({})).toEqual({});
});
});
+
+// The type is react-native-webgpu's, redeclared so that it stays a dev
+// dependency: tsc fails here if the two drift apart.
+type Same = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
+const sameType: Same =
+ true;
+void sameType;
diff --git a/src/lib/colorSpace.ts b/src/lib/colorSpace.ts
index e770c70..1660212 100644
--- a/src/lib/colorSpace.ts
+++ b/src/lib/colorSpace.ts
@@ -17,65 +17,55 @@ export interface NativeBufferColorSpace {
| "bt-1886";
}
-/** @internal The members of a WebCodecs color space it reads. */
-export interface WebCodecsColorSpace {
+/** @internal A WebCodecs color space, as VideoColorSpaceInit. */
+export type ColorSpaceInit = {
primaries?: string | null;
transfer?: string | null;
matrix?: string | null;
fullRange?: boolean | null;
-}
-
-const PRIMARIES: Record = {
- bt709: "srgb",
- bt470bg: "rec601",
- smpte170m: "rec601",
- bt2020: "rec2020",
- smpte432: "display-p3",
};
-const TRANSFER: Record = {
- bt709: "smpte-170m",
- smpte170m: "smpte-170m",
- "iec61966-2-1": "srgb",
- linear: "identity",
- pq: "pq",
- hlg: "hlg",
-};
+const PRIMARIES = new Map([
+ ["bt709", "srgb"],
+ ["bt470bg", "rec601"],
+ ["smpte170m", "rec601"],
+ ["bt2020", "rec2020"],
+ ["smpte432", "display-p3"],
+]);
-const MATRIX: Record = {
- bt709: "rec709",
- bt470bg: "rec601",
- smpte170m: "rec601",
- "bt2020-ncl": "rec2020",
-};
+const TRANSFER = new Map([
+ ["bt709", "smpte-170m"],
+ ["smpte170m", "smpte-170m"],
+ ["iec61966-2-1", "srgb"],
+ ["linear", "identity"],
+ ["pq", "pq"],
+ ["hlg", "hlg"],
+]);
-const lookup = (table: Record, name?: string | null) =>
- name != null && Object.prototype.hasOwnProperty.call(table, name)
- ? table[name]
- : undefined;
+const MATRIX = new Map([
+ ["bt709", "rec709"],
+ ["bt470bg", "rec601"],
+ ["smpte170m", "rec601"],
+ ["bt2020-ncl", "rec2020"],
+]);
/**
* @internal A WebCodecs color space in Dawn's names. Members it has no name
* for are left out, so the buffer's own stay.
*/
export const toNativeBufferColorSpace = (
- colorSpace: WebCodecsColorSpace,
+ colorSpace: ColorSpaceInit,
): NativeBufferColorSpace => {
- const result: NativeBufferColorSpace = {};
- const primaries = lookup(PRIMARIES, colorSpace.primaries);
- if (primaries) {
- result.primaries = primaries;
- }
- const transfer = lookup(TRANSFER, colorSpace.transfer);
- if (transfer) {
- result.transfer = transfer;
- }
- const matrix = lookup(MATRIX, colorSpace.matrix);
- if (matrix) {
- result.matrix = matrix;
- }
- if (typeof colorSpace.fullRange === "boolean") {
- result.range = colorSpace.fullRange ? "full" : "narrow";
- }
- return result;
+ const primaries = PRIMARIES.get(colorSpace.primaries ?? "");
+ const transfer = TRANSFER.get(colorSpace.transfer ?? "");
+ const matrix = MATRIX.get(colorSpace.matrix ?? "");
+ const { fullRange } = colorSpace;
+ return {
+ ...(primaries && { primaries }),
+ ...(transfer && { transfer }),
+ ...(matrix && { matrix }),
+ ...(typeof fullRange === "boolean" && {
+ range: fullRange ? "full" : "narrow",
+ }),
+ };
};
diff --git a/src/lib/video.ts b/src/lib/video.ts
index e2f411b..0ba3540 100644
--- a/src/lib/video.ts
+++ b/src/lib/video.ts
@@ -8,8 +8,11 @@ import type {
} from "../specs/WebCodecs.nitro";
import { avcCodedSize } from "./avc";
-import type { NativeBufferColorSpace } from "./colorSpace";
-import { toNativeBufferColorSpace } from "./colorSpace";
+import {
+ type ColorSpaceInit,
+ type NativeBufferColorSpace,
+ toNativeBufferColorSpace,
+} from "./colorSpace";
import {
cloneConfig,
CodecBase,
@@ -33,13 +36,6 @@ const BT709 = {
fullRange: false,
} as const;
-type ColorSpaceInit = {
- primaries?: string | null;
- transfer?: string | null;
- matrix?: string | null;
- fullRange?: boolean | null;
-};
-
const sameColorSpace = (a: ColorSpaceInit, b: ColorSpaceInit) =>
a.primaries === b.primaries &&
a.transfer === b.transfer &&
@@ -304,18 +300,10 @@ export class VideoFrame {
}
/**
- * Not in WebCodecs: `colorSpace` as react-native-webgpu's
- * createNativeBufferFromPointer takes it, next to `pixelBuffer`:
- *
- * ```ts
- * RNWebGPU.createNativeBufferFromPointer(frame.pixelBuffer, {
- * colorSpace: frame.nativeBufferColorSpace,
- * });
- * ```
- *
- * A decoder's buffers on Android carry no color information of their own:
- * without it, BT.709 video is read with the BT.601 matrix. On Apple
- * platforms the pixel buffer has its own, and this repeats them.
+ * Not in WebCodecs: `colorSpace` in the names react-native-webgpu's
+ * createNativeBufferFromPointer takes, to pass with `pixelBuffer`
+ * (`{ colorSpace: frame.nativeBufferColorSpace }`). Android needs it: see
+ * docs/guides.md, "Read video into WebGPU".
*/
get nativeBufferColorSpace(): NativeBufferColorSpace {
return toNativeBufferColorSpace(this.colorSpace);