From d19fb97ef9773c62134a5e31eb76434f826cc300 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 20 Jul 2023 00:09:53 +0200 Subject: [PATCH] Pass `SkiaRenderer` --- example/ios/Podfile.lock | 2 +- ios/CameraView.swift | 32 +++++++++---------- ios/Frame Processor/FrameProcessor.h | 3 +- ios/Frame Processor/FrameProcessor.mm | 3 +- .../FrameProcessorRuntimeManager.mm | 6 +++- ios/Skia Render Layer/SkiaFrameProcessor.h | 13 +++++++- ios/Skia Render Layer/SkiaFrameProcessor.mm | 17 ++++++---- ios/Skia Render Layer/SkiaPreviewView.swift | 4 +++ 8 files changed, 51 insertions(+), 29 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index c505d54f61..8982dff051 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -713,7 +713,7 @@ SPEC CHECKSUMS: RNStaticSafeAreaInsets: 055ddbf5e476321720457cdaeec0ff2ba40ec1b8 RNVectorIcons: fcc2f6cb32f5735b586e66d14103a74ce6ad61f8 SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17 - VisionCamera: c935274ff3da0e443b4fcad23e349715c4416991 + VisionCamera: 8d653f1257a1fdc2e04e579d07ab7a51f48eb82d Yoga: 65286bb6a07edce5e4fe8c90774da977ae8fc009 PODFILE CHECKSUM: ab9c06b18c63e741c04349c0fd630c6d3145081c diff --git a/ios/CameraView.swift b/ios/CameraView.swift index c507d77167..9ff5962310 100644 --- a/ios/CameraView.swift +++ b/ios/CameraView.swift @@ -91,16 +91,11 @@ public final class CameraView: UIView { internal var isRecording = false internal var recordingSession: RecordingSession? #if VISION_CAMERA_ENABLE_FRAME_PROCESSORS - @objc public var frameProcessor: FrameProcessor? { - didSet { - if let previewView = self.previewView as? SkiaPreviewView { - previewView.setFrameProcessor(frameProcessor) - } - } - } + @objc public var frameProcessor: FrameProcessor? + #endif + #if VISION_CAMERA_ENABLE_SKIA + private var skiaRenderer: SkiaRenderer? #endif - // CameraView+TakePhoto - internal var photoCaptureDelegates: [PhotoCaptureDelegate] = [] // CameraView+Zoom internal var pinchGestureRecognizer: UIPinchGestureRecognizer? internal var pinchScaleOffset: CGFloat = 1.0 @@ -171,10 +166,7 @@ public final class CameraView: UIView { if newSuperview != nil { if !isMounted { isMounted = true - guard let onViewReady = onViewReady else { - return - } - onViewReady(nil) + onViewReady?(nil) } } } @@ -185,6 +177,15 @@ public final class CameraView: UIView { previewView.bounds = bounds } } + + #if VISION_CAMERA_ENABLE_SKIA + @objc func getSkiaRenderer() -> SkiaRenderer { + if skiaRenderer == nil { + skiaRenderer = SkiaRenderer() + } + return skiaRenderer! + } + #endif func setupPreviewView() { if previewType == "skia" { @@ -192,10 +193,7 @@ public final class CameraView: UIView { #if VISION_CAMERA_ENABLE_SKIA if previewView is SkiaPreviewView { return } previewView?.removeFromSuperview() - previewView = SkiaPreviewView(frame: frame) - if let frameProcessor = self.frameProcessor { - (previewView as! SkiaPreviewView).setFrameProcessor(frameProcessor) - } + previewView = SkiaPreviewView(frame: frame, skiaRenderer: getSkiaRenderer()) #else invokeOnError(.system(.skiaUnavailable)) #endif diff --git a/ios/Frame Processor/FrameProcessor.h b/ios/Frame Processor/FrameProcessor.h index 55154f5a16..6f9035262d 100644 --- a/ios/Frame Processor/FrameProcessor.h +++ b/ios/Frame Processor/FrameProcessor.h @@ -19,7 +19,8 @@ @interface FrameProcessor : NSObject #ifdef __cplusplus -- (instancetype _Nonnull) initWithWorklet:(std::shared_ptr)context worklet:(std::shared_ptr)worklet; +- (instancetype _Nonnull) initWithWorklet:(std::shared_ptr)context + worklet:(std::shared_ptr)worklet; #endif - (void) call:(Frame* _Nonnull)frame; diff --git a/ios/Frame Processor/FrameProcessor.mm b/ios/Frame Processor/FrameProcessor.mm index d5e5bb7f43..0a44460799 100644 --- a/ios/Frame Processor/FrameProcessor.mm +++ b/ios/Frame Processor/FrameProcessor.mm @@ -21,7 +21,8 @@ @implementation FrameProcessor { std::shared_ptr _workletInvoker; } -- (instancetype)initWithWorklet:(std::shared_ptr)context worklet:(std::shared_ptr)worklet { +- (instancetype)initWithWorklet:(std::shared_ptr)context + worklet:(std::shared_ptr)worklet { if (self = [super init]) { _workletContext = context; _workletInvoker = std::make_shared(worklet); diff --git a/ios/Frame Processor/FrameProcessorRuntimeManager.mm b/ios/Frame Processor/FrameProcessorRuntimeManager.mm index 8189656ab0..46aa2d5004 100644 --- a/ios/Frame Processor/FrameProcessorRuntimeManager.mm +++ b/ios/Frame Processor/FrameProcessorRuntimeManager.mm @@ -39,6 +39,7 @@ @interface CameraQueues : NSObject __attribute__((objc_runtime_name("_TtC12VisionCamera10CameraView"))) @interface CameraView : UIView @property (nonatomic, copy) FrameProcessor* _Nullable frameProcessor; +- (SkiaRenderer* _Nonnull)getSkiaRenderer; @end @implementation FrameProcessorRuntimeManager { @@ -153,10 +154,13 @@ - (void) installFrameProcessorBindings { if (frameProcessorType == "frame-processor") { view.frameProcessor = [[FrameProcessor alloc] initWithWorklet:self->workletContext worklet:worklet]; + } else if (frameProcessorType == "skia-frame-processor") { #if VISION_CAMERA_ENABLE_SKIA + SkiaRenderer* skiaRenderer = [view getSkiaRenderer]; view.frameProcessor = [[SkiaFrameProcessor alloc] initWithWorklet:self->workletContext - worklet:worklet]; + worklet:worklet + skiaRenderer:skiaRenderer]; #else throw std::runtime_error("system/skia-unavailable: Skia is not installed!"); #endif diff --git a/ios/Skia Render Layer/SkiaFrameProcessor.h b/ios/Skia Render Layer/SkiaFrameProcessor.h index 813e0ec017..be00b2c404 100644 --- a/ios/Skia Render Layer/SkiaFrameProcessor.h +++ b/ios/Skia Render Layer/SkiaFrameProcessor.h @@ -10,7 +10,18 @@ #import #import "FrameProcessor.h" +#import "SkiaRenderer.h" -@interface SkiaFrameProcessor : FrameProcessor +#ifdef __cplusplus +#import "WKTJsiWorklet.h" +#endif + +@interface SkiaFrameProcessor: FrameProcessor + +#ifdef __cplusplus +- (instancetype _Nonnull) initWithWorklet:(std::shared_ptr)context + worklet:(std::shared_ptr)worklet + skiaRenderer:(SkiaRenderer* _Nonnull)skiaRenderer; +#endif @end diff --git a/ios/Skia Render Layer/SkiaFrameProcessor.mm b/ios/Skia Render Layer/SkiaFrameProcessor.mm index 844d7784dd..87fac5c5a7 100644 --- a/ios/Skia Render Layer/SkiaFrameProcessor.mm +++ b/ios/Skia Render Layer/SkiaFrameProcessor.mm @@ -10,19 +10,22 @@ #import "SkiaFrameProcessor.h" #import "SkiaRenderer.h" -@implementation SkiaFrameProcessor +@implementation SkiaFrameProcessor { + SkiaRenderer* _skiaRenderer; +} -- (instancetype)init { - if (self = [super init]) { - // TODO: init? +- (instancetype _Nonnull)initWithWorklet:(std::shared_ptr)context + worklet:(std::shared_ptr)worklet + skiaRenderer:(SkiaRenderer * _Nonnull)skiaRenderer { + if (self = [super initWithWorklet:context worklet:worklet]) { + _skiaRenderer = skiaRenderer; } return self; } - (void)call:(Frame*)frame { - // TODO: Get SkiaRenderer somehow... - SkiaRenderer* renderer = nil; - [renderer renderCameraFrameToOffscreenCanvas:frame.buffer withDrawCallback:^(SkiaCanvas _Nonnull) { + [_skiaRenderer renderCameraFrameToOffscreenCanvas:frame.buffer + withDrawCallback:^(SkiaCanvas _Nonnull) { // TODO: Pass SkiaCanvas along here... [super call:frame]; }]; diff --git a/ios/Skia Render Layer/SkiaPreviewView.swift b/ios/Skia Render Layer/SkiaPreviewView.swift index 538ad6aa79..92c954942f 100644 --- a/ios/Skia Render Layer/SkiaPreviewView.swift +++ b/ios/Skia Render Layer/SkiaPreviewView.swift @@ -51,6 +51,10 @@ class SkiaPreviewView: PreviewView { super.init(frame: frame) } + deinit { + self.displayLink.stop() + } + required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }