Skip to content

Commit

Permalink
Pass SkiaRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 19, 2023
1 parent 2f4b3ea commit d19fb97
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ SPEC CHECKSUMS:
RNStaticSafeAreaInsets: 055ddbf5e476321720457cdaeec0ff2ba40ec1b8
RNVectorIcons: fcc2f6cb32f5735b586e66d14103a74ce6ad61f8
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
VisionCamera: c935274ff3da0e443b4fcad23e349715c4416991
VisionCamera: 8d653f1257a1fdc2e04e579d07ab7a51f48eb82d
Yoga: 65286bb6a07edce5e4fe8c90774da977ae8fc009

PODFILE CHECKSUM: ab9c06b18c63e741c04349c0fd630c6d3145081c
Expand Down
32 changes: 15 additions & 17 deletions ios/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -185,17 +177,23 @@ 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" {
// Skia Preview View allows user to draw onto a Frame in a Frame Processor
#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
Expand Down
3 changes: 2 additions & 1 deletion ios/Frame Processor/FrameProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
@interface FrameProcessor : NSObject

#ifdef __cplusplus
- (instancetype _Nonnull) initWithWorklet:(std::shared_ptr<RNWorklet::JsiWorkletContext>)context worklet:(std::shared_ptr<RNWorklet::JsiWorklet>)worklet;
- (instancetype _Nonnull) initWithWorklet:(std::shared_ptr<RNWorklet::JsiWorkletContext>)context
worklet:(std::shared_ptr<RNWorklet::JsiWorklet>)worklet;
#endif

- (void) call:(Frame* _Nonnull)frame;
Expand Down
3 changes: 2 additions & 1 deletion ios/Frame Processor/FrameProcessor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ @implementation FrameProcessor {
std::shared_ptr<RNWorklet::WorkletInvoker> _workletInvoker;
}

- (instancetype)initWithWorklet:(std::shared_ptr<RNWorklet::JsiWorkletContext>)context worklet:(std::shared_ptr<RNWorklet::JsiWorklet>)worklet {
- (instancetype)initWithWorklet:(std::shared_ptr<RNWorklet::JsiWorkletContext>)context
worklet:(std::shared_ptr<RNWorklet::JsiWorklet>)worklet {
if (self = [super init]) {
_workletContext = context;
_workletInvoker = std::make_shared<RNWorklet::WorkletInvoker>(worklet);
Expand Down
6 changes: 5 additions & 1 deletion ios/Frame Processor/FrameProcessorRuntimeManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion ios/Skia Render Layer/SkiaFrameProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@

#import <Foundation/Foundation.h>
#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<RNWorklet::JsiWorkletContext>)context
worklet:(std::shared_ptr<RNWorklet::JsiWorklet>)worklet
skiaRenderer:(SkiaRenderer* _Nonnull)skiaRenderer;
#endif

@end
17 changes: 10 additions & 7 deletions ios/Skia Render Layer/SkiaFrameProcessor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<RNWorklet::JsiWorkletContext>)context
worklet:(std::shared_ptr<RNWorklet::JsiWorklet>)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];
}];
Expand Down
4 changes: 4 additions & 0 deletions ios/Skia Render Layer/SkiaPreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class SkiaPreviewView: PreviewView {
super.init(frame: frame)
}

Check failure on line 53 in ios/Skia Render Layer/SkiaPreviewView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
deinit {
self.displayLink.stop()
}

Check failure on line 57 in ios/Skia Render Layer/SkiaPreviewView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand Down

0 comments on commit d19fb97

Please sign in to comment.