-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
DrawableFrameHostObject
subclass
- Loading branch information
Showing
6 changed files
with
123 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// DrawableFrameHostObject.h | ||
// VisionCamera | ||
// | ||
// Created by Marc Rousavy on 20.07.23. | ||
// Copyright © 2023 mrousavy. All rights reserved. | ||
// | ||
|
||
#pragma once | ||
|
||
#import <jsi/jsi.h> | ||
#import "../Frame Processor/FrameHostObject.h" | ||
#import "../Frame Processor/Frame.h" | ||
#import <CoreMedia/CMSampleBuffer.h> | ||
|
||
#import "SkCanvas.h" | ||
#import "JsiSkCanvas.h" | ||
|
||
using namespace facebook; | ||
|
||
class JSI_EXPORT DrawableFrameHostObject: public FrameHostObject { | ||
public: | ||
explicit DrawableFrameHostObject(Frame* frame, | ||
std::shared_ptr<RNSkia::JsiSkCanvas> canvas): | ||
FrameHostObject(frame), _canvas(canvas) {} | ||
|
||
public: | ||
jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) override; | ||
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& rt) override; | ||
|
||
void invalidateCanvas(); | ||
|
||
private: | ||
std::shared_ptr<RNSkia::JsiSkCanvas> _canvas; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// DrawableFrameHostObject.mm | ||
// VisionCamera | ||
// | ||
// Created by Marc Rousavy on 20.07.23. | ||
// Copyright © 2023 mrousavy. All rights reserved. | ||
// | ||
|
||
#import "DrawableFrameHostObject.h" | ||
#import "SkCanvas.h" | ||
#import "SkImageHelpers.h" | ||
|
||
std::vector<jsi::PropNameID> DrawableFrameHostObject::getPropertyNames(jsi::Runtime& rt) { | ||
auto result = FrameHostObject::getPropertyNames(rt); | ||
|
||
// Skia - Render Frame | ||
result.push_back(jsi::PropNameID::forUtf8(rt, std::string("render"))); | ||
|
||
if (_canvas != nullptr) { | ||
auto canvasPropNames = _canvas->getPropertyNames(rt); | ||
for (auto& prop : canvasPropNames) { | ||
result.push_back(std::move(prop)); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
SkRect inscribe(SkSize size, SkRect rect) { | ||
auto halfWidthDelta = (rect.width() - size.width()) / 2.0; | ||
auto halfHeightDelta = (rect.height() - size.height()) / 2.0; | ||
return SkRect::MakeXYWH(rect.x() + halfWidthDelta, | ||
rect.y() + halfHeightDelta, size.width(), | ||
size.height()); | ||
} | ||
|
||
jsi::Value DrawableFrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) { | ||
auto name = propName.utf8(runtime); | ||
|
||
if (name == "render") { | ||
auto render = JSI_HOST_FUNCTION_LAMBDA { | ||
if (_canvas == nullptr) { | ||
throw jsi::JSError(runtime, "Trying to render a Frame without a Skia Canvas! Did you install Skia?"); | ||
} | ||
|
||
// convert CMSampleBuffer to SkImage | ||
auto context = _canvas->getCanvas()->recordingContext(); | ||
auto image = SkImageHelpers::convertCMSampleBufferToSkImage(context, frame.buffer); | ||
|
||
// draw SkImage | ||
if (count > 0) { | ||
// ..with paint/shader | ||
auto paintHostObject = arguments[0].asObject(runtime).asHostObject<RNSkia::JsiSkPaint>(runtime); | ||
auto paint = paintHostObject->getObject(); | ||
_canvas->getCanvas()->drawImage(image, 0, 0, SkSamplingOptions(), paint.get()); | ||
} else { | ||
// ..without paint/shader | ||
_canvas->getCanvas()->drawImage(image, 0, 0); | ||
} | ||
|
||
return jsi::Value::undefined(); | ||
}; | ||
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "render"), 1, render); | ||
} | ||
|
||
if (_canvas != nullptr) { | ||
// If we have a Canvas, try to access the property on there. | ||
auto result = _canvas->get(runtime, propName); | ||
if (!result.isUndefined()) { | ||
return result; | ||
} | ||
} | ||
|
||
// fallback to base implementation | ||
return FrameHostObject::get(runtime, propName); | ||
} | ||
|
||
void DrawableFrameHostObject::invalidateCanvas() { | ||
_canvas = nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters