Skip to content

Commit 6291043

Browse files
committed
🔧
1 parent a9b00fd commit 6291043

8 files changed

Lines changed: 28 additions & 122 deletions

File tree

packages/webgpu/apple/ApplePlatformContext.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class ApplePlatformContext : public PlatformContext {
1313
wgpu::Surface makeSurface(wgpu::Instance instance, void *surface, int width,
1414
int height) override;
1515

16-
void configureSurfaceColor(void *nativeSurface,
17-
const SurfaceColorConfig &config) override;
18-
1916
ImageData createImageBitmap(std::string blobId, double offset,
2017
double size) override;
2118

packages/webgpu/apple/ApplePlatformContext.mm

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <TargetConditionals.h>
44

5-
#import <QuartzCore/CAMetalLayer.h>
65
#import <React/RCTBlobManager.h>
76
#import <React/RCTBridge+Private.h>
87
#import <ReactCommon/RCTTurboModule.h>
@@ -40,70 +39,6 @@ void checkIfUsingSimulatorWithAPIValidation() {
4039
return instance.CreateSurface(&surfaceDescriptor);
4140
}
4241

43-
void ApplePlatformContext::configureSurfaceColor(
44-
void *nativeSurface, const SurfaceColorConfig &config) {
45-
if (!nativeSurface) {
46-
NSLog(@"[RNWebGPU] configureSurfaceColor: nativeSurface is null");
47-
return;
48-
}
49-
CAMetalLayer *layer = (__bridge CAMetalLayer *)nativeSurface;
50-
51-
// CAMetalLayer property setters are documented as thread-safe, and we need
52-
// these changes to land before the caller renders / presents the next
53-
// frame, so apply synchronously here instead of bouncing to the main queue.
54-
[CATransaction begin];
55-
[CATransaction setDisableActions:YES];
56-
57-
CGColorSpaceRef colorSpace = nullptr;
58-
if (config.extendedDynamicRange) {
59-
#if !TARGET_OS_OSX
60-
if (@available(iOS 16.0, *)) {
61-
layer.wantsExtendedDynamicRangeContent = YES;
62-
}
63-
#else
64-
layer.wantsExtendedDynamicRangeContent = YES;
65-
#endif
66-
// Extended linear sRGB allows shader values >1 to map into the display's
67-
// EDR headroom on supporting hardware.
68-
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceExtendedLinearSRGB);
69-
} else {
70-
#if !TARGET_OS_OSX
71-
if (@available(iOS 16.0, *)) {
72-
layer.wantsExtendedDynamicRangeContent = NO;
73-
}
74-
#else
75-
layer.wantsExtendedDynamicRangeContent = NO;
76-
#endif
77-
// Non-extended sRGB clamps float values >1 to SDR. Linear variants
78-
// ("kCGColorSpaceLinearSRGB") still pass values >1 through on iOS, so we
79-
// use gamma-encoded sRGB for the standard tone-mapping mode.
80-
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
81-
}
82-
if (colorSpace) {
83-
layer.colorspace = colorSpace;
84-
CGColorSpaceRelease(colorSpace);
85-
}
86-
87-
[CATransaction commit];
88-
89-
#if !TARGET_OS_OSX
90-
CGFloat headroom = 1.0;
91-
if (@available(iOS 16.0, *)) {
92-
headroom = UIScreen.mainScreen.currentEDRHeadroom;
93-
}
94-
NSLog(@"[RNWebGPU] HDR configure: extended=%d format=%u "
95-
@"layer.wantsEDR=%d layer.colorspace=%@ EDRHeadroom=%.2f",
96-
(int)config.extendedDynamicRange, (unsigned)config.format,
97-
(int)layer.wantsExtendedDynamicRangeContent, layer.colorspace,
98-
(double)headroom);
99-
#else
100-
NSLog(@"[RNWebGPU] HDR configure: extended=%d format=%u "
101-
@"layer.wantsEDR=%d layer.colorspace=%@",
102-
(int)config.extendedDynamicRange, (unsigned)config.format,
103-
(int)layer.wantsExtendedDynamicRangeContent, layer.colorspace);
104-
#endif
105-
}
106-
10742
static std::span<const uint8_t> nsDataToSpan(NSData *data) {
10843
return {static_cast<const uint8_t *>(data.bytes), data.length};
10944
}

packages/webgpu/apple/MetalView.mm

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,10 @@ - (void)configure {
2828
auto gpu = manager->_gpu;
2929
auto surface = manager->_platformContext->makeSurface(
3030
gpu, nativeSurface, size.width, size.height);
31-
auto info = registry.getSurfaceInfoOrCreate([_contextId intValue], gpu,
32-
size.width, size.height);
33-
info->switchToOnscreen(nativeSurface, surface);
34-
// If a previous configure() call from JS already requested an HDR / color
35-
// config for this surface, replay it onto the freshly attached layer. This
36-
// covers the race where JS calls context.configure() before this MetalView
37-
// mounts (e.g., on a key-driven Canvas remount).
38-
if (auto colorConfig = info->getColorConfig()) {
39-
manager->_platformContext->configureSurfaceColor(nativeSurface,
40-
*colorConfig);
41-
}
31+
registry
32+
.getSurfaceInfoOrCreate([_contextId intValue], gpu, size.width,
33+
size.height)
34+
->switchToOnscreen(nativeSurface, surface);
4235
}
4336

4437
- (void)update {

packages/webgpu/cpp/rnwgpu/PlatformContext.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,13 @@ struct ImageData {
1717
wgpu::TextureFormat format;
1818
};
1919

20-
struct SurfaceColorConfig {
21-
wgpu::TextureFormat format = wgpu::TextureFormat::Undefined;
22-
bool extendedDynamicRange = false;
23-
};
24-
2520
class PlatformContext {
2621
public:
2722
PlatformContext() = default;
2823
virtual ~PlatformContext() = default;
2924

3025
virtual wgpu::Surface makeSurface(wgpu::Instance instance, void *surface,
3126
int width, int height) = 0;
32-
33-
// Apply extra platform-specific color configuration on the native surface
34-
// (e.g. CAMetalLayer wantsExtendedDynamicRangeContent / colorspace on
35-
// Apple). Default is a no-op for platforms without HDR support.
36-
virtual void configureSurfaceColor(void * /*nativeSurface*/,
37-
const SurfaceColorConfig & /*config*/) {}
3827
virtual ImageData createImageBitmap(std::string blobId, double offset,
3928
double size) = 0;
4029

packages/webgpu/cpp/rnwgpu/SurfaceRegistry.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <unordered_map>
77
#include <utility>
88

9-
#include "PlatformContext.h"
109
#include "webgpu/webgpu_cpp.h"
1110

1211
namespace rnwgpu {
@@ -42,6 +41,7 @@ class SurfaceInfo {
4241
config.width = width;
4342
config.height = height;
4443
config.presentMode = wgpu::PresentMode::Fifo;
44+
_rebindColorChain();
4545
_configure();
4646
}
4747

@@ -153,17 +153,21 @@ class SurfaceInfo {
153153
return config.device;
154154
}
155155

156-
void setColorConfig(const SurfaceColorConfig &cfg) {
156+
void setColorManagement(std::optional<wgpu::SurfaceColorManagement> mgmt) {
157157
std::unique_lock<std::shared_mutex> lock(_mutex);
158-
_colorConfig = cfg;
158+
_colorManagement = std::move(mgmt);
159+
_rebindColorChain();
160+
if (surface) {
161+
surface.Configure(&config);
162+
}
159163
}
160164

161-
std::optional<SurfaceColorConfig> getColorConfig() {
162-
std::shared_lock<std::shared_mutex> lock(_mutex);
163-
return _colorConfig;
165+
private:
166+
void _rebindColorChain() {
167+
config.nextInChain =
168+
_colorManagement ? &_colorManagement.value() : nullptr;
164169
}
165170

166-
private:
167171
void _configure() {
168172
if (surface) {
169173
surface.Configure(&config);
@@ -187,7 +191,7 @@ class SurfaceInfo {
187191
wgpu::SurfaceConfiguration config;
188192
int width;
189193
int height;
190-
std::optional<SurfaceColorConfig> _colorConfig;
194+
std::optional<wgpu::SurfaceColorManagement> _colorManagement;
191195
};
192196

193197
class SurfaceRegistry {

packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,13 @@ void GPUCanvasContext::configure(
3636
surfaceConfiguration.presentMode = wgpu::PresentMode::Fifo;
3737
_surfaceInfo->configure(surfaceConfiguration);
3838

39-
if (_platformContext) {
40-
SurfaceColorConfig colorConfig;
41-
colorConfig.format = surfaceConfiguration.format;
42-
colorConfig.extendedDynamicRange =
43-
configuration->toneMappingMode == GPUCanvasToneMappingMode::Extended;
44-
// Stash so a (re)attaching native layer can replay this config — see
45-
// MetalView::configure on Apple. This avoids a race where configure()
46-
// fires from JS before the MetalView has called switchToOnscreen.
47-
_surfaceInfo->setColorConfig(colorConfig);
48-
auto nativeInfo = _surfaceInfo->getNativeInfo();
49-
if (nativeInfo.nativeSurface) {
50-
_platformContext->configureSurfaceColor(nativeInfo.nativeSurface,
51-
colorConfig);
52-
}
53-
}
39+
wgpu::SurfaceColorManagement colorManagement;
40+
colorManagement.colorSpace = wgpu::PredefinedColorSpace::SRGB;
41+
colorManagement.toneMappingMode =
42+
configuration->toneMappingMode == GPUCanvasToneMappingMode::Extended
43+
? wgpu::ToneMappingMode::Extended
44+
: wgpu::ToneMappingMode::Standard;
45+
_surfaceInfo->setColorManagement(colorManagement);
5446
}
5547

5648
void GPUCanvasContext::unconfigure() {}

packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "GPU.h"
1515
#include "GPUCanvasConfiguration.h"
1616
#include "GPUTexture.h"
17-
#include "PlatformContext.h"
1817
#include "SurfaceRegistry.h"
1918

2019
namespace rnwgpu {
@@ -25,11 +24,9 @@ class GPUCanvasContext : public NativeObject<GPUCanvasContext> {
2524
public:
2625
static constexpr const char *CLASS_NAME = "GPUCanvasContext";
2726

28-
GPUCanvasContext(std::shared_ptr<GPU> gpu,
29-
std::shared_ptr<PlatformContext> platformContext,
30-
int contextId, int width, int height)
31-
: NativeObject(CLASS_NAME), _gpu(std::move(gpu)),
32-
_platformContext(std::move(platformContext)) {
27+
GPUCanvasContext(std::shared_ptr<GPU> gpu, int contextId, int width,
28+
int height)
29+
: NativeObject(CLASS_NAME), _gpu(std::move(gpu)) {
3330
_canvas = std::make_shared<Canvas>(nullptr, width, height);
3431
auto &registry = rnwgpu::SurfaceRegistry::getInstance();
3532
_surfaceInfo =
@@ -64,7 +61,6 @@ class GPUCanvasContext : public NativeObject<GPUCanvasContext> {
6461
std::shared_ptr<Canvas> _canvas;
6562
std::shared_ptr<SurfaceInfo> _surfaceInfo;
6663
std::shared_ptr<GPU> _gpu;
67-
std::shared_ptr<PlatformContext> _platformContext;
6864
};
6965

7066
} // namespace rnwgpu

packages/webgpu/cpp/rnwgpu/api/RNWebGPU.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class RNWebGPU : public NativeObject<RNWebGPU> {
7070

7171
std::shared_ptr<GPUCanvasContext>
7272
MakeWebGPUCanvasContext(int contextId, float width, float height) {
73-
auto ctx = std::make_shared<GPUCanvasContext>(
74-
_gpu, _platformContext, contextId, width, height);
73+
auto ctx =
74+
std::make_shared<GPUCanvasContext>(_gpu, contextId, width, height);
7575
return ctx;
7676
}
7777

0 commit comments

Comments
 (0)