From dd9c3789c5c2c01a1c1fc8a1f07079d6d3e8e12a Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Tue, 24 Nov 2026 00:00:58 +0200 Subject: [PATCH 1/4] =?UTF-8?q?fix(=F0=9F=8D=8F):=20light=20EDR=20for=20to?= =?UTF-8?q?neMapping=20"extended"=20on=20iOS/macOS/visionOS=2026?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since the 26 SDKs, wantsExtendedDynamicRangeContent is deprecated and on its own no longer shows values above 1.0 brighter than SDR white. The layer now also asks for it with preferredDynamicRange and declares a contentsHeadroom; "standard" resets both. tvOS is unchanged. --- packages/webgpu/apple/MetalLayerColorSpace.mm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/webgpu/apple/MetalLayerColorSpace.mm b/packages/webgpu/apple/MetalLayerColorSpace.mm index 0e8e7f373a..783fec992c 100644 --- a/packages/webgpu/apple/MetalLayerColorSpace.mm +++ b/packages/webgpu/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 From e957da6c2355288e6d558abc0a49cff1e2932987 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Tue, 24 Nov 2026 00:32:05 +0200 Subject: [PATCH 2/4] =?UTF-8?q?test(=F0=9F=8D=8F):=20e2e=20spec=20for=20th?= =?UTF-8?q?e=20Canvas=20toneMapping=20prop=20on=20the=2026=20SDKs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The harness mounts two small canvases, toneMapping "extended" and "standard", and the spec reads their CAMetalLayer back: extended asks for CADynamicRangeHigh with a contentsHeadroom above 1 and wantsExtendedDynamicRangeContent, standard for CADynamicRangeStandard with no headroom; both are tagged extended sRGB. Skipped before iOS, macOS and visionOS 26 and where the canvas has no Core Animation layer. Without the preferredDynamicRange fix the extended canvas reports "standard" and the spec fails. The layer is read through a test-only RNWebGPU.describeCanvasLayer, left out of the TypeScript declarations. --- apps/example/src/Tests.tsx | 22 +++++ packages/webgpu/apple/ApplePlatformContext.h | 2 + packages/webgpu/apple/ApplePlatformContext.mm | 36 ++++++++ packages/webgpu/cpp/rnwgpu/PlatformContext.h | 7 ++ packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h | 16 ++++ .../src/__tests__/CanvasToneMapping.spec.ts | 87 +++++++++++++++++++ packages/webgpu/src/__tests__/setup.ts | 6 ++ 7 files changed, 176 insertions(+) create mode 100644 packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts diff --git a/apps/example/src/Tests.tsx b/apps/example/src/Tests.tsx index c1ffbf3fa2..348bb47837 100644 --- a/apps/example/src/Tests.tsx +++ b/apps/example/src/Tests.tsx @@ -8,6 +8,7 @@ import { findNodeHandle, } from "react-native"; import { + Canvas, GPUOffscreenCanvas, importDevice, // The flags as the package exports them, not the globals of the same name @@ -18,6 +19,7 @@ import { GPUShaderStage as exportedShaderStage, GPUTextureUsage as exportedTextureUsage, } from "react-native-webgpu"; +import type { CanvasRef } from "react-native-webgpu"; // Imported after react-native-webgpu on purpose: webgpu installs natively on // import and settles on a Dawn instance of its own while Skia's library is not // loaded yet. On Android, Skia's device then lives on a second instance, and @@ -50,6 +52,10 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { // A fixed native view the drawElementImageToTexture spec snapshots: four // 32x32pt solid quadrants (red, lime, blue, white), 64x64pt in total. const probeRef = useRef>(null); + // Two mounted canvases, one per toneMapping mode, whose native layers the + // CanvasToneMapping spec inspects. + const extendedCanvasRef = useRef(null); + const standardCanvasRef = useRef(null); useEffect(() => { let cancelled = false; (async () => { @@ -136,6 +142,10 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { probe: findNodeHandle(probeRef.current), probeSize: PROBE_SIZE, pixelRatio: PixelRatio.get(), + toneMappingCanvases: { + extended: extendedCanvasRef.current?.getContextId() ?? null, + standard: standardCanvasRef.current?.getContextId() ?? null, + }, }, workletDeviceStress, workletSerialization, @@ -219,6 +229,18 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { }} /> + + + + #import #import +#import #import #import @@ -559,6 +560,41 @@ ImageData renderViewIntoPixels(const ResolvedSnapshot &snapshot, return writeAppleTestVideoFile(); } +// The settings applyCAMetalLayerColorSpace makes, read back for the e2e spec. +// The getters of a model layer are safe off the main thread. +std::string +ApplePlatformContext::describeNativeSurfaceLayer(void *nativeSurface) { + if (nativeSurface == nullptr) { + return ""; + } + CAMetalLayer *layer = (__bridge CAMetalLayer *)nativeSurface; + NSMutableDictionary *info = [NSMutableDictionary dictionary]; + if (CGColorSpaceRef colorSpace = layer.colorspace) { + if (CFStringRef name = CGColorSpaceGetName(colorSpace)) { + info[@"colorspace"] = (__bridge NSString *)name; + } + } +#if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION + if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { + info[@"wantsExtendedDynamicRangeContent"] = + @(layer.wantsExtendedDynamicRangeContent); + } + if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) { + CADynamicRange range = layer.preferredDynamicRange; + info[@"preferredDynamicRange"] = + [range isEqualToString:CADynamicRangeHigh] ? @"high" + : [range isEqualToString:CADynamicRangeStandard] ? @"standard" + : range; + info[@"contentsHeadroom"] = @(layer.contentsHeadroom); + } +#endif + NSData *json = [NSJSONSerialization dataWithJSONObject:info + options:0 + error:nil]; + return json ? std::string(static_cast(json.bytes), json.length) + : ""; +} + VideoFrameHandle ApplePlatformContext::wrapNativeBuffer(void *pointer) { return wrapCVPixelBuffer(static_cast(pointer)); } diff --git a/packages/webgpu/cpp/rnwgpu/PlatformContext.h b/packages/webgpu/cpp/rnwgpu/PlatformContext.h index f9f96d652a..f20ead9e9a 100644 --- a/packages/webgpu/cpp/rnwgpu/PlatformContext.h +++ b/packages/webgpu/cpp/rnwgpu/PlatformContext.h @@ -228,6 +228,13 @@ class PlatformContext { virtual VideoFrameHandle createTestVideoFrame(uint32_t width, uint32_t height, VideoPixelFormat format) = 0; + // Test only (the e2e spec of the Canvas toneMapping prop): the dynamic range + // and colorspace settings of the layer behind a canvas's native surface, as + // a JSON object, or "" where the platform has no such layer. + virtual std::string describeNativeSurfaceLayer(void * /*nativeSurface*/) { + return ""; + } + // Allocate an empty, GPU-renderable buffer that a NativeBufferView can // display: an AHardwareBuffer (Android) or an IOSurface (Apple platforms), // with premultiplied pixels in `format`, tagged with `displayP3` or sRGB diff --git a/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h b/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h index 10f9dfe04d..693b466596 100644 --- a/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h +++ b/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h @@ -525,6 +525,20 @@ class RNWebGPU : public NativeObject { return rnwgpu::SurfaceRegistry::getInstance().allocateContextId(); } + // Test only, left out of the TypeScript declarations: the dynamic range + // and colorspace settings of the layer of a mounted canvas, as a JSON + // string (PlatformContext::describeNativeSurfaceLayer); "" when the canvas + // has no native surface or the platform no such layer. + std::string describeCanvasLayer(int contextId) { + auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); + auto info = registry.getSurfaceInfo(contextId); + if (info == nullptr) { + return ""; + } + return _platformContext->describeNativeSurfaceLayer( + info->getNativeInfo().nativeSurface); + } + std::shared_ptr getNativeSurface(int contextId) { auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); auto info = registry.getSurfaceInfo(contextId); @@ -566,6 +580,8 @@ class RNWebGPU : public NativeObject { &RNWebGPU::allocateContextId); installMethod(runtime, prototype, "getNativeSurface", &RNWebGPU::getNativeSurface); + installMethod(runtime, prototype, "describeCanvasLayer", + &RNWebGPU::describeCanvasLayer); installMethod(runtime, prototype, "destroyContext", &RNWebGPU::destroyContext); installMethod(runtime, prototype, "MakeWebGPUCanvasContext", diff --git a/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts b/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts new file mode 100644 index 0000000000..b636dbba19 --- /dev/null +++ b/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts @@ -0,0 +1,87 @@ +import { client } from "./setup"; + +// What RNWebGPU.describeCanvasLayer reports for the CAMetalLayer of a mounted +// canvas. preferredDynamicRange and contentsHeadroom exist on iOS, macOS and +// visionOS 26 and later only. +type LayerInfo = { + colorspace?: string; + wantsExtendedDynamicRangeContent?: boolean; + preferredDynamicRange?: string; + contentsHeadroom?: number; +}; + +type EvalResult = + | { kind: "skip"; reason: string } + | { kind: "fail"; reason: string } + | { kind: "ok"; extended: LayerInfo; standard: LayerInfo }; + +describe("Canvas toneMapping", () => { + // The harness (apps/example/src/Tests.tsx) mounts two canvases, one with + // toneMapping "extended" and one with "standard". Since the 26 SDKs the + // layer only shows values above 1.0 brighter than SDR white when it asks + // for a high dynamic range and declares a headroom: + // wantsExtendedDynamicRangeContent alone no longer does it. + it("asks the layer for EDR with the extended mode, and only then", async () => { + const result = await client.eval, EvalResult>( + ({ views }) => { + const rnwgpu = RNWebGPU as unknown as { + describeCanvasLayer?: (contextId: number) => string; + }; + if (typeof rnwgpu.describeCanvasLayer !== "function") { + return { + kind: "skip", + reason: "RNWebGPU.describeCanvasLayer is unavailable", + }; + } + const ids = views?.toneMappingCanvases; + if (!ids || ids.extended === null || ids.standard === null) { + return { + kind: "skip", + reason: "the harness mounts no toneMapping canvases", + }; + } + const describe = (contextId: number): LayerInfo | null => { + const json = rnwgpu.describeCanvasLayer!(contextId); + return json ? (JSON.parse(json) as LayerInfo) : null; + }; + const extended = describe(ids.extended); + const standard = describe(ids.standard); + if (extended === null || standard === null) { + return { + kind: "skip", + reason: "the canvases have no Core Animation layer here", + }; + } + if (extended.preferredDynamicRange === undefined) { + return { + kind: "skip", + reason: "preferredDynamicRange needs iOS/macOS/visionOS 26", + }; + } + return { kind: "ok", extended, standard }; + }, + ); + + if (result.kind === "skip") { + console.log(`CanvasToneMapping: skipping (${result.reason})`); + return; + } + if (result.kind === "fail") { + throw new Error(`CanvasToneMapping: ${result.reason}`); + } + const { extended, standard } = result; + + expect(extended.preferredDynamicRange).toBe("high"); + expect(extended.contentsHeadroom).toBeGreaterThan(1); + expect(extended.wantsExtendedDynamicRangeContent).toBe(true); + + expect(standard.preferredDynamicRange).toBe("standard"); + expect(standard.contentsHeadroom).toBe(0); + expect(standard.wantsExtendedDynamicRangeContent).toBe(false); + + // Both default to colorSpace "srgb", tagged with the gamma-encoded + // extended variant so that float formats keep values above 1.0. + expect(extended.colorspace).toBe("kCGColorSpaceExtendedSRGB"); + expect(standard.colorspace).toBe("kCGColorSpaceExtendedSRGB"); + }); +}); diff --git a/packages/webgpu/src/__tests__/setup.ts b/packages/webgpu/src/__tests__/setup.ts index b8e20b6d0a..c6c7de4ba4 100644 --- a/packages/webgpu/src/__tests__/setup.ts +++ b/packages/webgpu/src/__tests__/setup.ts @@ -108,6 +108,12 @@ interface GPUTestingContext { probe: number | null; probeSize: number; pixelRatio: number; + // The context ids of two mounted canvases, toneMapping "extended" and + // "standard", for the CanvasToneMapping spec. + toneMappingCanvases?: { + extended: number | null; + standard: number | null; + }; }; // Native app harness only: Skia's Graphite device wrapped with // importDevice(), or null when Skia has no GPU. See ImportDevice.spec.ts From 540aa8076ff47bcc67f2fd39d5db053e641763c3 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Wed, 25 Nov 2026 12:31:52 +0200 Subject: [PATCH 3/4] =?UTF-8?q?fix(=F0=9F=8D=8F):=20compile=20the=20EDR=20?= =?UTF-8?q?settings=20only=20with=20the=2026=20SDKs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit preferredDynamicRange, contentsHeadroom and CADynamicRange* exist only in the iOS/macOS/visionOS 26 SDKs, and @available does not keep older Xcodes from failing to compile them. RNWGPU_HAS_CA_DYNAMIC_RANGE guards them at compile time. The visionOS SDK pins __IPHONE_OS_VERSION_MAX_ALLOWED at 17.0, so visionOS checks __VISION_OS_VERSION_MAX_ALLOWED. Also: the layer read-back for the spec now sits next to the setter and shares its guards, the headroom is a named constant, describeCanvasLayer is declared with the other test-only RNWebGPU methods, and the spec uses itSkipsOnWeb and drops its unreachable branches. --- apps/example/src/Tests.tsx | 3 +- packages/webgpu/apple/ApplePlatformContext.mm | 31 +---- packages/webgpu/apple/MetalLayerColorSpace.h | 25 ++++ packages/webgpu/apple/MetalLayerColorSpace.mm | 39 +++++- packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h | 5 +- .../src/__tests__/CanvasToneMapping.spec.ts | 118 ++++++++---------- packages/webgpu/src/__tests__/setup.ts | 5 +- packages/webgpu/src/index.tsx | 3 + 8 files changed, 122 insertions(+), 107 deletions(-) diff --git a/apps/example/src/Tests.tsx b/apps/example/src/Tests.tsx index ee5e1976f9..325c457bb0 100644 --- a/apps/example/src/Tests.tsx +++ b/apps/example/src/Tests.tsx @@ -52,8 +52,7 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { // A fixed native view the drawElementImageToTexture spec snapshots: four // 32x32pt solid quadrants (red, lime, blue, white), 64x64pt in total. const probeRef = useRef>(null); - // Two mounted canvases, one per toneMapping mode, whose native layers the - // CanvasToneMapping spec inspects. + // One canvas per toneMapping mode, for the CanvasToneMapping spec. const extendedCanvasRef = useRef(null); const standardCanvasRef = useRef(null); useEffect(() => { diff --git a/packages/webgpu/apple/ApplePlatformContext.mm b/packages/webgpu/apple/ApplePlatformContext.mm index e97eade2c3..4ec461891d 100644 --- a/packages/webgpu/apple/ApplePlatformContext.mm +++ b/packages/webgpu/apple/ApplePlatformContext.mm @@ -5,11 +5,11 @@ #import #import #import -#import #import #import #include "AppleVideoPlayer.h" +#include "MetalLayerColorSpace.h" #import "RNWGUIKit.h" #include "RNWebGPUManager.h" @@ -560,39 +560,12 @@ ImageData renderViewIntoPixels(const ResolvedSnapshot &snapshot, return writeAppleTestVideoFile(); } -// The settings applyCAMetalLayerColorSpace makes, read back for the e2e spec. -// The getters of a model layer are safe off the main thread. std::string ApplePlatformContext::describeNativeSurfaceLayer(void *nativeSurface) { if (nativeSurface == nullptr) { return ""; } - CAMetalLayer *layer = (__bridge CAMetalLayer *)nativeSurface; - NSMutableDictionary *info = [NSMutableDictionary dictionary]; - if (CGColorSpaceRef colorSpace = layer.colorspace) { - if (CFStringRef name = CGColorSpaceGetName(colorSpace)) { - info[@"colorspace"] = (__bridge NSString *)name; - } - } -#if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION - if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { - info[@"wantsExtendedDynamicRangeContent"] = - @(layer.wantsExtendedDynamicRangeContent); - } - if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) { - CADynamicRange range = layer.preferredDynamicRange; - info[@"preferredDynamicRange"] = - [range isEqualToString:CADynamicRangeHigh] ? @"high" - : [range isEqualToString:CADynamicRangeStandard] ? @"standard" - : range; - info[@"contentsHeadroom"] = @(layer.contentsHeadroom); - } -#endif - NSData *json = [NSJSONSerialization dataWithJSONObject:info - options:0 - error:nil]; - return json ? std::string(static_cast(json.bytes), json.length) - : ""; + return describeCAMetalLayerColorSpace((__bridge CAMetalLayer *)nativeSurface); } VideoFrameHandle ApplePlatformContext::wrapNativeBuffer(void *pointer) { diff --git a/packages/webgpu/apple/MetalLayerColorSpace.h b/packages/webgpu/apple/MetalLayerColorSpace.h index aaa7f6944d..63b715bf4f 100644 --- a/packages/webgpu/apple/MetalLayerColorSpace.h +++ b/packages/webgpu/apple/MetalLayerColorSpace.h @@ -1,9 +1,29 @@ #pragma once +#include +#include + #import +#include + #include "CanvasColorSpace.h" +// preferredDynamicRange, contentsHeadroom and CADynamicRange* are declared +// only by the 26 SDKs (Xcode 26). @available guards the runtime, not the +// compilation, so older Xcodes need this too. The visionOS SDK keeps +// __IPHONE_OS_VERSION_MAX_ALLOWED at 17.0, hence its own macro. +#if (TARGET_OS_IOS && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \ + __IPHONE_OS_VERSION_MAX_ALLOWED >= 260000) || \ + (TARGET_OS_VISION && defined(__VISION_OS_VERSION_MAX_ALLOWED) && \ + __VISION_OS_VERSION_MAX_ALLOWED >= 260000) || \ + (TARGET_OS_OSX && defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \ + __MAC_OS_X_VERSION_MAX_ALLOWED >= 260000) +#define RNWGPU_HAS_CA_DYNAMIC_RANGE 1 +#else +#define RNWGPU_HAS_CA_DYNAMIC_RANGE 0 +#endif + namespace rnwgpu { // Tags a new CAMetalLayer with the colorspace of the canvas, and opts it into @@ -14,4 +34,9 @@ namespace rnwgpu { void applyCAMetalLayerColorSpace(CAMetalLayer *layer, CanvasLayerColorSpace colorSpace); +// Test only: what applyCAMetalLayerColorSpace set on the layer, as a JSON +// object (see PlatformContext::describeNativeSurfaceLayer). The getters of a +// model layer are safe off the main thread. +std::string describeCAMetalLayerColorSpace(CAMetalLayer *layer); + } // namespace rnwgpu diff --git a/packages/webgpu/apple/MetalLayerColorSpace.mm b/packages/webgpu/apple/MetalLayerColorSpace.mm index 783fec992c..a310b30a0a 100644 --- a/packages/webgpu/apple/MetalLayerColorSpace.mm +++ b/packages/webgpu/apple/MetalLayerColorSpace.mm @@ -6,6 +6,13 @@ namespace rnwgpu { +#if RNWGPU_HAS_CA_DYNAMIC_RANGE +// The headroom an "extended" layer declares. A WebGPU canvas puts no ceiling +// on its values, so it asks for more than any display has (float16 holds far +// more) and lets the display clamp to what it can show. +static constexpr CGFloat kExtendedContentsHeadroom = 16.0; +#endif + // WebGPU canvas values are encoded with the sRGB transfer function regardless // of the texture format, and the canvas's colorSpace says which primaries they // are in: "srgb" (the default) or "display-p3". The same shader output must @@ -50,17 +57,45 @@ void applyCAMetalLayerColorSpace(CAMetalLayer *layer, if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { layer.wantsExtendedDynamicRangeContent = extendedRange; } +#endif // 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 RNWGPU_HAS_CA_DYNAMIC_RANGE if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) { layer.preferredDynamicRange = extendedRange ? CADynamicRangeHigh : CADynamicRangeStandard; - layer.contentsHeadroom = extendedRange ? 16.0 : 0.0; + layer.contentsHeadroom = extendedRange ? kExtendedContentsHeadroom : 0.0; } -#else +#endif (void)extendedRange; +} + +std::string describeCAMetalLayerColorSpace(CAMetalLayer *layer) { + NSMutableDictionary *info = [NSMutableDictionary dictionary]; + if (CGColorSpaceRef colorSpace = layer.colorspace) { + if (CFStringRef name = CGColorSpaceGetName(colorSpace)) { + info[@"colorspace"] = (__bridge NSString *)name; + } + } +#if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION + if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { + info[@"wantsExtendedDynamicRangeContent"] = + @(layer.wantsExtendedDynamicRangeContent); + } +#endif +#if RNWGPU_HAS_CA_DYNAMIC_RANGE + // CADynamicRangeHigh and CADynamicRangeStandard are "high" and "standard". + if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) { + info[@"preferredDynamicRange"] = layer.preferredDynamicRange; + info[@"contentsHeadroom"] = @(layer.contentsHeadroom); + } #endif + // Strings, numbers and booleans only: serializing cannot fail. + NSData *json = [NSJSONSerialization dataWithJSONObject:info + options:0 + error:nil]; + return std::string(static_cast(json.bytes), json.length); } } // namespace rnwgpu diff --git a/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h b/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h index 714f1b86ca..0300a925b8 100644 --- a/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h +++ b/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h @@ -526,10 +526,7 @@ class RNWebGPU : public NativeObject { return rnwgpu::SurfaceRegistry::getInstance().allocateContextId(); } - // Test only, left out of the TypeScript declarations: the dynamic range - // and colorspace settings of the layer of a mounted canvas, as a JSON - // string (PlatformContext::describeNativeSurfaceLayer); "" when the canvas - // has no native surface or the platform no such layer. + // Test only: see PlatformContext::describeNativeSurfaceLayer. std::string describeCanvasLayer(int contextId) { auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); auto info = registry.getSurfaceInfo(contextId); diff --git a/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts b/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts index b636dbba19..86a8ad6f44 100644 --- a/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts +++ b/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts @@ -1,8 +1,7 @@ -import { client } from "./setup"; +import { client, itSkipsOnWeb } from "./setup"; // What RNWebGPU.describeCanvasLayer reports for the CAMetalLayer of a mounted -// canvas. preferredDynamicRange and contentsHeadroom exist on iOS, macOS and -// visionOS 26 and later only. +// canvas. type LayerInfo = { colorspace?: string; wantsExtendedDynamicRangeContent?: boolean; @@ -12,76 +11,61 @@ type LayerInfo = { type EvalResult = | { kind: "skip"; reason: string } - | { kind: "fail"; reason: string } | { kind: "ok"; extended: LayerInfo; standard: LayerInfo }; +// The harness (apps/example/src/Tests.tsx) mounts two canvases, one with +// toneMapping "extended" and one with "standard". Since the 26 SDKs the layer +// only shows values above 1.0 brighter than SDR white when it asks for a high +// dynamic range and declares a headroom: wantsExtendedDynamicRangeContent +// alone no longer does it. preferredDynamicRange and contentsHeadroom are +// reported only on iOS, macOS and visionOS 26+, in an app built with Xcode 26+. describe("Canvas toneMapping", () => { - // The harness (apps/example/src/Tests.tsx) mounts two canvases, one with - // toneMapping "extended" and one with "standard". Since the 26 SDKs the - // layer only shows values above 1.0 brighter than SDR white when it asks - // for a high dynamic range and declares a headroom: - // wantsExtendedDynamicRangeContent alone no longer does it. - it("asks the layer for EDR with the extended mode, and only then", async () => { - const result = await client.eval, EvalResult>( - ({ views }) => { - const rnwgpu = RNWebGPU as unknown as { - describeCanvasLayer?: (contextId: number) => string; - }; - if (typeof rnwgpu.describeCanvasLayer !== "function") { - return { - kind: "skip", - reason: "RNWebGPU.describeCanvasLayer is unavailable", + itSkipsOnWeb( + "asks the layer for EDR with the extended mode, and only then", + async () => { + const result = await client.eval, EvalResult>( + ({ views }) => { + const ids = views?.toneMappingCanvases; + if (!ids || ids.extended === null || ids.standard === null) { + return { kind: "skip", reason: "no toneMapping canvases mounted" }; + } + const describe = (contextId: number) => { + const json = RNWebGPU.describeCanvasLayer(contextId); + return json ? (JSON.parse(json) as LayerInfo) : null; }; - } - const ids = views?.toneMappingCanvases; - if (!ids || ids.extended === null || ids.standard === null) { - return { - kind: "skip", - reason: "the harness mounts no toneMapping canvases", - }; - } - const describe = (contextId: number): LayerInfo | null => { - const json = rnwgpu.describeCanvasLayer!(contextId); - return json ? (JSON.parse(json) as LayerInfo) : null; - }; - const extended = describe(ids.extended); - const standard = describe(ids.standard); - if (extended === null || standard === null) { - return { - kind: "skip", - reason: "the canvases have no Core Animation layer here", - }; - } - if (extended.preferredDynamicRange === undefined) { - return { - kind: "skip", - reason: "preferredDynamicRange needs iOS/macOS/visionOS 26", - }; - } - return { kind: "ok", extended, standard }; - }, - ); + const extended = describe(ids.extended); + const standard = describe(ids.standard); + if (!extended || !standard) { + return { kind: "skip", reason: "no Core Animation layer here" }; + } + if (extended.preferredDynamicRange === undefined) { + return { + kind: "skip", + reason: "preferredDynamicRange needs the 26 SDKs and OSes", + }; + } + return { kind: "ok", extended, standard }; + }, + ); - if (result.kind === "skip") { - console.log(`CanvasToneMapping: skipping (${result.reason})`); - return; - } - if (result.kind === "fail") { - throw new Error(`CanvasToneMapping: ${result.reason}`); - } - const { extended, standard } = result; + if (result.kind === "skip") { + console.log(`CanvasToneMapping: skipping (${result.reason})`); + return; + } + const { extended, standard } = result; - expect(extended.preferredDynamicRange).toBe("high"); - expect(extended.contentsHeadroom).toBeGreaterThan(1); - expect(extended.wantsExtendedDynamicRangeContent).toBe(true); + expect(extended.preferredDynamicRange).toBe("high"); + expect(extended.contentsHeadroom).toBeGreaterThan(1); + expect(extended.wantsExtendedDynamicRangeContent).toBe(true); - expect(standard.preferredDynamicRange).toBe("standard"); - expect(standard.contentsHeadroom).toBe(0); - expect(standard.wantsExtendedDynamicRangeContent).toBe(false); + expect(standard.preferredDynamicRange).toBe("standard"); + expect(standard.contentsHeadroom).toBe(0); + expect(standard.wantsExtendedDynamicRangeContent).toBe(false); - // Both default to colorSpace "srgb", tagged with the gamma-encoded - // extended variant so that float formats keep values above 1.0. - expect(extended.colorspace).toBe("kCGColorSpaceExtendedSRGB"); - expect(standard.colorspace).toBe("kCGColorSpaceExtendedSRGB"); - }); + // Both default to colorSpace "srgb", tagged with the gamma-encoded + // extended variant so that float formats keep values above 1.0. + expect(extended.colorspace).toBe("kCGColorSpaceExtendedSRGB"); + expect(standard.colorspace).toBe("kCGColorSpaceExtendedSRGB"); + }, + ); }); diff --git a/packages/webgpu/src/__tests__/setup.ts b/packages/webgpu/src/__tests__/setup.ts index c6c7de4ba4..548a94aa5d 100644 --- a/packages/webgpu/src/__tests__/setup.ts +++ b/packages/webgpu/src/__tests__/setup.ts @@ -108,9 +108,8 @@ interface GPUTestingContext { probe: number | null; probeSize: number; pixelRatio: number; - // The context ids of two mounted canvases, toneMapping "extended" and - // "standard", for the CanvasToneMapping spec. - toneMappingCanvases?: { + // See CanvasToneMapping.spec.ts. + toneMappingCanvases: { extended: number | null; standard: number | null; }; diff --git a/packages/webgpu/src/index.tsx b/packages/webgpu/src/index.tsx index 64dcc568ae..23c8422051 100644 --- a/packages/webgpu/src/index.tsx +++ b/packages/webgpu/src/index.tsx @@ -123,6 +123,9 @@ declare global { pixelFormat?: NativeBufferPixelFormat, ) => VideoPlayer; writeTestVideoFile: () => string; + // Test only: the colorspace and dynamic range settings of a mounted + // canvas's layer as JSON, or "" where there is no such layer. + describeCanvasLayer: (contextId: number) => string; // An empty native buffer (AHardwareBuffer / IOSurface) to render into // through importSharedTextureMemory() and display in a NativeBufferView. // Prefer the createNativeBuffer() export. From 7dc51409e3b8d8008474c63a9a1f7185dd968141 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Wed, 25 Nov 2026 14:45:35 +0200 Subject: [PATCH 4/4] =?UTF-8?q?fix(=F0=9F=8D=8F):=20clip=20"standard"=20an?= =?UTF-8?q?d=20show=20"extended"=20untouched=20on=20iOS=2018+=20/=2026?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CanvasToneMapping spec and its test-only describeCanvasLayer hook are replaced by a diagnostic screen (Diagnostics > Canvas toneMapping): an "extended" and a "standard" rgba16float canvas side by side, each a ramp from 0 to 4.0 with a marker at 1.0 and steps of 0.5 above it, next to a strip of plain SDR white, with buttons to mount either canvas alone. Checked on that screen on an iPhone with iOS 27.2.1 (EDR headroom 8x): wantsExtendedDynamicRangeContent = NO no longer clipped "standard" there, with or without this PR: a float drawable is untagged, and Core Animation shows it as is unless the layer asks for tone mapping. The layer now sets toneMapMode (iOS 18 / macOS 15 SDKs): "standard" is tone mapped to SDR from a declared headroom of 1.0, which keeps 0-1.0 as is and clips above, and "extended" is never tone mapped, so it is not compressed to fit the headroom of 16 it declares and clips at the display's peak. toneMapMode is guarded at compile time like the 26-SDK APIs. --- apps/example/src/App.tsx | 3 + .../src/Diagnostics/DiagnosticsList.tsx | 4 + apps/example/src/Diagnostics/ToneMapping.tsx | 216 ++++++++++++++++++ apps/example/src/Route.ts | 1 + apps/example/src/Tests.tsx | 21 -- packages/webgpu/apple/ApplePlatformContext.h | 2 - packages/webgpu/apple/ApplePlatformContext.mm | 9 - packages/webgpu/apple/MetalLayerColorSpace.h | 20 +- packages/webgpu/apple/MetalLayerColorSpace.mm | 67 +++--- packages/webgpu/cpp/rnwgpu/PlatformContext.h | 7 - packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h | 13 -- .../src/__tests__/CanvasToneMapping.spec.ts | 71 ------ packages/webgpu/src/__tests__/setup.ts | 5 - packages/webgpu/src/index.tsx | 3 - 14 files changed, 263 insertions(+), 179 deletions(-) create mode 100644 apps/example/src/Diagnostics/ToneMapping.tsx delete mode 100644 packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index d2aa9ddd69..ff036d6f16 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -49,6 +49,7 @@ import { BackgroundDetach } from "./Diagnostics/BackgroundDetach"; import { SurfaceChurn } from "./Diagnostics/SurfaceChurn"; import { TransparencyMode } from "./Diagnostics/TransparencyMode"; import { DeviceDestroyBeforeDetach } from "./Diagnostics/DeviceDestroyBeforeDetach"; +import { ToneMapping } from "./Diagnostics/ToneMapping"; import { StorageBufferVertices } from "./StorageBufferVertices"; import { SharedTextureMemory } from "./SharedTextureMemory"; import { ImportExternalTexture } from "./ImportExternalTexture"; @@ -113,6 +114,7 @@ const linking: LinkingOptions = { SurfaceChurn: "diagnostics/surface-churn", TransparencyMode: "diagnostics/transparency-mode", DeviceDestroyBeforeDetach: "diagnostics/device-destroy-before-detach", + ToneMapping: "diagnostics/tone-mapping", StorageBufferVertices: "storage-buffer-vertices", SharedTextureMemory: "shared-texture-memory", ImportExternalTexture: "import-external-texture", @@ -215,6 +217,7 @@ function App() { name="DeviceDestroyBeforeDetach" component={DeviceDestroyBeforeDetach} /> + { diff --git a/apps/example/src/Diagnostics/ToneMapping.tsx b/apps/example/src/Diagnostics/ToneMapping.tsx new file mode 100644 index 0000000000..916af71dde --- /dev/null +++ b/apps/example/src/Diagnostics/ToneMapping.tsx @@ -0,0 +1,216 @@ +import React, { useEffect, useRef, useState } from "react"; +import { PixelRatio, Pressable, StyleSheet, Text, View } from "react-native"; +import type { CanvasRef } from "react-native-webgpu"; +import { Canvas } from "react-native-webgpu"; + +// Two rgba16float canvases side by side, toneMapping "extended" on the left +// and "standard" on the right, each a gray ramp from 0 (bottom) to MAX_VALUE +// (top) in canvas values: smooth up to a full-width black line at 1.0, then +// in steps of 0.5, with short ticks at every other whole value. A strip of +// plain SDR white runs beside each canvas, and the buttons mount one canvas +// alone. On an HDR display: +// - extended: the steps above 1.0 are brighter than the white strip, up to +// the display's headroom (about 2.5 for 8x, the values being sRGB-encoded), +// and are flat only from there; +// - standard: everything above 1.0 is the white of the strip, with no steps; +// - below the 1.0 line both look the same. +// A screenshot does not capture EDR: look at the device. +const MAX_VALUE = 4; + +const shader = /* wgsl */ ` +struct VertexOut { + @builtin(position) position: vec4f, + @location(0) uv: vec2f, +}; + +@vertex +fn vs(@builtin(vertex_index) index: u32) -> VertexOut { + let corners = array(vec2f(-1, -1), vec2f(3, -1), vec2f(-1, 3)); + var out: VertexOut; + out.position = vec4f(corners[index], 0, 1); + out.uv = (corners[index] + 1) * 0.5; + return out; +} + +@fragment +fn fs(in: VertexOut) -> @location(0) vec4f { + let ramp = in.uv.y * ${MAX_VALUE}.0; + // Smooth up to 1.0, then steps of 0.5, so the step where it stops shows. + let value = select(floor(ramp * 2) / 2, ramp, ramp <= 1); + let line = abs(ramp - round(ramp)) < fwidth(ramp) * 2; + let marker = line && round(ramp) == 1; + let tick = line && round(ramp) > 1 && in.uv.x < 0.25; + if (marker || tick) { + return vec4f(0, 0, 0, 1); + } + return vec4f(value, value, value, 1); +} +`; + +const RampCanvas = ({ mode }: { mode: GPUCanvasToneMappingMode }) => { + const ref = useRef(null); + useEffect(() => { + let cancelled = false; + (async () => { + const adapter = await navigator.gpu.requestAdapter(); + const device = await adapter?.requestDevice(); + const context = ref.current?.getContext("webgpu"); + if (cancelled || !device || !context) { + return; + } + const canvas = context.canvas as HTMLCanvasElement; + canvas.width = canvas.clientWidth * PixelRatio.get(); + canvas.height = canvas.clientHeight * PixelRatio.get(); + const format: GPUTextureFormat = "rgba16float"; + context.configure({ + device, + format, + alphaMode: "opaque", + toneMapping: { mode }, + }); + const module = device.createShaderModule({ code: shader }); + const pipeline = device.createRenderPipeline({ + layout: "auto", + vertex: { module, entryPoint: "vs" }, + fragment: { module, entryPoint: "fs", targets: [{ format }] }, + }); + const encoder = device.createCommandEncoder(); + const pass = encoder.beginRenderPass({ + colorAttachments: [ + { + view: context.getCurrentTexture().createView(), + loadOp: "clear", + storeOp: "store", + }, + ], + }); + pass.setPipeline(pipeline); + pass.draw(3); + pass.end(); + device.queue.submit([encoder.finish()]); + context.present(); + })(); + return () => { + cancelled = true; + }; + }, [mode]); + return ( + + {mode} + + + {/* Plain SDR white, the reference for the part above 1.0. */} + + + + ); +}; + +type Shown = "both" | "extended" | "standard"; + +export const ToneMapping = () => { + const [shown, setShown] = useState("both"); + return ( + + + {(["both", "extended", "standard"] as const).map((option) => ( + setShown(option)} + style={[styles.button, shown === option && styles.buttonActive]} + > + {option} + + ))} + + + {shown !== "standard" ? ( + + ) : ( + + )} + + {Array.from({ length: MAX_VALUE + 1 }, (_, value) => ( + + {value.toFixed(1)} + + ))} + + {shown !== "extended" ? ( + + ) : ( + + )} + + + ); +}; + +const styles = StyleSheet.create({ + container: { + flex: 1, + padding: 16, + backgroundColor: "black", + }, + row: { + flex: 1, + flexDirection: "row", + }, + column: { + flex: 1, + }, + title: { + color: "white", + textAlign: "center", + marginBottom: 8, + }, + pair: { + flex: 1, + flexDirection: "row", + }, + canvas: { + flex: 1, + }, + sdrWhite: { + width: 12, + marginTop: "auto", + height: "75%", + backgroundColor: "white", + }, + buttons: { + flexDirection: "row", + justifyContent: "center", + gap: 8, + marginBottom: 8, + }, + button: { + paddingHorizontal: 12, + paddingVertical: 6, + borderRadius: 6, + backgroundColor: "#333", + }, + buttonActive: { + backgroundColor: "#06c", + }, + buttonText: { + color: "white", + }, + scale: { + width: 40, + marginTop: 28, + }, + label: { + position: "absolute", + width: "100%", + textAlign: "center", + color: "white", + fontSize: 12, + transform: [{ translateY: 7 }], + }, +}); diff --git a/apps/example/src/Route.ts b/apps/example/src/Route.ts index 3b16552fbe..eda62b1f67 100644 --- a/apps/example/src/Route.ts +++ b/apps/example/src/Route.ts @@ -40,6 +40,7 @@ export type Routes = { SurfaceChurn: undefined; TransparencyMode: undefined; DeviceDestroyBeforeDetach: undefined; + ToneMapping: undefined; StorageBufferVertices: undefined; SharedTextureMemory: undefined; ImportExternalTexture: undefined; diff --git a/apps/example/src/Tests.tsx b/apps/example/src/Tests.tsx index 325c457bb0..657965607c 100644 --- a/apps/example/src/Tests.tsx +++ b/apps/example/src/Tests.tsx @@ -8,7 +8,6 @@ import { findNodeHandle, } from "react-native"; import { - Canvas, GPUOffscreenCanvas, // importDevice, // The flags as the package exports them, not the globals of the same name @@ -19,7 +18,6 @@ import { GPUShaderStage as exportedShaderStage, GPUTextureUsage as exportedTextureUsage, } from "react-native-webgpu"; -import type { CanvasRef } from "react-native-webgpu"; // Imported after react-native-webgpu on purpose: webgpu installs natively on // import and settles on a Dawn instance of its own while Skia's library is not // loaded yet. On Android, Skia's device then lives on a second instance, and @@ -52,9 +50,6 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { // A fixed native view the drawElementImageToTexture spec snapshots: four // 32x32pt solid quadrants (red, lime, blue, white), 64x64pt in total. const probeRef = useRef>(null); - // One canvas per toneMapping mode, for the CanvasToneMapping spec. - const extendedCanvasRef = useRef(null); - const standardCanvasRef = useRef(null); useEffect(() => { let cancelled = false; (async () => { @@ -141,10 +136,6 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { probe: findNodeHandle(probeRef.current), probeSize: PROBE_SIZE, pixelRatio: PixelRatio.get(), - toneMappingCanvases: { - extended: extendedCanvasRef.current?.getContextId() ?? null, - standard: standardCanvasRef.current?.getContextId() ?? null, - }, }, workletDeviceStress, workletSerialization, @@ -231,18 +222,6 @@ export const Tests = ({ assets: { di3D, saturn, moon } }: AssetProps) => { }} /> - - - - #include "AppleVideoPlayer.h" -#include "MetalLayerColorSpace.h" #import "RNWGUIKit.h" #include "RNWebGPUManager.h" @@ -560,14 +559,6 @@ ImageData renderViewIntoPixels(const ResolvedSnapshot &snapshot, return writeAppleTestVideoFile(); } -std::string -ApplePlatformContext::describeNativeSurfaceLayer(void *nativeSurface) { - if (nativeSurface == nullptr) { - return ""; - } - return describeCAMetalLayerColorSpace((__bridge CAMetalLayer *)nativeSurface); -} - VideoFrameHandle ApplePlatformContext::wrapNativeBuffer(void *pointer) { return wrapCVPixelBuffer(static_cast(pointer)); } diff --git a/packages/webgpu/apple/MetalLayerColorSpace.h b/packages/webgpu/apple/MetalLayerColorSpace.h index 63b715bf4f..632006f8f1 100644 --- a/packages/webgpu/apple/MetalLayerColorSpace.h +++ b/packages/webgpu/apple/MetalLayerColorSpace.h @@ -5,8 +5,6 @@ #import -#include - #include "CanvasColorSpace.h" // preferredDynamicRange, contentsHeadroom and CADynamicRange* are declared @@ -24,6 +22,19 @@ #define RNWGPU_HAS_CA_DYNAMIC_RANGE 0 #endif +// CALayer.toneMapMode is declared only by the iOS 18 / macOS 15 / visionOS 2 +// SDKs (Xcode 16) and later. +#if (TARGET_OS_IOS && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \ + __IPHONE_OS_VERSION_MAX_ALLOWED >= 180000) || \ + (TARGET_OS_VISION && defined(__VISION_OS_VERSION_MAX_ALLOWED) && \ + __VISION_OS_VERSION_MAX_ALLOWED >= 20000) || \ + (TARGET_OS_OSX && defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \ + __MAC_OS_X_VERSION_MAX_ALLOWED >= 150000) +#define RNWGPU_HAS_CA_TONE_MAP_MODE 1 +#else +#define RNWGPU_HAS_CA_TONE_MAP_MODE 0 +#endif + namespace rnwgpu { // Tags a new CAMetalLayer with the colorspace of the canvas, and opts it into @@ -34,9 +45,4 @@ namespace rnwgpu { void applyCAMetalLayerColorSpace(CAMetalLayer *layer, CanvasLayerColorSpace colorSpace); -// Test only: what applyCAMetalLayerColorSpace set on the layer, as a JSON -// object (see PlatformContext::describeNativeSurfaceLayer). The getters of a -// model layer are safe off the main thread. -std::string describeCAMetalLayerColorSpace(CAMetalLayer *layer); - } // namespace rnwgpu diff --git a/packages/webgpu/apple/MetalLayerColorSpace.mm b/packages/webgpu/apple/MetalLayerColorSpace.mm index a310b30a0a..4ed655bda6 100644 --- a/packages/webgpu/apple/MetalLayerColorSpace.mm +++ b/packages/webgpu/apple/MetalLayerColorSpace.mm @@ -9,7 +9,7 @@ #if RNWGPU_HAS_CA_DYNAMIC_RANGE // The headroom an "extended" layer declares. A WebGPU canvas puts no ceiling // on its values, so it asks for more than any display has (float16 holds far -// more) and lets the display clamp to what it can show. +// more). With tone mapping off the display clips at what it can show. static constexpr CGFloat kExtendedContentsHeadroom = 16.0; #endif @@ -27,12 +27,10 @@ // converted from sRGB first. // // The toneMapping mode says what happens to values above 1.0 on a float -// canvas. Core Animation keeps an extended colorspace inside the display's -// SDR range unless the layer asks for extended dynamic range content, so -// without it the values clip at white: that is "standard". For "extended" the -// layer opts in, and the values use the display's headroom (EDR) as they do in -// a browser. An 8-bit canvas cannot hold values above 1.0, so the flag has no -// visible effect there. +// canvas: "standard" clips them at SDR white, and "extended" shows them +// brighter, up to the display's headroom (EDR), as a browser does. An 8-bit +// canvas cannot hold values above 1.0, so the mode has no visible effect +// there. void applyCAMetalLayerColorSpace(CAMetalLayer *layer, CanvasLayerColorSpace colorSpace) { // Main thread only: the properties land in the calling thread's implicit @@ -50,52 +48,39 @@ void applyCAMetalLayerColorSpace(CAMetalLayer *layer, CGColorSpaceRelease(cgColorSpace); const bool extendedRange = colorSpace.toneMappingMode == CanvasToneMappingMode::Extended; - // The property does not exist on tvOS (the SDK marks it unavailable, which - // @available cannot work around), so it is compiled only where it exists. - // On tvOS "extended" behaves like "standard". + // The EDR properties do not exist on tvOS (the SDK marks them unavailable, + // which @available cannot work around), so they are compiled only where + // they exist. On tvOS "extended" behaves like "standard". #if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { layer.wantsExtendedDynamicRangeContent = extendedRange; } +#if RNWGPU_HAS_CA_TONE_MAP_MODE + // The wantsExtendedDynamicRangeContent flag no longer clips by itself: a + // float drawable is untagged, and Core Animation shows it as is, above 1.0 + // too, unless the layer asks for tone mapping. "standard" asks for it (to + // SDR, see preferredDynamicRange below), and "extended" turns it off, so + // values up to the display's headroom are shown as they are, not + // compressed to fit the declared headroom. + if (@available(iOS 18.0, macOS 15.0, visionOS 2.0, *)) { + layer.toneMapMode = + extendedRange ? CAToneMapModeNever : CAToneMapModeIfSupported; + } #endif - // 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 RNWGPU_HAS_CA_DYNAMIC_RANGE + // Since iOS and macOS 26 the flag alone no longer lights EDR: the layer + // asks for it with preferredDynamicRange, which needs a contentsHeadroom + // above 1.0 to take effect. "standard" declares a headroom of 1.0: tone + // mapped from an untagged headroom (0), its values below 1.0 come out + // darker, while from 1.0 to SDR they are kept as is and clipped above. if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) { layer.preferredDynamicRange = extendedRange ? CADynamicRangeHigh : CADynamicRangeStandard; - layer.contentsHeadroom = extendedRange ? kExtendedContentsHeadroom : 0.0; + layer.contentsHeadroom = extendedRange ? kExtendedContentsHeadroom : 1.0; } #endif - (void)extendedRange; -} - -std::string describeCAMetalLayerColorSpace(CAMetalLayer *layer) { - NSMutableDictionary *info = [NSMutableDictionary dictionary]; - if (CGColorSpaceRef colorSpace = layer.colorspace) { - if (CFStringRef name = CGColorSpaceGetName(colorSpace)) { - info[@"colorspace"] = (__bridge NSString *)name; - } - } -#if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION - if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { - info[@"wantsExtendedDynamicRangeContent"] = - @(layer.wantsExtendedDynamicRangeContent); - } #endif -#if RNWGPU_HAS_CA_DYNAMIC_RANGE - // CADynamicRangeHigh and CADynamicRangeStandard are "high" and "standard". - if (@available(iOS 26.0, macOS 26.0, visionOS 26.0, *)) { - info[@"preferredDynamicRange"] = layer.preferredDynamicRange; - info[@"contentsHeadroom"] = @(layer.contentsHeadroom); - } -#endif - // Strings, numbers and booleans only: serializing cannot fail. - NSData *json = [NSJSONSerialization dataWithJSONObject:info - options:0 - error:nil]; - return std::string(static_cast(json.bytes), json.length); + (void)extendedRange; } } // namespace rnwgpu diff --git a/packages/webgpu/cpp/rnwgpu/PlatformContext.h b/packages/webgpu/cpp/rnwgpu/PlatformContext.h index 7ce2ea2a63..31c72ee287 100644 --- a/packages/webgpu/cpp/rnwgpu/PlatformContext.h +++ b/packages/webgpu/cpp/rnwgpu/PlatformContext.h @@ -228,13 +228,6 @@ class PlatformContext { virtual VideoFrameHandle createTestVideoFrame(uint32_t width, uint32_t height, VideoPixelFormat format) = 0; - // Test only (the e2e spec of the Canvas toneMapping prop): the dynamic range - // and colorspace settings of the layer behind a canvas's native surface, as - // a JSON object, or "" where the platform has no such layer. - virtual std::string describeNativeSurfaceLayer(void * /*nativeSurface*/) { - return ""; - } - // Allocate an empty, GPU-renderable buffer that a NativeBufferView can // display: an AHardwareBuffer (Android) or an IOSurface (Apple platforms), // with premultiplied pixels in `format`, tagged with `displayP3` or sRGB diff --git a/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h b/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h index 0300a925b8..29428235d6 100644 --- a/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h +++ b/packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h @@ -526,17 +526,6 @@ class RNWebGPU : public NativeObject { return rnwgpu::SurfaceRegistry::getInstance().allocateContextId(); } - // Test only: see PlatformContext::describeNativeSurfaceLayer. - std::string describeCanvasLayer(int contextId) { - auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); - auto info = registry.getSurfaceInfo(contextId); - if (info == nullptr) { - return ""; - } - return _platformContext->describeNativeSurfaceLayer( - info->getNativeInfo().nativeSurface); - } - std::shared_ptr getNativeSurface(int contextId) { auto ®istry = rnwgpu::SurfaceRegistry::getInstance(); auto info = registry.getSurfaceInfo(contextId); @@ -578,8 +567,6 @@ class RNWebGPU : public NativeObject { &RNWebGPU::allocateContextId); installMethod(runtime, prototype, "getNativeSurface", &RNWebGPU::getNativeSurface); - installMethod(runtime, prototype, "describeCanvasLayer", - &RNWebGPU::describeCanvasLayer); installMethod(runtime, prototype, "destroyContext", &RNWebGPU::destroyContext); installMethod(runtime, prototype, "MakeWebGPUCanvasContext", diff --git a/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts b/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts deleted file mode 100644 index 86a8ad6f44..0000000000 --- a/packages/webgpu/src/__tests__/CanvasToneMapping.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { client, itSkipsOnWeb } from "./setup"; - -// What RNWebGPU.describeCanvasLayer reports for the CAMetalLayer of a mounted -// canvas. -type LayerInfo = { - colorspace?: string; - wantsExtendedDynamicRangeContent?: boolean; - preferredDynamicRange?: string; - contentsHeadroom?: number; -}; - -type EvalResult = - | { kind: "skip"; reason: string } - | { kind: "ok"; extended: LayerInfo; standard: LayerInfo }; - -// The harness (apps/example/src/Tests.tsx) mounts two canvases, one with -// toneMapping "extended" and one with "standard". Since the 26 SDKs the layer -// only shows values above 1.0 brighter than SDR white when it asks for a high -// dynamic range and declares a headroom: wantsExtendedDynamicRangeContent -// alone no longer does it. preferredDynamicRange and contentsHeadroom are -// reported only on iOS, macOS and visionOS 26+, in an app built with Xcode 26+. -describe("Canvas toneMapping", () => { - itSkipsOnWeb( - "asks the layer for EDR with the extended mode, and only then", - async () => { - const result = await client.eval, EvalResult>( - ({ views }) => { - const ids = views?.toneMappingCanvases; - if (!ids || ids.extended === null || ids.standard === null) { - return { kind: "skip", reason: "no toneMapping canvases mounted" }; - } - const describe = (contextId: number) => { - const json = RNWebGPU.describeCanvasLayer(contextId); - return json ? (JSON.parse(json) as LayerInfo) : null; - }; - const extended = describe(ids.extended); - const standard = describe(ids.standard); - if (!extended || !standard) { - return { kind: "skip", reason: "no Core Animation layer here" }; - } - if (extended.preferredDynamicRange === undefined) { - return { - kind: "skip", - reason: "preferredDynamicRange needs the 26 SDKs and OSes", - }; - } - return { kind: "ok", extended, standard }; - }, - ); - - if (result.kind === "skip") { - console.log(`CanvasToneMapping: skipping (${result.reason})`); - return; - } - const { extended, standard } = result; - - expect(extended.preferredDynamicRange).toBe("high"); - expect(extended.contentsHeadroom).toBeGreaterThan(1); - expect(extended.wantsExtendedDynamicRangeContent).toBe(true); - - expect(standard.preferredDynamicRange).toBe("standard"); - expect(standard.contentsHeadroom).toBe(0); - expect(standard.wantsExtendedDynamicRangeContent).toBe(false); - - // Both default to colorSpace "srgb", tagged with the gamma-encoded - // extended variant so that float formats keep values above 1.0. - expect(extended.colorspace).toBe("kCGColorSpaceExtendedSRGB"); - expect(standard.colorspace).toBe("kCGColorSpaceExtendedSRGB"); - }, - ); -}); diff --git a/packages/webgpu/src/__tests__/setup.ts b/packages/webgpu/src/__tests__/setup.ts index 548a94aa5d..b8e20b6d0a 100644 --- a/packages/webgpu/src/__tests__/setup.ts +++ b/packages/webgpu/src/__tests__/setup.ts @@ -108,11 +108,6 @@ interface GPUTestingContext { probe: number | null; probeSize: number; pixelRatio: number; - // See CanvasToneMapping.spec.ts. - toneMappingCanvases: { - extended: number | null; - standard: number | null; - }; }; // Native app harness only: Skia's Graphite device wrapped with // importDevice(), or null when Skia has no GPU. See ImportDevice.spec.ts diff --git a/packages/webgpu/src/index.tsx b/packages/webgpu/src/index.tsx index 23c8422051..64dcc568ae 100644 --- a/packages/webgpu/src/index.tsx +++ b/packages/webgpu/src/index.tsx @@ -123,9 +123,6 @@ declare global { pixelFormat?: NativeBufferPixelFormat, ) => VideoPlayer; writeTestVideoFile: () => string; - // Test only: the colorspace and dynamic range settings of a mounted - // canvas's layer as JSON, or "" where there is no such layer. - describeCanvasLayer: (contextId: number) => string; // An empty native buffer (AHardwareBuffer / IOSurface) to render into // through importSharedTextureMemory() and display in a NativeBufferView. // Prefer the createNativeBuffer() export.