2023-07-20 07:30:04 -06:00
|
|
|
//
|
|
|
|
// FrameProcessor.mm
|
|
|
|
// VisionCamera
|
|
|
|
//
|
|
|
|
// Created by Marc Rousavy on 13.07.23.
|
|
|
|
// Copyright © 2023 mrousavy. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FrameProcessor.h"
|
2023-09-01 04:58:32 -06:00
|
|
|
#import <Foundation/Foundation.h>
|
2023-07-20 07:30:04 -06:00
|
|
|
|
|
|
|
#import "FrameHostObject.h"
|
2023-09-01 04:58:32 -06:00
|
|
|
#import "WKTJsiWorklet.h"
|
|
|
|
#import <jsi/jsi.h>
|
|
|
|
#import <memory>
|
2023-07-20 07:30:04 -06:00
|
|
|
|
|
|
|
using namespace facebook;
|
|
|
|
|
|
|
|
@implementation FrameProcessor {
|
|
|
|
std::shared_ptr<RNWorklet::JsiWorkletContext> _workletContext;
|
|
|
|
std::shared_ptr<RNWorklet::WorkletInvoker> _workletInvoker;
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:52:30 -06:00
|
|
|
- (instancetype)initWithWorklet:(std::shared_ptr<RNWorklet::JsiWorklet>)worklet
|
|
|
|
context:(std::shared_ptr<RNWorklet::JsiWorkletContext>)context {
|
2023-07-20 07:30:04 -06:00
|
|
|
if (self = [super init]) {
|
|
|
|
_workletInvoker = std::make_shared<RNWorklet::WorkletInvoker>(worklet);
|
2023-07-21 09:52:30 -06:00
|
|
|
_workletContext = context;
|
2023-07-20 07:30:04 -06:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)callWithFrameHostObject:(std::shared_ptr<FrameHostObject>)frameHostObject {
|
|
|
|
// Call the Frame Processor on the Worklet Runtime
|
|
|
|
jsi::Runtime& runtime = _workletContext->getWorkletRuntime();
|
2023-09-01 04:58:32 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
try {
|
|
|
|
// Wrap HostObject as JSI Value
|
|
|
|
auto argument = jsi::Object::createFromHostObject(runtime, frameHostObject);
|
|
|
|
jsi::Value jsValue(std::move(argument));
|
2023-09-01 04:58:32 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
// Call the Worklet with the Frame JS Host Object as an argument
|
|
|
|
_workletInvoker->call(runtime, jsi::Value::undefined(), &jsValue, 1);
|
|
|
|
} catch (jsi::JSError& jsError) {
|
|
|
|
// JS Error occured, print it to console.
|
|
|
|
auto message = jsError.getMessage();
|
2023-09-01 04:58:32 -06:00
|
|
|
|
2023-07-20 07:30:04 -06:00
|
|
|
_workletContext->invokeOnJsThread([message](jsi::Runtime& jsRuntime) {
|
2023-09-01 04:58:32 -06:00
|
|
|
auto logFn = jsRuntime.global()
|
|
|
|
.getPropertyAsObject(jsRuntime, "console")
|
|
|
|
.getPropertyAsFunction(jsRuntime, "error");
|
|
|
|
logFn.call(jsRuntime, jsi::String::createFromUtf8(
|
|
|
|
jsRuntime, "Frame Processor threw an error: " + message));
|
2023-07-20 07:30:04 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)call:(Frame* _Nonnull)frame {
|
|
|
|
// Create the Frame Host Object wrapping the internal Frame
|
|
|
|
auto frameHostObject = std::make_shared<FrameHostObject>(frame);
|
|
|
|
[self callWithFrameHostObject:frameHostObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|