Skip to content

Commit 2fb55e8

Browse files
DaveyEkewcandillon
andauthored
fix(🏞️): look up the blob module through the module registry (#600)
Co-authored-by: William Candillon <wcandillon@gmail.com>
1 parent 9afbee2 commit 2fb55e8

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎packages/webgpu/apple/ApplePlatformContext.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ class ApplePlatformContext : public PlatformContext {
1313
// UIView* / NSView* bridged to void*, or nullptr). Called on the main thread
1414
// only. Provided by WebGPUModule from React Native's view registry.
1515
using ViewLookup = std::function<void *(int)>;
16+
// Returns React Native's RCTBlobManager (unretained, bridged to void*), or
17+
// nullptr. Provided by WebGPUModule from React Native's module registry.
18+
using BlobManagerLookup = std::function<void *()>;
1619

17-
explicit ApplePlatformContext(ViewLookup viewLookup = nullptr);
20+
explicit ApplePlatformContext(ViewLookup viewLookup,
21+
BlobManagerLookup blobManagerLookup);
1822
~ApplePlatformContext() = default;
1923

2024
void snapshotView(const ViewSnapshotRequest &request,
@@ -52,6 +56,7 @@ class ApplePlatformContext : public PlatformContext {
5256

5357
private:
5458
ViewLookup _viewLookup;
59+
BlobManagerLookup _blobManagerLookup;
5560
};
5661

5762
} // namespace rnwgpu

‎packages/webgpu/apple/ApplePlatformContext.mm‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#import <CoreVideo/CoreVideo.h>
77
#import <ImageIO/ImageIO.h>
88
#import <React/RCTBlobManager.h>
9-
#import <React/RCTBridge+Private.h>
109
#import <ReactCommon/RCTTurboModule.h>
1110

1211
#include "AppleVideoPlayer.h"
@@ -35,8 +34,10 @@ void checkIfUsingSimulatorWithAPIValidation() {
3534
#endif
3635
}
3736

38-
ApplePlatformContext::ApplePlatformContext(ViewLookup viewLookup)
39-
: _viewLookup(std::move(viewLookup)) {
37+
ApplePlatformContext::ApplePlatformContext(ViewLookup viewLookup,
38+
BlobManagerLookup blobManagerLookup)
39+
: _viewLookup(std::move(viewLookup)),
40+
_blobManagerLookup(std::move(blobManagerLookup)) {
4041
checkIfUsingSimulatorWithAPIValidation();
4142
}
4243

@@ -360,8 +361,7 @@ ImageData renderViewIntoPixels(const ResolvedSnapshot &snapshot,
360361

361362
ImageData ApplePlatformContext::createImageBitmap(std::string blobId,
362363
double offset, double size) {
363-
RCTBlobManager *blobManager =
364-
[[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
364+
RCTBlobManager *blobManager = (__bridge RCTBlobManager *)_blobManagerLookup();
365365
NSData *blobData =
366366
[blobManager resolve:[NSString stringWithUTF8String:blobId.c_str()]
367367
offset:(long)offset
@@ -378,9 +378,8 @@ ImageData renderViewIntoPixels(const ResolvedSnapshot &snapshot,
378378
std::string blobId, double offset, double size,
379379
std::function<void(ImageData)> onSuccess,
380380
std::function<void(std::string)> onError) {
381-
// Resolve blob on current thread (requires RCTBridge access)
382-
RCTBlobManager *blobManager =
383-
[[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
381+
// Resolve blob on current thread
382+
RCTBlobManager *blobManager = (__bridge RCTBlobManager *)_blobManagerLookup();
384383
NSData *blobData =
385384
[blobManager resolve:[NSString stringWithUTF8String:blobId.c_str()]
386385
offset:(long)offset

‎packages/webgpu/apple/WebGPUModule.mm‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ @implementation WebGPUModule
3737
// (Fabric component views included), which drawElementImageToTexture needs.
3838
@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
3939

40+
// Synthesize the module registry so the TurboModule infra injects it too. It
41+
// reaches React Native's BlobModule in both Bridge and Bridgeless mode.
42+
@synthesize moduleRegistry = _moduleRegistry;
43+
4044
+ (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager {
4145
return webgpuManager;
4246
}
@@ -93,8 +97,18 @@ - (void)invalidate {
9397
[strongSelf.viewRegistry_DEPRECATED viewForReactTag:@(tag)];
9498
return (__bridge void *)view;
9599
};
100+
rnwgpu::ApplePlatformContext::BlobManagerLookup blobManagerLookup =
101+
[weakSelf]() -> void * {
102+
WebGPUModule *strongSelf = weakSelf;
103+
if (strongSelf == nil) {
104+
return nullptr;
105+
}
106+
return (__bridge void *)[strongSelf.moduleRegistry
107+
moduleForName:"BlobModule"];
108+
};
96109
std::shared_ptr<rnwgpu::PlatformContext> platformContext =
97-
std::make_shared<rnwgpu::ApplePlatformContext>(std::move(viewLookup));
110+
std::make_shared<rnwgpu::ApplePlatformContext>(
111+
std::move(viewLookup), std::move(blobManagerLookup));
98112
webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
99113
platformContext);
100114
return @true;

0 commit comments

Comments
 (0)