Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/docs/content/api/gpu-device-extensions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ An external texture built over a texture of a shared memory does not open an acc
Its descriptor is the same as with a frame as the source (`colorSpace`, `rotation`, `mirrored`, plus `sourceColorSpace`, `cropOrigin` and `cropSize`, see below).
When the producer drops the buffer, destroy the external texture, the texture and the memory, in that order.

On Android, a buffer from another library (wrapped with `createNativeBufferFromPointer`) carries no color metadata: pass `sourceColorSpace` when you know it, or the conversion assumes what the platform reports for the buffer, BT.601 limited range by default.
On Android, a buffer from another library (wrapped with `createNativeBufferFromPointer`) carries no color metadata, so the conversion assumes what the platform reports for the buffer, BT.601 limited range by default. When you know how the buffer is encoded, pass its color space as `colorSpace` to `createNativeBufferFromPointer`, or as `sourceColorSpace` here.

## dawnToggles

Expand Down
13 changes: 12 additions & 1 deletion apps/docs/content/api/native-buffer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,23 @@ The width and height are integers from 1 to 16384.
## createNativeBufferFromPointer

```ts
createNativeBufferFromPointer(pointer: bigint): NativeBuffer
createNativeBufferFromPointer(pointer: bigint, options?: CreateNativeBufferFromPointerOptions): NativeBuffer
```

Wraps a buffer produced by another library (a `CVPixelBufferRef` on Apple platforms, an `AHardwareBuffer*` on Android, for instance VisionCamera's `frame.getNativeBuffer().pointer`) into a `NativeBuffer` that holds a reference of its own until released.
It runs on the JS thread; on a worklet runtime, such as a frame processor, call `RNWebGPU.createNativeBufferFromPointer(pointer)` instead (see [Vision Camera](/docs/integrations/vision-camera)).

A YUV buffer may carry no color information of its own, as an `AHardwareBuffer` from a MediaCodec `ImageReader`, and is then read with the platform's guess (BT.601 limited range on Android when the driver has none).
When you know how it is encoded, pass `colorSpace`, a [`NativeBufferColorSpace`](/api/gpu-device-extensions#gpuexternaltexture-extensions) like `sourceColorSpace` in `importExternalTexture()`; the members it sets replace what the buffer says:

```ts
const frame = createNativeBufferFromPointer(pointer, {
colorSpace: { matrix: "rec709", range: "narrow" },
});
```

<auto-type-table path="../../../../packages/webgpu/src/types.ts" name="CreateNativeBufferFromPointerOptions" />

`createTestVideoFrame(width, height)` allocates a frame filled with a test pattern, for tests and examples that need a frame without a camera or a video.

## NativeBufferView
Expand Down
25 changes: 23 additions & 2 deletions packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "ArrayBuffer.h"
#include "Canvas.h"
#include "CreateNativeBufferFromPointerOptions.h"
#include "GPU.h"
#include "GPUCanvasContext.h"
#include "GPUDevice.h"
Expand Down Expand Up @@ -320,9 +321,29 @@ class RNWebGPU : public NativeObject<RNWebGPU> {

// 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<VideoFrame> createNativeBufferFromPointer(void *pointer) {
// CFRetains / acquires so the caller can release immediately. The members
// of `options.colorSpace` replace what the buffer says about its own.
std::shared_ptr<VideoFrame> createNativeBufferFromPointer(
void *pointer,
std::optional<std::shared_ptr<CreateNativeBufferFromPointerOptions>>
options) {
auto handle = _platformContext->wrapNativeBuffer(pointer);
if (options && (*options)->colorSpace) {
const auto &given = *(*options)->colorSpace;
auto &reported = handle.colorSpace;
if (given.matrix) {
reported.matrix = given.matrix;
}
if (given.range) {
reported.range = given.range;
}
if (given.primaries) {
reported.primaries = given.primaries;
}
if (given.transfer) {
reported.transfer = given.transfer;
}
}
return std::make_shared<VideoFrame>(std::move(handle));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <memory>
#include <optional>
#include <stdexcept>

#include "GPUExternalTextureDescriptor.h"
#include "JSIConverter.h"
#include "PlatformContext.h"

namespace jsi = facebook::jsi;

namespace rnwgpu {

// The options of createNativeBufferFromPointer(). `colorSpace` (a
// NativeBufferColorSpace, as importExternalTexture's sourceColorSpace) says
// how to read a YUV buffer that carries no color information of its own, as
// an AHardwareBuffer from a MediaCodec ImageReader. The members it sets
// replace what the buffer says.
struct CreateNativeBufferFromPointerOptions {
std::optional<VideoFrameColorSpace> colorSpace;
};

template <>
struct JSIConverter<std::shared_ptr<CreateNativeBufferFromPointerOptions>> {
static std::shared_ptr<CreateNativeBufferFromPointerOptions>
fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
auto result = std::make_shared<CreateNativeBufferFromPointerOptions>();
if (!outOfBounds && arg.isObject()) {
auto prop = arg.getObject(runtime).getProperty(runtime, "colorSpace");
if (prop.isObject()) {
result->colorSpace = external_texture_color_space::parse(
runtime, prop.getObject(runtime), "colorSpace");
}
}
return result;
}
static jsi::Value
toJSI(jsi::Runtime &runtime,
std::shared_ptr<CreateNativeBufferFromPointerOptions> arg) {
throw std::runtime_error(
"Invalid CreateNativeBufferFromPointerOptions::toJSI()");
}
};

} // namespace rnwgpu
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ namespace external_texture_color_space {
// The JS names of the members of a source color space, the Dawn enums
// spelled the way the frame producers report them (see
// NativeBufferColorSpace in src/types.ts).
inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &name) {
inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &owner,
const std::string &name) {
if (name == "identity") {
return wgpu::ColorSpaceYCbCrMatrixDawn::Identity;
}
Expand All @@ -64,12 +65,13 @@ inline wgpu::ColorSpaceYCbCrMatrixDawn parseMatrix(const std::string &name) {
if (name == "rec2020") {
return wgpu::ColorSpaceYCbCrMatrixDawn::Rec2020;
}
throw std::runtime_error("sourceColorSpace.matrix: unknown value '" + name +
throw std::runtime_error(owner + ".matrix: unknown value '" + name +
"' (expected 'identity', 'rec601', 'rec709' or "
"'rec2020')");
}

inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &name) {
inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &owner,
const std::string &name) {
if (name == "identity") {
return wgpu::ColorSpaceYCbCrRangeDawn::Identity;
}
Expand All @@ -79,11 +81,12 @@ inline wgpu::ColorSpaceYCbCrRangeDawn parseRange(const std::string &name) {
if (name == "full") {
return wgpu::ColorSpaceYCbCrRangeDawn::Full;
}
throw std::runtime_error("sourceColorSpace.range: unknown value '" + name +
throw std::runtime_error(owner + ".range: unknown value '" + name +
"' (expected 'identity', 'narrow' or 'full')");
}

inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &name) {
inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &owner,
const std::string &name) {
if (name == "srgb") {
return wgpu::ColorSpacePrimariesDawn::SRGB;
}
Expand All @@ -99,13 +102,13 @@ inline wgpu::ColorSpacePrimariesDawn parsePrimaries(const std::string &name) {
if (name == "display-p3") {
return wgpu::ColorSpacePrimariesDawn::DisplayP3;
}
throw std::runtime_error("sourceColorSpace.primaries: unknown value '" +
name +
throw std::runtime_error(owner + ".primaries: unknown value '" + name +
"' (expected 'srgb', 'rec709', 'rec601', "
"'rec2020' or 'display-p3')");
}

inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &name) {
inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &owner,
const std::string &name) {
if (name == "identity") {
return wgpu::ColorSpaceTransferDawn::Identity;
}
Expand All @@ -127,13 +130,16 @@ inline wgpu::ColorSpaceTransferDawn parseTransfer(const std::string &name) {
if (name == "bt-1886") {
return wgpu::ColorSpaceTransferDawn::BT_1886;
}
throw std::runtime_error("sourceColorSpace.transfer: unknown value '" + name +
throw std::runtime_error(owner + ".transfer: unknown value '" + name +
"' (expected 'identity', 'srgb', 'display-p3', "
"'smpte-170m', 'hlg', 'pq' or 'bt-1886')");
}

// `owner` names the member being parsed in the error messages
// ("sourceColorSpace", "colorSpace").
inline VideoFrameColorSpace parse(jsi::Runtime &runtime,
const jsi::Object &value) {
const jsi::Object &value,
const std::string &owner) {
VideoFrameColorSpace colorSpace;
auto member = [&](const char *name) -> std::optional<std::string> {
if (!value.hasProperty(runtime, name)) {
Expand All @@ -146,16 +152,16 @@ inline VideoFrameColorSpace parse(jsi::Runtime &runtime,
return prop.asString(runtime).utf8(runtime);
};
if (auto matrix = member("matrix")) {
colorSpace.matrix = parseMatrix(*matrix);
colorSpace.matrix = parseMatrix(owner, *matrix);
}
if (auto range = member("range")) {
colorSpace.range = parseRange(*range);
colorSpace.range = parseRange(owner, *range);
}
if (auto primaries = member("primaries")) {
colorSpace.primaries = parsePrimaries(*primaries);
colorSpace.primaries = parsePrimaries(owner, *primaries);
}
if (auto transfer = member("transfer")) {
colorSpace.transfer = parseTransfer(*transfer);
colorSpace.transfer = parseTransfer(owner, *transfer);
}
return colorSpace;
}
Expand Down Expand Up @@ -204,7 +210,7 @@ struct JSIConverter<std::shared_ptr<rnwgpu::GPUExternalTextureDescriptor>> {
auto prop = value.getProperty(runtime, "sourceColorSpace");
if (prop.isObject()) {
result->sourceColorSpace = external_texture_color_space::parse(
runtime, prop.getObject(runtime));
runtime, prop.getObject(runtime), "sourceColorSpace");
}
}
if (value.hasProperty(runtime, "rotation")) {
Expand Down
Loading
Loading