|
1 | 1 | #import <CoreGraphics/CoreGraphics.h> |
2 | 2 | #import <Foundation/Foundation.h> |
3 | 3 | #import <QuartzCore/CAMetalLayer.h> |
4 | | -#import <QuartzCore/CATransaction.h> |
5 | 4 |
|
6 | | -#include "CanvasColorSpace.h" |
7 | | -#include "webgpu/webgpu_cpp.h" |
| 5 | +#include "MetalLayerColorSpace.h" |
8 | 6 |
|
9 | 7 | namespace rnwgpu { |
10 | 8 |
|
11 | 9 | // WebGPU canvas values are encoded with the sRGB transfer function regardless |
12 | | -// of the texture format, and GPUCanvasConfiguration.colorSpace says which |
13 | | -// primaries they are in: "srgb" (the default) or "display-p3". The same shader |
14 | | -// output must display identically on bgra8unorm and rgba16float surfaces. |
15 | | -// Core Animation interprets a float-format layer with a nil colorspace as |
16 | | -// extended *linear* sRGB, which displays the same values noticeably brighter. |
17 | | -// Tagging the layer as (gamma-encoded) extended sRGB matches the browser |
18 | | -// behavior: identical colors, with the extra precision of float16. For |
19 | | -// "display-p3" the layer is tagged Display P3 instead, or its extended range |
20 | | -// variant on a float format, so the values reach a wide-gamut display without |
21 | | -// being converted from sRGB first. |
| 10 | +// of the texture format, and the canvas's colorSpace says which primaries they |
| 11 | +// are in: "srgb" (the default) or "display-p3". The same shader output must |
| 12 | +// display identically on bgra8unorm and rgba16float surfaces. Core Animation |
| 13 | +// interprets a float-format layer with a nil colorspace as extended *linear* |
| 14 | +// sRGB, which displays the same values noticeably brighter. The layer is |
| 15 | +// tagged before the format is known (configure() comes later, from JS), so it |
| 16 | +// gets the gamma-encoded extended variant of its gamut: on an 8-bit format it |
| 17 | +// reads the same values as the plain colorspace, and on a float format it |
| 18 | +// gives identical colors with the extra precision of float16, as in a browser. |
| 19 | +// For "display-p3" the values reach a wide-gamut display without being |
| 20 | +// converted from sRGB first. |
22 | 21 | // |
23 | | -// GPUCanvasConfiguration.toneMapping.mode says what happens to values above |
24 | | -// 1.0 on a float canvas. Core Animation keeps an extended colorspace inside |
25 | | -// the display's SDR range unless the layer asks for extended dynamic range |
26 | | -// content, so without it the values clip at white: that is "standard". For |
27 | | -// "extended" the layer opts in, and the values use the display's headroom |
28 | | -// (EDR) as they do in a browser. An 8-bit canvas cannot hold values above 1.0, |
29 | | -// so the flag only applies to a float format. |
30 | | -void applyCAMetalLayerColorSpace(void *nativeSurface, |
31 | | - wgpu::TextureFormat format, |
32 | | - CanvasColorSpace canvasColorSpace, |
33 | | - CanvasToneMappingMode toneMappingMode) { |
34 | | - CALayer *layer = (__bridge CALayer *)nativeSurface; |
35 | | - if (![layer isKindOfClass:[CAMetalLayer class]]) { |
36 | | - return; |
37 | | - } |
38 | | - auto metalLayer = static_cast<CAMetalLayer *>(layer); |
39 | | - const bool isFloat = format == wgpu::TextureFormat::RGBA16Float; |
40 | | - CFStringRef name = nullptr; |
41 | | - if (canvasColorSpace == CanvasColorSpace::DisplayP3) { |
42 | | - name = isFloat ? kCGColorSpaceExtendedDisplayP3 : kCGColorSpaceDisplayP3; |
43 | | - } else if (isFloat) { |
44 | | - name = kCGColorSpaceExtendedSRGB; |
45 | | - } |
46 | | - if (name != nullptr) { |
47 | | - CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(name); |
48 | | - metalLayer.colorspace = colorSpace; |
49 | | - CGColorSpaceRelease(colorSpace); |
50 | | - } else if (metalLayer.colorspace != nil) { |
51 | | - // Restore the default (no color matching) when reconfiguring back to an |
52 | | - // 8-bit sRGB canvas. |
53 | | - metalLayer.colorspace = nil; |
54 | | - } |
| 22 | +// The toneMapping mode says what happens to values above 1.0 on a float |
| 23 | +// canvas. Core Animation keeps an extended colorspace inside the display's |
| 24 | +// SDR range unless the layer asks for extended dynamic range content, so |
| 25 | +// without it the values clip at white: that is "standard". For "extended" the |
| 26 | +// layer opts in, and the values use the display's headroom (EDR) as they do in |
| 27 | +// a browser. An 8-bit canvas cannot hold values above 1.0, so the flag has no |
| 28 | +// visible effect there. |
| 29 | +void applyCAMetalLayerColorSpace(CAMetalLayer *layer, |
| 30 | + CanvasLayerColorSpace colorSpace) { |
| 31 | + // Main thread only: the properties land in the calling thread's implicit |
| 32 | + // CATransaction, which the main run loop commits. Committing it from any |
| 33 | + // other thread ([CATransaction flush]) commits the whole layer tree there, |
| 34 | + // and lays out whatever UIKit view has a layout pending, off the main |
| 35 | + // thread (react-native-skia #4474). |
| 36 | + NSCAssert(NSThread.isMainThread, |
| 37 | + @"applyCAMetalLayerColorSpace must be called on the main thread"); |
| 38 | + CGColorSpaceRef cgColorSpace = CGColorSpaceCreateWithName( |
| 39 | + colorSpace.colorSpace == CanvasColorSpace::DisplayP3 |
| 40 | + ? kCGColorSpaceExtendedDisplayP3 |
| 41 | + : kCGColorSpaceExtendedSRGB); |
| 42 | + layer.colorspace = cgColorSpace; |
| 43 | + CGColorSpaceRelease(cgColorSpace); |
55 | 44 | const bool extendedRange = |
56 | | - isFloat && toneMappingMode == CanvasToneMappingMode::Extended; |
| 45 | + colorSpace.toneMappingMode == CanvasToneMappingMode::Extended; |
57 | 46 | // The property does not exist on tvOS (the SDK marks it unavailable, which |
58 | 47 | // @available cannot work around), so it is compiled only where it exists. |
59 | 48 | // On tvOS "extended" behaves like "standard". |
60 | 49 | #if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_VISION |
61 | 50 | if (@available(iOS 16.0, macOS 10.11, visionOS 1.0, *)) { |
62 | | - if (metalLayer.wantsExtendedDynamicRangeContent != extendedRange) { |
63 | | - metalLayer.wantsExtendedDynamicRangeContent = extendedRange; |
64 | | - } |
| 51 | + layer.wantsExtendedDynamicRangeContent = extendedRange; |
65 | 52 | } |
66 | 53 | #else |
67 | 54 | (void)extendedRange; |
68 | 55 | #endif |
69 | | - // The change must be set synchronously so the first present already sees |
70 | | - // it, and it must reach the render server. On a non-main thread (RN JS or |
71 | | - // worklet runtime) the property lands in that thread's implicit |
72 | | - // CATransaction, which may never commit on threads without a spinning |
73 | | - // runloop, so flush it now. On the main thread the runloop commits it. |
74 | | - if (!NSThread.isMainThread) { |
75 | | - [CATransaction flush]; |
76 | | - } |
77 | 56 | } |
78 | 57 |
|
79 | 58 | } // namespace rnwgpu |
0 commit comments