feat: SharedArray:wrapData:withSize init for iOS (#2410)

* feat: `ArrayBuffer:wrapData:withSize` init for iOS

* Format

* fix build error

* Update ExampleFrameProcessorPlugin.m

* docs: Add class docs for SharedArray
This commit is contained in:
Marc Rousavy
2024-01-18 10:41:26 +01:00
committed by GitHub
parent e21a1c2110
commit 992934e00e
8 changed files with 56 additions and 21 deletions

View File

@@ -170,7 +170,7 @@ id convertJSIValueToObjCObject(jsi::Runtime& runtime, const jsi::Value& value, s
} else if (o.isArrayBuffer(runtime)) {
// ArrayBuffer
auto arrayBuffer = std::make_shared<jsi::ArrayBuffer>(o.getArrayBuffer(runtime));
return [[SharedArray alloc] initWithRuntime:runtime arrayBuffer:arrayBuffer];
return [[SharedArray alloc] initWithRuntime:runtime wrapArrayBuffer:arrayBuffer];
} else {
// object
return convertJSIObjectToNSDictionary(runtime, o, jsInvoker);

View File

@@ -12,25 +12,51 @@
#import <Foundation/Foundation.h>
#ifdef __cplusplus
#import "../../cpp/MutableRawBuffer.h"
#import <jsi/jsi.h>
using namespace facebook;
#endif
NS_ASSUME_NONNULL_BEGIN
/**
* An ArrayBuffer of type uint8 that can be shared between native and JS without copying.
*/
@interface SharedArray : NSObject
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy size:(NSInteger)size;
/**
* Allocates a new SharedArray with the given size.
* Use `data` to write to the SharedArray.
*/
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy allocateWithSize:(NSInteger)size;
/**
* Wraps the given data in a SharedArray without copying.
* Use `data` to write to the SharedArray.
*/
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy
wrapData:(uint8_t*)data
withSize:(NSInteger)size
freeOnDealloc:(BOOL)freeOnDealloc;
#ifdef __cplusplus
- (instancetype)initWithRuntime:(jsi::Runtime&)runtime arrayBuffer:(std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer;
/**
* Wraps the given JSI ArrayBuffer in a SharedArray for native access.
* Use `data` to write to the SharedArray.
*/
- (instancetype)initWithRuntime:(jsi::Runtime&)runtime wrapArrayBuffer:(std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer;
- (std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer;
#endif
/**
* The underlying contents of the ArrayBuffer which can be used for reading and writing.
*/
@property(nonatomic, readonly, nonnull) uint8_t* data;
/**
* The size of the ArrayBuffer.
*/
@property(nonatomic, readonly) NSInteger size;
@end

View File

@@ -7,7 +7,7 @@
//
#import "SharedArray.h"
#import "../../cpp/MutableRawBuffer.h"
#import "MutableRawBuffer.h"
#import <Foundation/Foundation.h>
#import <jsi/jsi.h>
@@ -19,12 +19,19 @@ using namespace facebook;
std::shared_ptr<jsi::ArrayBuffer> _arrayBuffer;
}
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy size:(NSInteger)size {
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy allocateWithSize:(NSInteger)size {
uint8_t* data = (uint8_t*)malloc(size * sizeof(uint8_t));
return [self initWithProxy:proxy wrapData:data withSize:size freeOnDealloc:YES];
}
- (instancetype)initWithProxy:(VisionCameraProxyHolder*)proxy
wrapData:(uint8_t*)data
withSize:(NSInteger)size
freeOnDealloc:(BOOL)freeOnDealloc {
if (self = [super init]) {
jsi::Runtime& runtime = proxy.proxy->getWorkletRuntime();
uint8_t* data = (uint8_t*)malloc(size * sizeof(uint8_t));
auto mutableBuffer = std::make_shared<vision::MutableRawBuffer>(data, size, [=]() { free(data); });
auto mutableBuffer = std::make_shared<vision::MutableRawBuffer>(data, size, freeOnDealloc);
_arrayBuffer = std::make_shared<jsi::ArrayBuffer>(runtime, mutableBuffer);
_data = data;
_size = size;
@@ -32,7 +39,7 @@ using namespace facebook;
return self;
}
- (instancetype)initWithRuntime:(jsi::Runtime&)runtime arrayBuffer:(std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer {
- (instancetype)initWithRuntime:(jsi::Runtime&)runtime wrapArrayBuffer:(std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer {
if (self = [super init]) {
_arrayBuffer = arrayBuffer;
_data = _arrayBuffer->data(runtime);