| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  | // | 
					
						
							|  |  |  | //  SharedArray.mm | 
					
						
							|  |  |  | //  VisionCamera | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //  Created by Marc Rousavy on 12.01.24. | 
					
						
							|  |  |  | //  Copyright © 2024 mrousavy. All rights reserved. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #import "SharedArray.h" | 
					
						
							| 
									
										
										
										
											2024-01-18 10:41:26 +01:00
										 |  |  | #import "MutableRawBuffer.h" | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  | #import <Foundation/Foundation.h> | 
					
						
							|  |  |  | #import <jsi/jsi.h> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace facebook; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @implementation SharedArray { | 
					
						
							|  |  |  |   uint8_t* _data; | 
					
						
							| 
									
										
										
										
											2024-01-17 18:30:26 +01:00
										 |  |  |   NSInteger _size; | 
					
						
							| 
									
										
										
										
											2024-01-17 20:18:46 +01:00
										 |  |  |   std::shared_ptr<jsi::ArrayBuffer> _arrayBuffer; | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-18 10:41:26 +01:00
										 |  |  | - (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 { | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  |   if (self = [super init]) { | 
					
						
							|  |  |  |     jsi::Runtime& runtime = proxy.proxy->getWorkletRuntime(); | 
					
						
							| 
									
										
										
										
											2024-01-17 20:18:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-18 10:41:26 +01:00
										 |  |  |     auto mutableBuffer = std::make_shared<vision::MutableRawBuffer>(data, size, freeOnDealloc); | 
					
						
							| 
									
										
										
										
											2024-01-17 20:18:46 +01:00
										 |  |  |     _arrayBuffer = std::make_shared<jsi::ArrayBuffer>(runtime, mutableBuffer); | 
					
						
							|  |  |  |     _data = data; | 
					
						
							| 
									
										
										
										
											2024-01-17 18:30:26 +01:00
										 |  |  |     _size = size; | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-18 10:41:26 +01:00
										 |  |  | - (instancetype)initWithRuntime:(jsi::Runtime&)runtime wrapArrayBuffer:(std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer { | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  |   if (self = [super init]) { | 
					
						
							| 
									
										
										
										
											2024-01-17 20:18:46 +01:00
										 |  |  |     _arrayBuffer = arrayBuffer; | 
					
						
							|  |  |  |     _data = _arrayBuffer->data(runtime); | 
					
						
							|  |  |  |     _size = _arrayBuffer->size(runtime); | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-17 20:18:46 +01:00
										 |  |  | - (std::shared_ptr<jsi::ArrayBuffer>)arrayBuffer { | 
					
						
							|  |  |  |   return _arrayBuffer; | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (uint8_t*)data { | 
					
						
							|  |  |  |   return _data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-17 18:30:26 +01:00
										 |  |  | - (NSInteger)size { | 
					
						
							|  |  |  |   return _size; | 
					
						
							| 
									
										
										
										
											2024-01-12 16:00:36 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @end |