Skip to content

Commit

Permalink
feat: Add macOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
Saadnajmi committed Sep 19, 2024
1 parent f96936e commit 611c8f2
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace rnwgpu {

class IOSPlatformContext : public PlatformContext {
class ApplePlatformContext : public PlatformContext {
public:
IOSPlatformContext() = default;
~IOSPlatformContext() = default;
ApplePlatformContext() = default;
~ApplePlatformContext() = default;

wgpu::Surface makeSurface(wgpu::Instance instance, void *surface, int width,
int height) override;
Expand All @@ -16,4 +16,4 @@ class IOSPlatformContext : public PlatformContext {
double size) override;
};

} // namespace rnwgpu
} // namespace rnwgpu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "IOSPlatformContext.h"
#include "ApplePlatformContext.h"

#import <React/RCTBlobManager.h>
#import <React/RCTBridge+Private.h>
Expand All @@ -9,7 +9,7 @@

namespace rnwgpu {

wgpu::Surface IOSPlatformContext::makeSurface(wgpu::Instance instance,
wgpu::Surface ApplePlatformContext::makeSurface(wgpu::Instance instance,
void *surface, int width,
int height) {
wgpu::SurfaceDescriptorFromMetalLayer metalSurfaceDesc;
Expand All @@ -19,7 +19,7 @@
return instance.CreateSurface(&surfaceDescriptor);
}

ImageData IOSPlatformContext::createImageBitmap(std::string blobId,
ImageData ApplePlatformContext::createImageBitmap(std::string blobId,
double offset, double size) {
RCTBlobManager *blobManager =
[[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
Expand All @@ -32,12 +32,20 @@
throw std::runtime_error("Couldn't retrive blob data");
}

#if !TARGET_OS_OSX
UIImage *image = [UIImage imageWithData:blobData];
#else
NSImage *image = [[NSImage alloc] initWithData:blobData];
#endif
if (!image) {
throw std::runtime_error("Couldn't decode image");
}

#if !TARGET_OS_OSX
CGImageRef cgImage = image.CGImage;
#else
CGImageRef cgImage = [image CGImageForProposedRect:NULL context:NULL hints:NULL];
#endif
size_t width = CGImageGetWidth(cgImage);
size_t height = CGImageGetHeight(cgImage);
size_t bitsPerComponent = 8;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#import "WebGPUModule.h"
#import <UIKit/UIKit.h>
#import "RNWGUIKit.h"

@interface MetalView : UIView
@interface MetalView : RNWGPlatformView

@property NSNumber *contextId;

Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions packages/webgpu/apple/RNWGUIKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if !TARGET_OS_OSX
#import <UIKit/UIKit.h>
#else
#import <Appkit/Appkit.h>
#endif

#if !TARGET_OS_OSX
typedef UIView RNWGPlatformView;
#else
typedef NSView RNWGPlatformView;
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface SurfaceUtils : NSObject

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "WebGPUModule.h"
#import "GPUCanvasContext.h"
#include "IOSPlatformContext.h"
#include "ApplePlatformContext.h"

#import <React/RCTBridge+Private.h>
#import <React/RCTLog.h>
Expand Down Expand Up @@ -67,7 +67,7 @@ - (void)invalidate {
jsInvoker = cxxBridge.jsCallInvoker;
}
std::shared_ptr<rnwgpu::PlatformContext> platformContext =
std::make_shared<rnwgpu::IOSPlatformContext>();
std::make_shared<rnwgpu::ApplePlatformContext>();
// TODO: remove allocation here
webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
platformContext);
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "MetalView.h"
#import "RCTBridge.h"
#import "RNWGUIKit.h"
#import "WebGPUModule.h"
#import <React/RCTUIManager.h>
#import <React/RCTViewManager.h>
Expand All @@ -11,11 +12,11 @@ @implementation WebGPUViewManager

RCT_EXPORT_MODULE(WebGPUView)

- (UIView *)view {
- (RNWGPlatformView *)view {
return [MetalView new];
}

RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, UIView) {
RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, RNWGPlatformView) {
NSNumber *contextId = [RCTConvert NSNumber:json];
[(MetalView *)view setContextId:contextId];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/webgpu/react-native-wgpu.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Pod::Spec.new do |s|
]

s.ios.vendored_frameworks = [
'libs/ios/libwebgpu_dawn.xcframework',
'libs/apple/libwebgpu_dawn.xcframework',
]

s.visionos.vendored_frameworks = [
'libs/ios/libwebgpu_dawn_visionos.xcframework',
'libs/apple/libwebgpu_dawn_visionos.xcframework',
]

s.pod_target_xcconfig = {
Expand Down

0 comments on commit 611c8f2

Please sign in to comment.