Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions packages/skia/cpp/api/JsiSkAnimatedImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,31 @@ class JsiSkAnimatedImage
static constexpr const char *CLASS_NAME = "AnimatedImage";

// TODO-API: Properties?
JSI_HOST_FUNCTION(getCurrentFrame) {
std::shared_ptr<JsiSkImage> getCurrentFrame() {
auto image = getObject()->getCurrentFrame();
return makeJsiObject(
runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
return std::make_shared<JsiSkImage>(getContext(), std::move(image));
}

JSI_HOST_FUNCTION(getFrameCount) {
return static_cast<int>(getObject()->getFrameCount());
}
int getFrameCount() { return static_cast<int>(getObject()->getFrameCount()); }

JSI_HOST_FUNCTION(currentFrameDuration) {
int currentFrameDuration() {
return static_cast<int>(getObject()->currentFrameDuration());
}

JSI_HOST_FUNCTION(decodeNextFrame) {
int decodeNextFrame() {
return static_cast<int>(getObject()->decodeNextFrame());
}

static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installCommon(runtime, prototype);
installHostMethod(runtime, prototype, "getFrameCount",
&JsiSkAnimatedImage::getFrameCount);
installHostMethod(runtime, prototype, "getCurrentFrame",
&JsiSkAnimatedImage::getCurrentFrame);
installHostMethod(runtime, prototype, "currentFrameDuration",
&JsiSkAnimatedImage::currentFrameDuration);
installHostMethod(runtime, prototype, "decodeNextFrame",
&JsiSkAnimatedImage::decodeNextFrame);
installMethod(runtime, prototype, "getFrameCount",
&JsiSkAnimatedImage::getFrameCount);
installMethod(runtime, prototype, "getCurrentFrame",
&JsiSkAnimatedImage::getCurrentFrame);
installMethod(runtime, prototype, "currentFrameDuration",
&JsiSkAnimatedImage::currentFrameDuration);
installMethod(runtime, prototype, "decodeNextFrame",
&JsiSkAnimatedImage::decodeNextFrame);
}

JsiSkAnimatedImage(std::shared_ptr<RNSkPlatformContext> context,
Expand Down
16 changes: 9 additions & 7 deletions packages/skia/cpp/api/JsiSkAnimatedImageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <memory>
#include <utility>
#include <variant>

#include <jsi/jsi.h>

#include "JsiSkAnimatedImage.h"
#include "JsiSkConverters.h"
#include "JsiSkData.h"
#include "JsiSkNativeObjects.h"
#include "jsi/JsiPromises.h"
Expand All @@ -19,22 +21,22 @@ class JsiSkAnimatedImageFactory
public:
static constexpr const char *CLASS_NAME = "AnimatedImageFactory";

JSI_HOST_FUNCTION(MakeAnimatedImageFromEncoded) {
auto data = JsiSkData::fromValue(runtime, arguments[0]);
std::variant<std::nullptr_t, std::shared_ptr<JsiSkAnimatedImage>>
MakeAnimatedImageFromEncoded(sk_sp<SkData> data) {
auto codec = SkAndroidCodec::MakeFromData(data);
auto image = SkAnimatedImage::Make(std::move(codec));
if (image == nullptr) {
return jsi::Value::null();
return nullptr;
}
return makeJsiObject(runtime, std::make_shared<JsiSkAnimatedImage>(
getContext(), std::move(image)));
return std::make_shared<JsiSkAnimatedImage>(getContext(),
std::move(image));
}

size_t getMemoryPressure() override { return 1024; }

static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installHostMethod(runtime, prototype, "MakeAnimatedImageFromEncoded",
&JsiSkAnimatedImageFactory::MakeAnimatedImageFromEncoded);
installMethod(runtime, prototype, "MakeAnimatedImageFromEncoded",
&JsiSkAnimatedImageFactory::MakeAnimatedImageFromEncoded);
}

explicit JsiSkAnimatedImageFactory(
Expand Down
108 changes: 45 additions & 63 deletions packages/skia/cpp/api/JsiSkColorFilterFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include "JsiSkColor.h"
#include "JsiSkColorFilter.h"
#include "JsiSkConverters.h"
#include "JsiSkNativeObjects.h"
#include <jsi/jsi.h>
#include <memory>
#include <utility>
#include <vector>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
Expand All @@ -24,87 +26,67 @@ class JsiSkColorFilterFactory
public:
static constexpr const char *CLASS_NAME = "ColorFilterFactory";

JSI_HOST_FUNCTION(MakeMatrix) {
auto jsiMatrix = arguments[0].asObject(runtime).asArray(runtime);
float matrix[20];
for (int i = 0; i < 20; i++) {
if (jsiMatrix.size(runtime) > i) {
matrix[i] = jsiMatrix.getValueAtIndex(runtime, i).asNumber();
}
std::shared_ptr<JsiSkColorFilter> MakeMatrix(std::vector<float> values) {
float matrix[20] = {0};
for (size_t i = 0; i < 20 && i < values.size(); i++) {
matrix[i] = values[i];
}
// Return the newly constructed object
return makeJsiObject(
runtime, std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::Matrix(std::move(matrix))));
return std::make_shared<JsiSkColorFilter>(getContext(),
SkColorFilters::Matrix(matrix));
}

JSI_HOST_FUNCTION(MakeBlend) {
auto color = JsiSkColor::fromValue(runtime, arguments[0]);
SkBlendMode blend = (SkBlendMode)arguments[1].asNumber();
// Return the newly constructed object
return makeJsiObject(
runtime, std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::Blend(color, blend)));
std::shared_ptr<JsiSkColorFilter> MakeBlend(JsiColor color, double blend) {
return std::make_shared<JsiSkColorFilter>(
getContext(),
SkColorFilters::Blend(color, static_cast<SkBlendMode>(blend)));
}

JSI_HOST_FUNCTION(MakeCompose) {
auto outer = JsiSkColorFilter::fromValue(runtime, arguments[0]);
auto inner = JsiSkColorFilter::fromValue(runtime, arguments[1]);
// Return the newly constructed object
return makeJsiObject(
runtime, std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::Compose(std::move(outer),
std::move(inner))));
std::shared_ptr<JsiSkColorFilter> MakeCompose(sk_sp<SkColorFilter> outer,
sk_sp<SkColorFilter> inner) {
return std::make_shared<JsiSkColorFilter>(
getContext(),
SkColorFilters::Compose(std::move(outer), std::move(inner)));
}

JSI_HOST_FUNCTION(MakeLerp) {
auto t = arguments[0].asNumber();
auto dst = JsiSkColorFilter::fromValue(runtime, arguments[1]);
auto src = JsiSkColorFilter::fromValue(runtime, arguments[2]);
// Return the newly constructed object
return makeJsiObject(
runtime, std::make_shared<JsiSkColorFilter>(
getContext(),
SkColorFilters::Lerp(t, std::move(dst), std::move(src))));
std::shared_ptr<JsiSkColorFilter> MakeLerp(double t,
sk_sp<SkColorFilter> dst,
sk_sp<SkColorFilter> src) {
return std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::Lerp(t, std::move(dst), std::move(src)));
}

JSI_HOST_FUNCTION(MakeSRGBToLinearGamma) {
// Return the newly constructed object
return makeJsiObject(
runtime, std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::SRGBToLinearGamma()));
std::shared_ptr<JsiSkColorFilter> MakeSRGBToLinearGamma() {
return std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::SRGBToLinearGamma());
}

JSI_HOST_FUNCTION(MakeLinearToSRGBGamma) {
// Return the newly constructed object
return makeJsiObject(
runtime, std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::LinearToSRGBGamma()));
std::shared_ptr<JsiSkColorFilter> MakeLinearToSRGBGamma() {
return std::make_shared<JsiSkColorFilter>(
getContext(), SkColorFilters::LinearToSRGBGamma());
}

JSI_HOST_FUNCTION(MakeLumaColorFilter) {
// Return the newly constructed object
return makeJsiObject(runtime, std::make_shared<JsiSkColorFilter>(
getContext(), SkLumaColorFilter::Make()));
std::shared_ptr<JsiSkColorFilter> MakeLumaColorFilter() {
return std::make_shared<JsiSkColorFilter>(getContext(),
SkLumaColorFilter::Make());
}

size_t getMemoryPressure() override { return 1024; }

static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installHostMethod(runtime, prototype, "MakeMatrix",
&JsiSkColorFilterFactory::MakeMatrix);
installHostMethod(runtime, prototype, "MakeBlend",
&JsiSkColorFilterFactory::MakeBlend);
installHostMethod(runtime, prototype, "MakeCompose",
&JsiSkColorFilterFactory::MakeCompose);
installHostMethod(runtime, prototype, "MakeLerp",
&JsiSkColorFilterFactory::MakeLerp);
installHostMethod(runtime, prototype, "MakeSRGBToLinearGamma",
&JsiSkColorFilterFactory::MakeSRGBToLinearGamma);
installHostMethod(runtime, prototype, "MakeLinearToSRGBGamma",
&JsiSkColorFilterFactory::MakeLinearToSRGBGamma);
installHostMethod(runtime, prototype, "MakeLumaColorFilter",
&JsiSkColorFilterFactory::MakeLumaColorFilter);
installMethod(runtime, prototype, "MakeMatrix",
&JsiSkColorFilterFactory::MakeMatrix);
installMethod(runtime, prototype, "MakeBlend",
&JsiSkColorFilterFactory::MakeBlend);
installMethod(runtime, prototype, "MakeCompose",
&JsiSkColorFilterFactory::MakeCompose);
installMethod(runtime, prototype, "MakeLerp",
&JsiSkColorFilterFactory::MakeLerp);
installMethod(runtime, prototype, "MakeSRGBToLinearGamma",
&JsiSkColorFilterFactory::MakeSRGBToLinearGamma);
installMethod(runtime, prototype, "MakeLinearToSRGBGamma",
&JsiSkColorFilterFactory::MakeLinearToSRGBGamma);
installMethod(runtime, prototype, "MakeLumaColorFilter",
&JsiSkColorFilterFactory::MakeLumaColorFilter);
}

explicit JsiSkColorFilterFactory(std::shared_ptr<RNSkPlatformContext> context)
Expand Down
48 changes: 19 additions & 29 deletions packages/skia/cpp/api/JsiSkContourMeasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <memory>
#include <utility>
#include <vector>

#include <jsi/jsi.h>

#include "JsiSkNativeObjects.h"
#include "JsiSkPoint.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
Expand All @@ -31,55 +33,43 @@ class JsiSkContourMeasure
: JsiSkWrappingSkPtrNativeObject<JsiSkContourMeasure, SkContourMeasure>(
std::move(context), std::move(contourMeasure)) {}

JSI_HOST_FUNCTION(getPosTan) {
auto dist = arguments[0].asNumber();
std::vector<std::shared_ptr<JsiSkPoint>> getPosTan(double dist) {
SkPoint position;
SkPoint tangent;
auto result = getObject()->getPosTan(dist, &position, &tangent);
if (!result) {
throw jsi::JSError(runtime, "getPosTan() failed");
throw std::runtime_error("getPosTan() failed");
}
auto posTan = jsi::Array(runtime, 2);
auto pos = makeJsiObject(
runtime, std::make_shared<JsiSkPoint>(getContext(), position));
auto tan = makeJsiObject(
runtime, std::make_shared<JsiSkPoint>(getContext(), tangent));
posTan.setValueAtIndex(runtime, 0, pos);
posTan.setValueAtIndex(runtime, 1, tan);
return posTan;
return {std::make_shared<JsiSkPoint>(getContext(), position),
std::make_shared<JsiSkPoint>(getContext(), tangent)};
}

JSI_HOST_FUNCTION(length) {
return jsi::Value(SkScalarToDouble(getObject()->length()));
}
double length() { return SkScalarToDouble(getObject()->length()); }

JSI_HOST_FUNCTION(isClosed) { return jsi::Value(getObject()->isClosed()); }
bool isClosed() { return getObject()->isClosed(); }

JSI_HOST_FUNCTION(getSegment) {
auto start = arguments[0].asNumber();
auto end = arguments[1].asNumber();
auto startWithMoveTo = arguments[2].getBool();
std::shared_ptr<JsiSkPath> getSegment(double start, double end,
bool startWithMoveTo) {
SkPathBuilder builder;
auto result =
getObject()->getSegment(start, end, &builder, startWithMoveTo);
if (!result) {
throw jsi::JSError(runtime, "getSegment() failed");
throw std::runtime_error("getSegment() failed");
}
return JsiSkPath::toValue(runtime, getContext(), builder.snapshot());
return std::make_shared<JsiSkPath>(getContext(), builder.snapshot());
}

size_t getMemoryPressure() override { return 1024; }

static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installCommon(runtime, prototype);
installHostMethod(runtime, prototype, "getPosTan",
&JsiSkContourMeasure::getPosTan);
installHostMethod(runtime, prototype, "length",
&JsiSkContourMeasure::length);
installHostMethod(runtime, prototype, "isClosed",
&JsiSkContourMeasure::isClosed);
installHostMethod(runtime, prototype, "getSegment",
&JsiSkContourMeasure::getSegment);
installMethod(runtime, prototype, "getPosTan",
&JsiSkContourMeasure::getPosTan);
installMethod(runtime, prototype, "length", &JsiSkContourMeasure::length);
installMethod(runtime, prototype, "isClosed",
&JsiSkContourMeasure::isClosed);
installMethod(runtime, prototype, "getSegment",
&JsiSkContourMeasure::getSegment);
}
};
} // namespace RNSkia
12 changes: 6 additions & 6 deletions packages/skia/cpp/api/JsiSkContourMeasureIter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>
#include <optional>
#include <utility>

#include "JsiSkContourMeasure.h"
Expand Down Expand Up @@ -33,19 +34,18 @@ class JsiSkContourMeasureIter
std::move(context), std::make_shared<SkContourMeasureIter>(
path, forceClosed, resScale)) {}

JSI_HOST_FUNCTION(next) {
std::optional<std::shared_ptr<JsiSkContourMeasure>> next() {
auto next = getObject()->next();
if (next == nullptr) {
return jsi::Value::undefined();
return std::nullopt;
}
return makeJsiObject(runtime, std::make_shared<JsiSkContourMeasure>(
getContext(), std::move(next)));
return std::make_shared<JsiSkContourMeasure>(getContext(),
std::move(next));
}

static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installCommon(runtime, prototype);
installHostMethod(runtime, prototype, "next",
&JsiSkContourMeasureIter::next);
installMethod(runtime, prototype, "next", &JsiSkContourMeasureIter::next);
}

size_t getMemoryPressure() override {
Expand Down
Loading
Loading