diff --git a/apps/docs/docs/animations/reanimated3.md b/apps/docs/docs/animations/reanimated3.md
index d26f142bfb..5d2d7f6d91 100644
--- a/apps/docs/docs/animations/reanimated3.md
+++ b/apps/docs/docs/animations/reanimated3.md
@@ -5,7 +5,8 @@ sidebar_label: Animations
slug: /animations/animations
---
-React Native Skia offers integration with [Reanimated v3 and above](https://docs.swmansion.com/react-native-reanimated/), enabling the execution of animations on the UI thread.
+React Native Skia offers integration with [Reanimated](https://docs.swmansion.com/react-native-reanimated/), enabling the execution of animations on the UI thread.
+Starting version `2.10` and above, this integration requires **Reanimated v4 or above**. For lower version numbers, you can use Reanimated v3.
React Native Skia supports the direct usage of Reanimated's shared and derived values as properties. There is no need for functions like `createAnimatedComponent` or `useAnimatedProps`; simply pass the Reanimated values directly as properties.
diff --git a/apps/docs/docs/getting-started/installation.md b/apps/docs/docs/getting-started/installation.md
index 3920306319..75a3f81aa1 100644
--- a/apps/docs/docs/getting-started/installation.md
+++ b/apps/docs/docs/getting-started/installation.md
@@ -11,7 +11,8 @@ Skia serves as the graphics engine for Google Chrome and Chrome OS, Android, Flu
**Version compatibility:**
`react-native@>=0.79` and `react@>=19` are required.
In addition you should make sure you're on at least `iOS 14` and `Android API level 21` or above.
-To use React Native Skia with video support, `Android API level 26` or above is required.
+To use React Native Skia with video support, `Android API level 26` or above is required.
+To use React Native Skia with Reanimated on native platforms, `react-native-reanimated@>=4.0.0` (with `react-native-worklets@>=0.7.0`) is required.
For `react-native@<=0.78` and `react@<=18`, you need to use `@shopify/react-native-skia` version `1.12.4` or below.
diff --git a/apps/example/ios/Podfile.lock b/apps/example/ios/Podfile.lock
index 91b6a07c4e..79b8fb4a8f 100644
--- a/apps/example/ios/Podfile.lock
+++ b/apps/example/ios/Podfile.lock
@@ -3207,7 +3207,7 @@ SPEC CHECKSUMS:
FBLazyVector: 309703e71d3f2f1ed7dc7889d58309c9d77a95a4
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
glog: 5683914934d5b6e4240e497e0f4a3b42d1854183
- hermes-engine: 11b010917f5f15150b2c015abddef1573d2bb05d
+ hermes-engine: ad676c360175e5b8af471b8ce1389e6cf4f9e1ee
RCT-Folly: 59ec0ac1f2f39672a0c6e6cecdd39383b764646f
RCTDeprecation: a41bbdd9af30bf2e5715796b313e44ec43eefff1
RCTRequired: 7be34aabb0b77c3cefe644528df0fa0afad4e4d0
@@ -3245,7 +3245,7 @@ SPEC CHECKSUMS:
React-Mapbuffer: 2e0e7cc5b7064eaed9c8b8afc3a87621cb7ef5cd
React-microtasksnativemodule: dd4d33b251b57e5027c572c6d0b45cbfbcfaa386
react-native-safe-area-context: 54d812805f3c4e08a4580ad086cbde1d8780c2e4
- react-native-skia: b28747dc1cf8c344ad041f14a618eda7f429bfeb
+ react-native-skia: 4a90dfa21f4544d69c6be0c49958378169a88532
React-NativeModulesApple: 7f2f2fed3f6c858889eb61d09941be965d52df58
React-networking: 43e5e6773ac2ca2a93261a1388fed269c9fce092
React-oscompat: 80166b66da22e7af7fad94474e9997bd52d4c8c6
diff --git a/externals/depot_tools b/externals/depot_tools
index f488573369..c46c2e9057 160000
--- a/externals/depot_tools
+++ b/externals/depot_tools
@@ -1 +1 @@
-Subproject commit f488573369dfbbdd523177b93a369d4096d06e5d
+Subproject commit c46c2e905741fa94ce6870f9c95978117b8a7917
diff --git a/packages/skia/android/CMakeLists.txt b/packages/skia/android/CMakeLists.txt
index 61b71abadc..1341c12ad8 100644
--- a/packages/skia/android/CMakeLists.txt
+++ b/packages/skia/android/CMakeLists.txt
@@ -99,10 +99,10 @@ add_library(
"${PROJECT_SOURCE_DIR}/cpp/rnskia-android/AHardwareBufferUtils.cpp"
"${PROJECT_SOURCE_DIR}/cpp/rnskia-android/RNSkAndroidVideo.cpp"
- "${PROJECT_SOURCE_DIR}/../cpp/jsi/JsiHostObject.cpp"
"${PROJECT_SOURCE_DIR}/../cpp/jsi/RuntimeLifecycleMonitor.cpp"
"${PROJECT_SOURCE_DIR}/../cpp/jsi/RuntimeAwareCache.cpp"
"${PROJECT_SOURCE_DIR}/../cpp/jsi/JsiPromises.cpp"
+ "${PROJECT_SOURCE_DIR}/../cpp/jsi/Promise.cpp"
"${PROJECT_SOURCE_DIR}/../cpp/rnskia/RNSkManager.cpp"
@@ -115,10 +115,7 @@ add_library(
)
if(SK_GRAPHITE)
- # WebGPU JSI infrastructure (jsi2)
target_sources(${PACKAGE_NAME} PRIVATE
- "${PROJECT_SOURCE_DIR}/../cpp/jsi2/Promise.cpp"
-
# WebGPU async system
"${PROJECT_SOURCE_DIR}/../cpp/rnwgpu/async/AsyncTaskHandle.cpp"
"${PROJECT_SOURCE_DIR}/../cpp/rnwgpu/async/RuntimeContext.cpp"
@@ -181,7 +178,6 @@ target_include_directories(
# WebGPU bindings
../cpp
- ../cpp/jsi2
../cpp/rnwgpu
../cpp/rnwgpu/api
../cpp/rnwgpu/api/descriptors
diff --git a/packages/skia/cpp/api/JsiNativeBuffer.h b/packages/skia/cpp/api/JsiNativeBuffer.h
index 480e8baca9..109ecaaae9 100644
--- a/packages/skia/cpp/api/JsiNativeBuffer.h
+++ b/packages/skia/cpp/api/JsiNativeBuffer.h
@@ -6,6 +6,7 @@
#include
#include "JsiSkImage.h"
+#include "JsiSkNativeObjects.h"
namespace RNSkia {
@@ -15,8 +16,11 @@ namespace jsi = facebook::jsi;
Implementation of the ParagraphBuilderFactory for making ParagraphBuilder JSI
object
*/
-class JsiNativeBufferFactory : public JsiSkHostObject {
+class JsiNativeBufferFactory
+ : public JsiSkNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "NativeBufferFactory";
+
JSI_HOST_FUNCTION(MakeFromImage) {
auto image = JsiSkImage::fromValue(runtime, arguments[0]);
image->makeNonTextureImage();
@@ -40,18 +44,19 @@ class JsiNativeBufferFactory : public JsiSkHostObject {
return jsi::Value::undefined();
}
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiNativeBufferFactory, Release),
- JSI_EXPORT_FUNC(JsiNativeBufferFactory, MakeFromImage),
- JSI_EXPORT_FUNC(JsiNativeBufferFactory, MakeTestBuffer))
-
- size_t getMemoryPressure() const override { return 1024; }
-
- std::string getObjectType() const override {
- return "JsiNativeBufferFactory";
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installHostMethod(runtime, prototype, "Release",
+ &JsiNativeBufferFactory::Release);
+ installHostMethod(runtime, prototype, "MakeFromImage",
+ &JsiNativeBufferFactory::MakeFromImage);
+ installHostMethod(runtime, prototype, "MakeTestBuffer",
+ &JsiNativeBufferFactory::MakeTestBuffer);
}
+ size_t getMemoryPressure() override { return 1024; }
+
explicit JsiNativeBufferFactory(std::shared_ptr context)
- : JsiSkHostObject(std::move(context)) {}
+ : JsiSkNativeObject(std::move(context)) {}
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkAnimatedImage.h b/packages/skia/cpp/api/JsiSkAnimatedImage.h
index a7e47214e8..eb181ccd3b 100644
--- a/packages/skia/cpp/api/JsiSkAnimatedImage.h
+++ b/packages/skia/cpp/api/JsiSkAnimatedImage.h
@@ -7,7 +7,7 @@
#include
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#include "api/third_party/base64.h"
#pragma clang diagnostic push
@@ -29,15 +29,16 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
class JsiSkAnimatedImage
- : public JsiSkWrappingSkPtrHostObject {
+ : public JsiSkWrappingSkPtrNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "AnimatedImage";
+
// TODO-API: Properties?
JSI_HOST_FUNCTION(getCurrentFrame) {
auto image = getObject()->getCurrentFrame();
- auto hostObjectInstance =
- std::make_shared(getContext(), std::move(image));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(getContext(), std::move(image)));
}
JSI_HOST_FUNCTION(getFrameCount) {
@@ -52,21 +53,24 @@ class JsiSkAnimatedImage
return static_cast(getObject()->decodeNextFrame());
}
- EXPORT_JSI_API_TYPENAME(JsiSkAnimatedImage, AnimatedImage)
-
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImage, dispose),
- JSI_EXPORT_FUNC(JsiSkAnimatedImage, getFrameCount),
- JSI_EXPORT_FUNC(JsiSkAnimatedImage, getCurrentFrame),
- JSI_EXPORT_FUNC(JsiSkAnimatedImage,
- currentFrameDuration),
- JSI_EXPORT_FUNC(JsiSkAnimatedImage, 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);
+ }
JsiSkAnimatedImage(std::shared_ptr context,
const sk_sp image)
- : JsiSkWrappingSkPtrHostObject(std::move(context),
- std::move(image)) {}
+ : JsiSkWrappingSkPtrNativeObject(
+ std::move(context), std::move(image)) {}
- size_t getMemoryPressure() const override {
+ size_t getMemoryPressure() override {
auto animation = getObject();
if (!animation) {
return 0;
@@ -131,8 +135,6 @@ class JsiSkAnimatedImage
return estimated;
}
-
- std::string getObjectType() const override { return "JsiSkAnimatedImage"; }
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkAnimatedImageFactory.h b/packages/skia/cpp/api/JsiSkAnimatedImageFactory.h
index 20222c3311..d6ecff500b 100644
--- a/packages/skia/cpp/api/JsiSkAnimatedImageFactory.h
+++ b/packages/skia/cpp/api/JsiSkAnimatedImageFactory.h
@@ -5,17 +5,20 @@
#include
-#include "jsi/JsiPromises.h"
#include "JsiSkAnimatedImage.h"
#include "JsiSkData.h"
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
+#include "jsi/JsiPromises.h"
namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkAnimatedImageFactory : public JsiSkHostObject {
+class JsiSkAnimatedImageFactory
+ : public JsiSkNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "AnimatedImageFactory";
+
JSI_HOST_FUNCTION(MakeAnimatedImageFromEncoded) {
auto data = JsiSkData::fromValue(runtime, arguments[0]);
auto codec = SkAndroidCodec::MakeFromData(data);
@@ -23,24 +26,20 @@ class JsiSkAnimatedImageFactory : public JsiSkHostObject {
if (image == nullptr) {
return jsi::Value::null();
}
- auto hostObjectInstance =
- std::make_shared(getContext(), std::move(image));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, getContext());
+ return makeJsiObject(runtime, std::make_shared(
+ getContext(), std::move(image)));
}
- size_t getMemoryPressure() const override { return 1024; }
+ size_t getMemoryPressure() override { return 1024; }
- std::string getObjectType() const override {
- return "JsiSkAnimatedImageFactory";
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installHostMethod(runtime, prototype, "MakeAnimatedImageFromEncoded",
+ &JsiSkAnimatedImageFactory::MakeAnimatedImageFromEncoded);
}
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImageFactory,
- MakeAnimatedImageFromEncoded))
-
explicit JsiSkAnimatedImageFactory(
std::shared_ptr context)
- : JsiSkHostObject(std::move(context)) {}
+ : JsiSkNativeObject(std::move(context)) {}
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkApi.h b/packages/skia/cpp/api/JsiSkApi.h
index 2962b61b2a..b71ef9bab4 100644
--- a/packages/skia/cpp/api/JsiSkApi.h
+++ b/packages/skia/cpp/api/JsiSkApi.h
@@ -4,7 +4,7 @@
#include "rnskia/RNSkPlatformContext.h"
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#ifdef SK_GRAPHITE
#include "rnskia/RNDawnContext.h"
@@ -67,11 +67,11 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkApi : public JsiSkHostObject {
+class JsiSkApi : public JsiSkNativeObject {
public:
- size_t getMemoryPressure() const override { return 8192; }
+ static constexpr const char *CLASS_NAME = "Api";
- std::string getObjectType() const override { return "JsiSkApi"; }
+ size_t getMemoryPressure() override { return 8192; }
/**
* Constructs the Skia Api object that can be installed into a runtime
@@ -79,104 +79,258 @@ class JsiSkApi : public JsiSkHostObject {
* @param context Platform context
*/
explicit JsiSkApi(const std::shared_ptr &context)
- : JsiSkHostObject(context) {
+ : JsiSkNativeObject(context),
+ _svgFactory(std::make_shared(context)),
+ _imageFactory(std::make_shared(context)),
+ _animatedImageFactory(
+ std::make_shared(context)),
+ _typefaceFactory(std::make_shared(context)),
+ _dataFactory(std::make_shared(context)),
+ _imageFilterFactory(std::make_shared(context)),
+ _pathEffectFactory(std::make_shared(context)),
+ _pathFactory(std::make_shared(context)),
+ _pathBuilderFactory(std::make_shared(context)),
+ _colorFilterFactory(std::make_shared(context)),
+ _maskFilterFactory(std::make_shared(context)),
+ _runtimeEffectFactory(
+ std::make_shared(context)),
+ _shaderFactory(std::make_shared(context)),
+ _textBlobFactory(std::make_shared(context)),
+ _surfaceFactory(std::make_shared(context)),
+ _pictureFactory(std::make_shared(context)),
+ _fontMgrFactory(std::make_shared(context)),
+ _skottieFactory(std::make_shared(context)),
+ _typefaceFontProviderFactory(
+ std::make_shared(context)),
+ _paragraphBuilderFactory(
+ std::make_shared(context)),
+ _nativeBufferFactory(
+ std::make_shared(context)) {
// We create the system font manager eagerly since it has proven to be too
// slow to do it on demand
JsiSkFontMgrFactory::getFontMgr(getContext());
- installFunction("Video", JsiVideo::createCtor(context));
- installFunction("Context", JsiSkiaContext::createCtor(context));
- installFunction("Font", JsiSkFont::createCtor(context));
- installFunction("Paint", JsiSkPaint::createCtor(context));
- installFunction("RSXform", JsiSkRSXform::createCtor(context));
- installFunction("RSXformFromRadians",
- JsiSkRSXform::createCtorFromRadians(context));
- installFunction("Matrix", JsiSkMatrix::createCtor(context));
- installFunction("XYWHRect", JsiSkRect::createCtor(context));
- installFunction("RRectXY", JsiSkRRect::createCtor(context));
- installFunction("Point", JsiSkPoint::createCtor(context));
- installFunction("RuntimeShaderBuilder",
- JsiSkRuntimeShaderBuilder::createCtor(context));
- installFunction("ContourMeasureIter",
- JsiSkContourMeasureIter::createCtor(context));
- installFunction("MakeVertices", JsiSkVertices::createCtor(context));
- installFunction("PictureRecorder",
- JsiSkPictureRecorder::createCtor(context));
- installFunction("Color", JsiSkColor::createCtor());
-
- installReadonlyProperty("SVG", std::make_shared(context));
- installReadonlyProperty("Image",
- std::make_shared(context));
- installReadonlyProperty(
- "AnimatedImage", std::make_shared(context));
- installReadonlyProperty("Typeface",
- std::make_shared(context));
- installReadonlyProperty("Data",
- std::make_shared(context));
- installReadonlyProperty("ImageFilter",
- std::make_shared(context));
- installReadonlyProperty("PathEffect",
- std::make_shared(context));
- installReadonlyProperty("Path",
- std::make_shared(context));
- installReadonlyProperty("PathBuilder",
- std::make_shared(context));
- installReadonlyProperty("ColorFilter",
- std::make_shared(context));
- installReadonlyProperty("MaskFilter",
- std::make_shared(context));
- installReadonlyProperty(
- "RuntimeEffect", std::make_shared(context));
- installReadonlyProperty("Shader",
- std::make_shared(context));
- installReadonlyProperty("TextBlob",
- std::make_shared(context));
- installReadonlyProperty("Surface",
- std::make_shared(context));
- installReadonlyProperty("Picture",
- std::make_shared(context));
- installReadonlyProperty("FontMgr",
- std::make_shared(context));
- installReadonlyProperty("Skottie",
- std::make_shared(context));
- installReadonlyProperty(
- "TypefaceFontProvider",
- std::make_shared(context));
-
- installReadonlyProperty(
- "ParagraphBuilder",
- std::make_shared(context));
-
- installReadonlyProperty("NativeBuffer",
- std::make_shared(context));
-
- installFunction("Recorder", JsiRecorder::createCtor(context));
-
- installFunction(
- "hasDevice", JSI_HOST_FUNCTION_LAMBDA {
+ }
+
+ // Constructor functions: each delegates to the class's createCtor host
+ // function using the platform context resolved from native state, so the
+ // prototype stays context-free and can be installed on any runtime.
+ JSI_HOST_FUNCTION(Video) {
+ return JsiVideo::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(Context) {
+ return JsiSkiaContext::createCtor(getContext())(runtime, thisValue,
+ arguments, count);
+ }
+ JSI_HOST_FUNCTION(Font) {
+ return JsiSkFont::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(Paint) {
+ return JsiSkPaint::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(RSXform) {
+ return JsiSkRSXform::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(RSXformFromRadians) {
+ return JsiSkRSXform::createCtorFromRadians(getContext())(runtime, thisValue,
+ arguments, count);
+ }
+ JSI_HOST_FUNCTION(Matrix) {
+ return JsiSkMatrix::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(XYWHRect) {
+ return JsiSkRect::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(RRectXY) {
+ return JsiSkRRect::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(Point) {
+ return JsiSkPoint::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+ JSI_HOST_FUNCTION(RuntimeShaderBuilder) {
+ return JsiSkRuntimeShaderBuilder::createCtor(getContext())(
+ runtime, thisValue, arguments, count);
+ }
+ JSI_HOST_FUNCTION(ContourMeasureIter) {
+ return JsiSkContourMeasureIter::createCtor(getContext())(runtime, thisValue,
+ arguments, count);
+ }
+ JSI_HOST_FUNCTION(MakeVertices) {
+ return JsiSkVertices::createCtor(getContext())(runtime, thisValue,
+ arguments, count);
+ }
+ JSI_HOST_FUNCTION(PictureRecorder) {
+ return JsiSkPictureRecorder::createCtor(getContext())(runtime, thisValue,
+ arguments, count);
+ }
+ JSI_HOST_FUNCTION(Color) {
+ return JsiSkColor::createCtor()(runtime, thisValue, arguments, count);
+ }
+ JSI_HOST_FUNCTION(Recorder) {
+ return JsiRecorder::createCtor(getContext())(runtime, thisValue, arguments,
+ count);
+ }
+
+ JSI_HOST_FUNCTION(hasDevice) {
#ifdef SK_GRAPHITE
- return jsi::Value(true);
+ return jsi::Value(true);
#else
- return jsi::Value(false);
+ return jsi::Value(false);
#endif
- });
+ }
- installFunction(
- "getDevice", JSI_HOST_FUNCTION_LAMBDA {
+ JSI_HOST_FUNCTION(getDevice) {
#ifdef SK_GRAPHITE
- auto &dawnContext = DawnContext::getInstance();
- // Per-runtime context: async ops on this device resolve on the calling
- // runtime's own thread (via its ProcessEvents pump).
- auto context = rnwgpu::async::RuntimeContext::getOrCreate(
- runtime, dawnContext.getWGPUInstance());
- auto device = std::make_shared(
- dawnContext.getWGPUDevice(), context, "Skia Device");
- return rnwgpu::GPUDevice::create(runtime, device);
+ auto &dawnContext = DawnContext::getInstance();
+ // Per-runtime context: async ops on this device resolve on the calling
+ // runtime's own thread (via its ProcessEvents pump).
+ auto context = rnwgpu::async::RuntimeContext::getOrCreate(
+ runtime, dawnContext.getWGPUInstance());
+ auto device = std::make_shared(
+ dawnContext.getWGPUDevice(), context, "Skia Device");
+ return rnwgpu::GPUDevice::create(runtime, device);
#else
- throw jsi::JSError(runtime,
- "getDevice() is only available with the Graphite "
- "backend. Rebuild with SK_GRAPHITE enabled.");
+ throw jsi::JSError(runtime,
+ "getDevice() is only available with the Graphite "
+ "backend. Rebuild with SK_GRAPHITE enabled.");
#endif
- });
}
+
+ // Factory properties: like the legacy HostObject implementation, each
+ // property access returns a fresh JS wrapper around the shared factory
+ // instance.
+ JSI_PROPERTY_GET(SVG) { return makeJsiObject(runtime, _svgFactory); }
+ JSI_PROPERTY_GET(Image) { return makeJsiObject(runtime, _imageFactory); }
+ JSI_PROPERTY_GET(AnimatedImage) {
+ return makeJsiObject(runtime, _animatedImageFactory);
+ }
+ JSI_PROPERTY_GET(Typeface) {
+ return makeJsiObject(runtime, _typefaceFactory);
+ }
+ JSI_PROPERTY_GET(Data) { return makeJsiObject(runtime, _dataFactory); }
+ JSI_PROPERTY_GET(ImageFilter) {
+ return makeJsiObject(runtime, _imageFilterFactory);
+ }
+ JSI_PROPERTY_GET(PathEffect) {
+ return makeJsiObject(runtime, _pathEffectFactory);
+ }
+ JSI_PROPERTY_GET(Path) { return makeJsiObject(runtime, _pathFactory); }
+ JSI_PROPERTY_GET(PathBuilder) {
+ return makeJsiObject(runtime, _pathBuilderFactory);
+ }
+ JSI_PROPERTY_GET(ColorFilter) {
+ return makeJsiObject(runtime, _colorFilterFactory);
+ }
+ JSI_PROPERTY_GET(MaskFilter) {
+ return makeJsiObject(runtime, _maskFilterFactory);
+ }
+ JSI_PROPERTY_GET(RuntimeEffect) {
+ return makeJsiObject(runtime, _runtimeEffectFactory);
+ }
+ JSI_PROPERTY_GET(Shader) { return makeJsiObject(runtime, _shaderFactory); }
+ JSI_PROPERTY_GET(TextBlob) {
+ return makeJsiObject(runtime, _textBlobFactory);
+ }
+ JSI_PROPERTY_GET(Surface) { return makeJsiObject(runtime, _surfaceFactory); }
+ JSI_PROPERTY_GET(Picture) { return makeJsiObject(runtime, _pictureFactory); }
+ JSI_PROPERTY_GET(FontMgr) { return makeJsiObject(runtime, _fontMgrFactory); }
+ JSI_PROPERTY_GET(Skottie) { return makeJsiObject(runtime, _skottieFactory); }
+ JSI_PROPERTY_GET(TypefaceFontProvider) {
+ return makeJsiObject(runtime, _typefaceFontProviderFactory);
+ }
+ JSI_PROPERTY_GET(ParagraphBuilder) {
+ return makeJsiObject(runtime, _paragraphBuilderFactory);
+ }
+ JSI_PROPERTY_GET(NativeBuffer) {
+ return makeJsiObject(runtime, _nativeBufferFactory);
+ }
+
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installHostMethod(runtime, prototype, "Video", &JsiSkApi::Video);
+ installHostMethod(runtime, prototype, "Context", &JsiSkApi::Context);
+ installHostMethod(runtime, prototype, "Font", &JsiSkApi::Font);
+ installHostMethod(runtime, prototype, "Paint", &JsiSkApi::Paint);
+ installHostMethod(runtime, prototype, "RSXform", &JsiSkApi::RSXform);
+ installHostMethod(runtime, prototype, "RSXformFromRadians",
+ &JsiSkApi::RSXformFromRadians);
+ installHostMethod(runtime, prototype, "Matrix", &JsiSkApi::Matrix);
+ installHostMethod(runtime, prototype, "XYWHRect", &JsiSkApi::XYWHRect);
+ installHostMethod(runtime, prototype, "RRectXY", &JsiSkApi::RRectXY);
+ installHostMethod(runtime, prototype, "Point", &JsiSkApi::Point);
+ installHostMethod(runtime, prototype, "RuntimeShaderBuilder",
+ &JsiSkApi::RuntimeShaderBuilder);
+ installHostMethod(runtime, prototype, "ContourMeasureIter",
+ &JsiSkApi::ContourMeasureIter);
+ installHostMethod(runtime, prototype, "MakeVertices",
+ &JsiSkApi::MakeVertices);
+ installHostMethod(runtime, prototype, "PictureRecorder",
+ &JsiSkApi::PictureRecorder);
+ installHostMethod(runtime, prototype, "Color", &JsiSkApi::Color);
+ installHostMethod(runtime, prototype, "Recorder", &JsiSkApi::Recorder);
+ installHostMethod(runtime, prototype, "hasDevice", &JsiSkApi::hasDevice);
+ installHostMethod(runtime, prototype, "getDevice", &JsiSkApi::getDevice);
+ installHostGetter(runtime, prototype, "SVG", &JsiSkApi::get_SVG);
+ installHostGetter(runtime, prototype, "Image", &JsiSkApi::get_Image);
+ installHostGetter(runtime, prototype, "AnimatedImage",
+ &JsiSkApi::get_AnimatedImage);
+ installHostGetter(runtime, prototype, "Typeface", &JsiSkApi::get_Typeface);
+ installHostGetter(runtime, prototype, "Data", &JsiSkApi::get_Data);
+ installHostGetter(runtime, prototype, "ImageFilter",
+ &JsiSkApi::get_ImageFilter);
+ installHostGetter(runtime, prototype, "PathEffect",
+ &JsiSkApi::get_PathEffect);
+ installHostGetter(runtime, prototype, "Path", &JsiSkApi::get_Path);
+ installHostGetter(runtime, prototype, "PathBuilder",
+ &JsiSkApi::get_PathBuilder);
+ installHostGetter(runtime, prototype, "ColorFilter",
+ &JsiSkApi::get_ColorFilter);
+ installHostGetter(runtime, prototype, "MaskFilter",
+ &JsiSkApi::get_MaskFilter);
+ installHostGetter(runtime, prototype, "RuntimeEffect",
+ &JsiSkApi::get_RuntimeEffect);
+ installHostGetter(runtime, prototype, "Shader", &JsiSkApi::get_Shader);
+ installHostGetter(runtime, prototype, "TextBlob", &JsiSkApi::get_TextBlob);
+ installHostGetter(runtime, prototype, "Surface", &JsiSkApi::get_Surface);
+ installHostGetter(runtime, prototype, "Picture", &JsiSkApi::get_Picture);
+ installHostGetter(runtime, prototype, "FontMgr", &JsiSkApi::get_FontMgr);
+ installHostGetter(runtime, prototype, "Skottie", &JsiSkApi::get_Skottie);
+ installHostGetter(runtime, prototype, "TypefaceFontProvider",
+ &JsiSkApi::get_TypefaceFontProvider);
+ installHostGetter(runtime, prototype, "ParagraphBuilder",
+ &JsiSkApi::get_ParagraphBuilder);
+ installHostGetter(runtime, prototype, "NativeBuffer",
+ &JsiSkApi::get_NativeBuffer);
+ }
+
+private:
+ std::shared_ptr _svgFactory;
+ std::shared_ptr _imageFactory;
+ std::shared_ptr _animatedImageFactory;
+ std::shared_ptr _typefaceFactory;
+ std::shared_ptr _dataFactory;
+ std::shared_ptr _imageFilterFactory;
+ std::shared_ptr _pathEffectFactory;
+ std::shared_ptr _pathFactory;
+ std::shared_ptr _pathBuilderFactory;
+ std::shared_ptr _colorFilterFactory;
+ std::shared_ptr _maskFilterFactory;
+ std::shared_ptr _runtimeEffectFactory;
+ std::shared_ptr _shaderFactory;
+ std::shared_ptr _textBlobFactory;
+ std::shared_ptr _surfaceFactory;
+ std::shared_ptr _pictureFactory;
+ std::shared_ptr _fontMgrFactory;
+ std::shared_ptr _skottieFactory;
+ std::shared_ptr
+ _typefaceFontProviderFactory;
+ std::shared_ptr _paragraphBuilderFactory;
+ std::shared_ptr _nativeBufferFactory;
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkCanvas.h b/packages/skia/cpp/api/JsiSkCanvas.h
index 7cfa9ea2f8..3376463d15 100644
--- a/packages/skia/cpp/api/JsiSkCanvas.h
+++ b/packages/skia/cpp/api/JsiSkCanvas.h
@@ -5,10 +5,10 @@
#include
#include "JsiSkFont.h"
-#include "JsiSkHostObjects.h"
#include "JsiSkImage.h"
#include "JsiSkImageInfo.h"
#include "JsiSkMatrix.h"
+#include "JsiSkNativeObjects.h"
#include "JsiSkPaint.h"
#include "JsiSkPath.h"
#include "JsiSkPicture.h"
@@ -49,8 +49,10 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkCanvas : public JsiSkHostObject {
+class JsiSkCanvas : public JsiSkNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "Canvas";
+
JSI_HOST_FUNCTION(drawPaint) {
auto paint = JsiSkPaint::fromValue(runtime, arguments[0]);
_canvas->drawPaint(*paint);
@@ -246,10 +248,8 @@ class JsiSkCanvas : public JsiSkHostObject {
}
JSI_HOST_FUNCTION(getTotalMatrix) {
- auto matrix =
- std::make_shared(getContext(), _canvas->getTotalMatrix());
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, matrix,
- getContext());
+ return makeJsiObject(runtime, std::make_shared(
+ getContext(), _canvas->getTotalMatrix()));
}
JSI_HOST_FUNCTION(drawPoints) {
@@ -651,9 +651,10 @@ class JsiSkCanvas : public JsiSkHostObject {
#if defined(SK_GRAPHITE)
// Graphite records draws lazily and offers no synchronous GPU readback. If
// this canvas belongs to a surface, snap & submit its recording, snapshot
- // it to a CPU raster image and read from that (mirroring makeImageSnapshot).
- // A canvas without an owning surface (e.g. a picture-recording canvas) has
- // no texture to read back, so fall through to the raster canvas read below.
+ // it to a CPU raster image and read from that (mirroring
+ // makeImageSnapshot). A canvas without an owning surface (e.g. a
+ // picture-recording canvas) has no texture to read back, so fall through to
+ // the raster canvas read below.
if (_surface) {
// Snapshot first: makeImageSnapshot records a copy task into the recorder
// that must be submitted before the texture can be read back (this is the
@@ -663,8 +664,8 @@ class JsiSkCanvas : public JsiSkHostObject {
DawnContext::getInstance().submitRecording(recorder->snap().get());
}
auto raster = DawnContext::getInstance().MakeRasterImage(snapshot);
- if (!raster ||
- !raster->readPixels(nullptr, *info, bfrPtr, bytesPerRow, srcX, srcY)) {
+ if (!raster || !raster->readPixels(nullptr, *info, bfrPtr, bytesPerRow,
+ srcX, srcY)) {
return jsi::Value::null();
}
return dest;
@@ -677,55 +678,72 @@ class JsiSkCanvas : public JsiSkHostObject {
return dest;
}
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkCanvas, drawPaint),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawLine),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawRect),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImage),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImageRect),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImageCubic),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImageOptions),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImageNine),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImageRectCubic),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawImageRectOptions),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawCircle),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawArc),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawRRect),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawDRRect),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawOval),
- JSI_EXPORT_FUNC(JsiSkCanvas, restoreToCount),
- JSI_EXPORT_FUNC(JsiSkCanvas, getSaveCount),
- JSI_EXPORT_FUNC(JsiSkCanvas, getTotalMatrix),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawPoints),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawPatch),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawPath),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawVertices),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawText),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawTextBlob),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawGlyphs),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawSvg),
- JSI_EXPORT_FUNC(JsiSkCanvas, clipPath),
- JSI_EXPORT_FUNC(JsiSkCanvas, clipRect),
- JSI_EXPORT_FUNC(JsiSkCanvas, clipRRect),
- JSI_EXPORT_FUNC(JsiSkCanvas, save),
- JSI_EXPORT_FUNC(JsiSkCanvas, saveLayer),
- JSI_EXPORT_FUNC(JsiSkCanvas, restore),
- JSI_EXPORT_FUNC(JsiSkCanvas, rotate),
- JSI_EXPORT_FUNC(JsiSkCanvas, translate),
- JSI_EXPORT_FUNC(JsiSkCanvas, scale),
- JSI_EXPORT_FUNC(JsiSkCanvas, skew),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawColor),
- JSI_EXPORT_FUNC(JsiSkCanvas, clear),
- JSI_EXPORT_FUNC(JsiSkCanvas, concat),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawPicture),
- JSI_EXPORT_FUNC(JsiSkCanvas, drawAtlas),
- JSI_EXPORT_FUNC(JsiSkCanvas, readPixels))
-
- size_t getMemoryPressure() const override { return 1024; }
-
- std::string getObjectType() const override { return "JsiSkCanvas"; }
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installHostMethod(runtime, prototype, "drawPaint", &JsiSkCanvas::drawPaint);
+ installHostMethod(runtime, prototype, "drawLine", &JsiSkCanvas::drawLine);
+ installHostMethod(runtime, prototype, "drawRect", &JsiSkCanvas::drawRect);
+ installHostMethod(runtime, prototype, "drawImage", &JsiSkCanvas::drawImage);
+ installHostMethod(runtime, prototype, "drawImageRect",
+ &JsiSkCanvas::drawImageRect);
+ installHostMethod(runtime, prototype, "drawImageCubic",
+ &JsiSkCanvas::drawImageCubic);
+ installHostMethod(runtime, prototype, "drawImageOptions",
+ &JsiSkCanvas::drawImageOptions);
+ installHostMethod(runtime, prototype, "drawImageNine",
+ &JsiSkCanvas::drawImageNine);
+ installHostMethod(runtime, prototype, "drawImageRectCubic",
+ &JsiSkCanvas::drawImageRectCubic);
+ installHostMethod(runtime, prototype, "drawImageRectOptions",
+ &JsiSkCanvas::drawImageRectOptions);
+ installHostMethod(runtime, prototype, "drawCircle",
+ &JsiSkCanvas::drawCircle);
+ installHostMethod(runtime, prototype, "drawArc", &JsiSkCanvas::drawArc);
+ installHostMethod(runtime, prototype, "drawRRect", &JsiSkCanvas::drawRRect);
+ installHostMethod(runtime, prototype, "drawDRRect",
+ &JsiSkCanvas::drawDRRect);
+ installHostMethod(runtime, prototype, "drawOval", &JsiSkCanvas::drawOval);
+ installHostMethod(runtime, prototype, "restoreToCount",
+ &JsiSkCanvas::restoreToCount);
+ installHostMethod(runtime, prototype, "getSaveCount",
+ &JsiSkCanvas::getSaveCount);
+ installHostMethod(runtime, prototype, "getTotalMatrix",
+ &JsiSkCanvas::getTotalMatrix);
+ installHostMethod(runtime, prototype, "drawPoints",
+ &JsiSkCanvas::drawPoints);
+ installHostMethod(runtime, prototype, "drawPatch", &JsiSkCanvas::drawPatch);
+ installHostMethod(runtime, prototype, "drawPath", &JsiSkCanvas::drawPath);
+ installHostMethod(runtime, prototype, "drawVertices",
+ &JsiSkCanvas::drawVertices);
+ installHostMethod(runtime, prototype, "drawText", &JsiSkCanvas::drawText);
+ installHostMethod(runtime, prototype, "drawTextBlob",
+ &JsiSkCanvas::drawTextBlob);
+ installHostMethod(runtime, prototype, "drawGlyphs",
+ &JsiSkCanvas::drawGlyphs);
+ installHostMethod(runtime, prototype, "drawSvg", &JsiSkCanvas::drawSvg);
+ installHostMethod(runtime, prototype, "clipPath", &JsiSkCanvas::clipPath);
+ installHostMethod(runtime, prototype, "clipRect", &JsiSkCanvas::clipRect);
+ installHostMethod(runtime, prototype, "clipRRect", &JsiSkCanvas::clipRRect);
+ installHostMethod(runtime, prototype, "save", &JsiSkCanvas::save);
+ installHostMethod(runtime, prototype, "saveLayer", &JsiSkCanvas::saveLayer);
+ installHostMethod(runtime, prototype, "restore", &JsiSkCanvas::restore);
+ installHostMethod(runtime, prototype, "rotate", &JsiSkCanvas::rotate);
+ installHostMethod(runtime, prototype, "translate", &JsiSkCanvas::translate);
+ installHostMethod(runtime, prototype, "scale", &JsiSkCanvas::scale);
+ installHostMethod(runtime, prototype, "skew", &JsiSkCanvas::skew);
+ installHostMethod(runtime, prototype, "drawColor", &JsiSkCanvas::drawColor);
+ installHostMethod(runtime, prototype, "clear", &JsiSkCanvas::clear);
+ installHostMethod(runtime, prototype, "concat", &JsiSkCanvas::concat);
+ installHostMethod(runtime, prototype, "drawPicture",
+ &JsiSkCanvas::drawPicture);
+ installHostMethod(runtime, prototype, "drawAtlas", &JsiSkCanvas::drawAtlas);
+ installHostMethod(runtime, prototype, "readPixels",
+ &JsiSkCanvas::readPixels);
+ }
+
+ size_t getMemoryPressure() override { return 1024; }
explicit JsiSkCanvas(std::shared_ptr context)
- : JsiSkHostObject(std::move(context)) {}
+ : JsiSkNativeObject(std::move(context)) {}
JsiSkCanvas(std::shared_ptr context, SkCanvas *canvas)
: JsiSkCanvas(std::move(context)) {
diff --git a/packages/skia/cpp/api/JsiSkColor.h b/packages/skia/cpp/api/JsiSkColor.h
index 305a8b8bac..5391c383bb 100644
--- a/packages/skia/cpp/api/JsiSkColor.h
+++ b/packages/skia/cpp/api/JsiSkColor.h
@@ -6,7 +6,7 @@
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#include "api/third_party/CSSColorParser.h"
#pragma clang diagnostic push
@@ -20,11 +20,13 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkColor : public RNJsi::JsiHostObject {
+/**
+ * JsiSkColor is a pure utility class — colors are represented as
+ * Float32Arrays on the JS side, so there is no wrapper object.
+ */
+class JsiSkColor {
public:
- JsiSkColor() : JsiHostObject() {}
-
- ~JsiSkColor() override = default;
+ JsiSkColor() = default;
static jsi::Object toValue(jsi::Runtime &runtime, SkColor color) {
auto result = runtime.global()
diff --git a/packages/skia/cpp/api/JsiSkColorFilter.h b/packages/skia/cpp/api/JsiSkColorFilter.h
index 8cba568c5e..f323068246 100644
--- a/packages/skia/cpp/api/JsiSkColorFilter.h
+++ b/packages/skia/cpp/api/JsiSkColorFilter.h
@@ -1,6 +1,6 @@
#pragma once
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#include
#include
#include
@@ -16,19 +16,26 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkColorFilter : public JsiSkWrappingSkPtrHostObject {
+class JsiSkColorFilter
+ : public JsiSkWrappingSkPtrNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "ColorFilter";
+
JsiSkColorFilter(std::shared_ptr context,
sk_sp colorFilter)
- : JsiSkWrappingSkPtrHostObject(std::move(context),
- std::move(colorFilter)) {}
+ : JsiSkWrappingSkPtrNativeObject(
+ std::move(context), std::move(colorFilter)) {}
- size_t getMemoryPressure() const override { return 2048; }
+ size_t getMemoryPressure() override { return 2048; }
- std::string getObjectType() const override { return "JsiSkColorFilter"; }
+ static sk_sp fromValue(jsi::Runtime &runtime,
+ const jsi::Value &obj) {
+ return objectFromValue(runtime, obj);
+ }
- EXPORT_JSI_API_TYPENAME(JsiSkColorFilter, ColorFilter)
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkColorFilter, dispose))
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installCommon(runtime, prototype);
+ }
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkColorFilterFactory.h b/packages/skia/cpp/api/JsiSkColorFilterFactory.h
index d9a70b9c2d..70e2a64d00 100644
--- a/packages/skia/cpp/api/JsiSkColorFilterFactory.h
+++ b/packages/skia/cpp/api/JsiSkColorFilterFactory.h
@@ -2,7 +2,7 @@
#include "JsiSkColor.h"
#include "JsiSkColorFilter.h"
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#include
#include
#include
@@ -19,8 +19,11 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkColorFilterFactory : public JsiSkHostObject {
+class JsiSkColorFilterFactory
+ : public JsiSkNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "ColorFilterFactory";
+
JSI_HOST_FUNCTION(MakeMatrix) {
auto jsiMatrix = arguments[0].asObject(runtime).asArray(runtime);
float matrix[20];
@@ -30,31 +33,28 @@ class JsiSkColorFilterFactory : public JsiSkHostObject {
}
}
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(), SkColorFilters::Matrix(std::move(matrix)));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(
+ getContext(), SkColorFilters::Matrix(std::move(matrix))));
}
JSI_HOST_FUNCTION(MakeBlend) {
auto color = JsiSkColor::fromValue(runtime, arguments[0]);
SkBlendMode blend = (SkBlendMode)arguments[1].asNumber();
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(), SkColorFilters::Blend(color, blend));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(
+ getContext(), SkColorFilters::Blend(color, blend)));
}
JSI_HOST_FUNCTION(MakeCompose) {
auto outer = JsiSkColorFilter::fromValue(runtime, arguments[0]);
auto inner = JsiSkColorFilter::fromValue(runtime, arguments[1]);
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(),
- SkColorFilters::Compose(std::move(outer), std::move(inner)));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(
+ getContext(), SkColorFilters::Compose(std::move(outer),
+ std::move(inner))));
}
JSI_HOST_FUNCTION(MakeLerp) {
@@ -62,52 +62,52 @@ class JsiSkColorFilterFactory : public JsiSkHostObject {
auto dst = JsiSkColorFilter::fromValue(runtime, arguments[1]);
auto src = JsiSkColorFilter::fromValue(runtime, arguments[2]);
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(), SkColorFilters::Lerp(t, std::move(dst), std::move(src)));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(
+ getContext(),
+ SkColorFilters::Lerp(t, std::move(dst), std::move(src))));
}
JSI_HOST_FUNCTION(MakeSRGBToLinearGamma) {
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(), SkColorFilters::SRGBToLinearGamma());
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(
+ getContext(), SkColorFilters::SRGBToLinearGamma()));
}
JSI_HOST_FUNCTION(MakeLinearToSRGBGamma) {
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(), SkColorFilters::LinearToSRGBGamma());
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(
+ getContext(), SkColorFilters::LinearToSRGBGamma()));
}
JSI_HOST_FUNCTION(MakeLumaColorFilter) {
// Return the newly constructed object
- auto colorFilter = std::make_shared(
- getContext(), SkLumaColorFilter::Make());
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
- getContext());
+ return makeJsiObject(runtime, std::make_shared(
+ getContext(), SkLumaColorFilter::Make()));
}
- size_t getMemoryPressure() const override { return 1024; }
-
- std::string getObjectType() const override {
- return "JsiSkColorFilterFactory";
+ 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);
}
- JSI_EXPORT_FUNCTIONS(
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeMatrix),
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeBlend),
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeCompose),
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeLerp),
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeSRGBToLinearGamma),
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeLinearToSRGBGamma),
- JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeLumaColorFilter))
-
explicit JsiSkColorFilterFactory(std::shared_ptr context)
- : JsiSkHostObject(std::move(context)) {}
+ : JsiSkNativeObject(std::move(context)) {}
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkContourMeasure.h b/packages/skia/cpp/api/JsiSkContourMeasure.h
index 4e0fed0269..a0f137ddb6 100644
--- a/packages/skia/cpp/api/JsiSkContourMeasure.h
+++ b/packages/skia/cpp/api/JsiSkContourMeasure.h
@@ -5,7 +5,7 @@
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -21,12 +21,15 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
class JsiSkContourMeasure
- : public JsiSkWrappingSkPtrHostObject {
+ : public JsiSkWrappingSkPtrNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "ContourMeasure";
+
JsiSkContourMeasure(std::shared_ptr context,
const sk_sp contourMeasure)
- : JsiSkWrappingSkPtrHostObject(std::move(context),
- std::move(contourMeasure)) {}
+ : JsiSkWrappingSkPtrNativeObject(
+ std::move(context), std::move(contourMeasure)) {}
JSI_HOST_FUNCTION(getPosTan) {
auto dist = arguments[0].asNumber();
@@ -37,12 +40,10 @@ class JsiSkContourMeasure
throw jsi::JSError(runtime, "getPosTan() failed");
}
auto posTan = jsi::Array(runtime, 2);
- auto posPoint = std::make_shared(getContext(), position);
- auto pos = JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, posPoint,
- getContext());
- auto tanPoint = std::make_shared(getContext(), tangent);
- auto tan = JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, tanPoint,
- getContext());
+ auto pos = makeJsiObject(
+ runtime, std::make_shared(getContext(), position));
+ auto tan = makeJsiObject(
+ runtime, std::make_shared(getContext(), tangent));
posTan.setValueAtIndex(runtime, 0, pos);
posTan.setValueAtIndex(runtime, 1, tan);
return posTan;
@@ -67,16 +68,18 @@ class JsiSkContourMeasure
return JsiSkPath::toValue(runtime, getContext(), builder.snapshot());
}
- size_t getMemoryPressure() const override { return 1024; }
-
- std::string getObjectType() const override { return "JsiSkContourMeasure"; }
-
- EXPORT_JSI_API_TYPENAME(JsiSkContourMeasure, ContourMeasure)
-
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkContourMeasure, getPosTan),
- JSI_EXPORT_FUNC(JsiSkContourMeasure, length),
- JSI_EXPORT_FUNC(JsiSkContourMeasure, isClosed),
- JSI_EXPORT_FUNC(JsiSkContourMeasure, getSegment),
- JSI_EXPORT_FUNC(JsiSkContourMeasure, dispose))
+ 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);
+ }
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkContourMeasureIter.h b/packages/skia/cpp/api/JsiSkContourMeasureIter.h
index 32da03aa67..7102cf2678 100644
--- a/packages/skia/cpp/api/JsiSkContourMeasureIter.h
+++ b/packages/skia/cpp/api/JsiSkContourMeasureIter.h
@@ -4,7 +4,7 @@
#include
#include "JsiSkContourMeasure.h"
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -20,12 +20,16 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
class JsiSkContourMeasureIter
- : public JsiSkWrappingSharedPtrHostObject {
+ : public JsiSkWrappingSharedPtrNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "ContourMeasureIter";
+
JsiSkContourMeasureIter(std::shared_ptr context,
const SkPath &path, bool forceClosed,
SkScalar resScale = 1)
- : JsiSkWrappingSharedPtrHostObject(
+ : JsiSkWrappingSharedPtrNativeObject(
std::move(context), std::make_shared(
path, forceClosed, resScale)) {}
@@ -34,24 +38,18 @@ class JsiSkContourMeasureIter
if (next == nullptr) {
return jsi::Value::undefined();
}
- auto nextObject =
- std::make_shared(getContext(), std::move(next));
-
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, nextObject,
- getContext());
+ return makeJsiObject(runtime, std::make_shared(
+ getContext(), std::move(next)));
}
- EXPORT_JSI_API_TYPENAME(JsiSkContourMeasureIter, ContourMeasureIter)
-
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkContourMeasureIter, next),
- JSI_EXPORT_FUNC(JsiSkContourMeasureIter, dispose))
-
- size_t getMemoryPressure() const override {
- return std::max(sizeof(SkContourMeasureIter), kMinMemoryPressure);
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installCommon(runtime, prototype);
+ installHostMethod(runtime, prototype, "next",
+ &JsiSkContourMeasureIter::next);
}
- std::string getObjectType() const override {
- return "JsiSkContourMeasureIter";
+ size_t getMemoryPressure() override {
+ return std::max(sizeof(SkContourMeasureIter), kMinMemoryPressure);
}
/**
@@ -68,10 +66,9 @@ class JsiSkContourMeasureIter
auto forceClosed = arguments[1].getBool();
auto resScale = arguments[2].asNumber();
// Return the newly constructed object
- auto iter = std::make_shared(
- std::move(context), path->snapshot(), forceClosed, resScale);
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, iter,
- context);
+ return makeJsiObject(runtime, std::make_shared(
+ std::move(context), path->snapshot(),
+ forceClosed, resScale));
};
}
};
diff --git a/packages/skia/cpp/api/JsiSkData.h b/packages/skia/cpp/api/JsiSkData.h
index e63052e203..788bf4dfb7 100644
--- a/packages/skia/cpp/api/JsiSkData.h
+++ b/packages/skia/cpp/api/JsiSkData.h
@@ -5,7 +5,7 @@
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -19,19 +19,25 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkData : public JsiSkWrappingSkPtrHostObject {
+class JsiSkData : public JsiSkWrappingSkPtrNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "Data";
+
JsiSkData(std::shared_ptr context, sk_sp asset)
- : JsiSkWrappingSkPtrHostObject(std::move(context), std::move(asset)) {}
+ : JsiSkWrappingSkPtrNativeObject(std::move(context),
+ std::move(asset)) {}
- size_t getMemoryPressure() const override {
+ size_t getMemoryPressure() override {
auto data = getObject();
return data ? data->size() : 0;
}
- std::string getObjectType() const override { return "JsiSkData"; }
+ static sk_sp fromValue(jsi::Runtime &runtime, const jsi::Value &obj) {
+ return objectFromValue(runtime, obj);
+ }
- EXPORT_JSI_API_TYPENAME(JsiSkData, Data)
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkData, dispose))
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installCommon(runtime, prototype);
+ }
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkDataFactory.h b/packages/skia/cpp/api/JsiSkDataFactory.h
index 477992b1e8..0b9cc868ab 100644
--- a/packages/skia/cpp/api/JsiSkDataFactory.h
+++ b/packages/skia/cpp/api/JsiSkDataFactory.h
@@ -5,16 +5,19 @@
#include
-#include "jsi/JsiPromises.h"
#include "JsiSkData.h"
+#include "JsiSkNativeObjects.h"
#include "api/third_party/base64.h"
+#include "jsi/JsiPromises.h"
namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkDataFactory : public JsiSkHostObject {
+class JsiSkDataFactory : public JsiSkNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "DataFactory";
+
JSI_HOST_FUNCTION(fromURI) {
auto jsiLocalUri = arguments[0].asString(runtime);
auto localUri = jsiLocalUri.utf8(runtime);
@@ -38,10 +41,9 @@ class JsiSkDataFactory : public JsiSkHostObject {
context = std::move(context),
promise = std::move(promise),
result = std::move(result)]() {
- auto hostObjectInstance =
- std::make_shared(context, std::move(result));
- promise->resolve(JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, context));
+ promise->resolve(makeJsiObject(
+ runtime,
+ std::make_shared(context, std::move(result))));
});
});
});
@@ -56,10 +58,8 @@ class JsiSkDataFactory : public JsiSkHostObject {
auto data =
SkData::MakeWithCopy(buffer.data(runtime), buffer.size(runtime));
- auto hostObjectInstance =
- std::make_shared(getContext(), std::move(data));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(getContext(), std::move(data)));
}
JSI_HOST_FUNCTION(fromBase64) {
@@ -85,22 +85,23 @@ class JsiSkDataFactory : public JsiSkHostObject {
return jsi::Value::undefined();
}
- auto hostObjectInstance =
- std::make_shared(getContext(), std::move(data));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(getContext(), std::move(data)));
}
- size_t getMemoryPressure() const override { return 1024; }
+ size_t getMemoryPressure() override { return 1024; }
- std::string getObjectType() const override { return "JsiSkDataFactory"; }
-
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkDataFactory, fromURI),
- JSI_EXPORT_FUNC(JsiSkDataFactory, fromBytes),
- JSI_EXPORT_FUNC(JsiSkDataFactory, fromBase64))
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installHostMethod(runtime, prototype, "fromURI",
+ &JsiSkDataFactory::fromURI);
+ installHostMethod(runtime, prototype, "fromBytes",
+ &JsiSkDataFactory::fromBytes);
+ installHostMethod(runtime, prototype, "fromBase64",
+ &JsiSkDataFactory::fromBase64);
+ }
explicit JsiSkDataFactory(std::shared_ptr context)
- : JsiSkHostObject(std::move(context)) {}
+ : JsiSkNativeObject(std::move(context)) {}
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkFont.h b/packages/skia/cpp/api/JsiSkFont.h
index be387ed61d..0b85c4fbdf 100644
--- a/packages/skia/cpp/api/JsiSkFont.h
+++ b/packages/skia/cpp/api/JsiSkFont.h
@@ -5,7 +5,7 @@
#include
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#include "utils/RNSkLog.h"
#include
@@ -26,8 +26,10 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkFont : public JsiSkWrappingSharedPtrHostObject {
+class JsiSkFont : public JsiSkWrappingSharedPtrNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "Font";
+
JSI_HOST_FUNCTION(getGlyphWidths) {
auto jsiGlyphs = arguments[0].asObject(runtime).asArray(runtime);
std::vector glyphs;
@@ -91,9 +93,8 @@ class JsiSkFont : public JsiSkWrappingSharedPtrHostObject {
getObject()->measureText(str.c_str(), str.length(), SkTextEncoding::kUTF8,
&bounds);
}
- auto rect = std::make_shared(getContext(), std::move(bounds));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, rect,
- getContext());
+ return makeJsiObject(
+ runtime, std::make_shared(getContext(), std::move(bounds)));
}
JSI_HOST_FUNCTION(getMetrics) {
@@ -251,39 +252,56 @@ class JsiSkFont : public JsiSkWrappingSharedPtrHostObject {
return jsi::Value::undefined();
}
- EXPORT_JSI_API_TYPENAME(JsiSkFont, Font)
-
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkFont, getSize),
- JSI_EXPORT_FUNC(JsiSkFont, getMetrics),
- JSI_EXPORT_FUNC(JsiSkFont, getGlyphIDs),
- JSI_EXPORT_FUNC(JsiSkFont, getGlyphIntercepts),
- JSI_EXPORT_FUNC(JsiSkFont, getScaleX),
- JSI_EXPORT_FUNC(JsiSkFont, getSkewX),
- JSI_EXPORT_FUNC(JsiSkFont, getTypeface),
- JSI_EXPORT_FUNC(JsiSkFont, setEdging),
- JSI_EXPORT_FUNC(JsiSkFont, embeddedBitmaps),
- JSI_EXPORT_FUNC(JsiSkFont, setHinting),
- JSI_EXPORT_FUNC(JsiSkFont, setLinearMetrics),
- JSI_EXPORT_FUNC(JsiSkFont, setScaleX),
- JSI_EXPORT_FUNC(JsiSkFont, setSkewX),
- JSI_EXPORT_FUNC(JsiSkFont, setSize),
- JSI_EXPORT_FUNC(JsiSkFont, setEmbolden),
- JSI_EXPORT_FUNC(JsiSkFont, setSubpixel),
- JSI_EXPORT_FUNC(JsiSkFont, setTypeface),
- JSI_EXPORT_FUNC(JsiSkFont, getGlyphWidths),
- JSI_EXPORT_FUNC(JsiSkFont, getTextWidth),
- JSI_EXPORT_FUNC(JsiSkFont, measureText),
- JSI_EXPORT_FUNC(JsiSkFont, dispose))
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installCommon(runtime, prototype);
+ installHostMethod(runtime, prototype, "getSize", &JsiSkFont::getSize);
+ installHostMethod(runtime, prototype, "getMetrics", &JsiSkFont::getMetrics);
+ installHostMethod(runtime, prototype, "getGlyphIDs",
+ &JsiSkFont::getGlyphIDs);
+ installHostMethod(runtime, prototype, "getGlyphIntercepts",
+ &JsiSkFont::getGlyphIntercepts);
+ installHostMethod(runtime, prototype, "getScaleX", &JsiSkFont::getScaleX);
+ installHostMethod(runtime, prototype, "getSkewX", &JsiSkFont::getSkewX);
+ installHostMethod(runtime, prototype, "getTypeface",
+ &JsiSkFont::getTypeface);
+ installHostMethod(runtime, prototype, "setEdging", &JsiSkFont::setEdging);
+ installHostMethod(runtime, prototype, "embeddedBitmaps",
+ &JsiSkFont::embeddedBitmaps);
+ installHostMethod(runtime, prototype, "setHinting", &JsiSkFont::setHinting);
+ installHostMethod(runtime, prototype, "setLinearMetrics",
+ &JsiSkFont::setLinearMetrics);
+ installHostMethod(runtime, prototype, "setScaleX", &JsiSkFont::setScaleX);
+ installHostMethod(runtime, prototype, "setSkewX", &JsiSkFont::setSkewX);
+ installHostMethod(runtime, prototype, "setSize", &JsiSkFont::setSize);
+ installHostMethod(runtime, prototype, "setEmbolden",
+ &JsiSkFont::setEmbolden);
+ installHostMethod(runtime, prototype, "setSubpixel",
+ &JsiSkFont::setSubpixel);
+ installHostMethod(runtime, prototype, "setTypeface",
+ &JsiSkFont::setTypeface);
+ installHostMethod(runtime, prototype, "getGlyphWidths",
+ &JsiSkFont::getGlyphWidths);
+ installHostMethod(runtime, prototype, "getTextWidth",
+ &JsiSkFont::getTextWidth);
+ installHostMethod(runtime, prototype, "measureText",
+ &JsiSkFont::measureText);
+ }
JsiSkFont(std::shared_ptr context, const SkFont &font)
- : JsiSkWrappingSharedPtrHostObject(std::move(context),
- std::make_shared(font)) {}
+ : JsiSkWrappingSharedPtrNativeObject(
+ std::move(context), std::make_shared(font)) {}
- size_t getMemoryPressure() const override {
+ size_t getMemoryPressure() override {
return std::max(sizeof(SkFont), kMinMemoryPressure);
}
- std::string getObjectType() const override { return "JsiSkFont"; }
+ /**
+ Returns the underlying object from a host object of this type
+ */
+ static std::shared_ptr fromValue(jsi::Runtime &runtime,
+ const jsi::Value &obj) {
+ return objectFromValue(runtime, obj);
+ }
/**
* Creates the function for construction a new instance of the SkFont
@@ -299,22 +317,16 @@ class JsiSkFont : public JsiSkWrappingSharedPtrHostObject {
if (count == 2) {
auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
auto size = arguments[1].asNumber();
- auto hostObjectInstance =
- std::make_shared(context, SkFont(typeface, size));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, std::move(context));
+ return makeJsiObject(runtime, std::make_shared(
+ context, SkFont(typeface, size)));
} else if (count == 1) {
auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
- auto hostObjectInstance =
- std::make_shared(context, SkFont(typeface));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, std::move(context));
+ return makeJsiObject(
+ runtime, std::make_shared(context, SkFont(typeface)));
} else {
// Return the newly constructed object
- auto hostObjectInstance =
- std::make_shared(context, SkFont());
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, std::move(context));
+ return makeJsiObject(runtime,
+ std::make_shared(context, SkFont()));
}
};
}
diff --git a/packages/skia/cpp/api/JsiSkFontMgr.h b/packages/skia/cpp/api/JsiSkFontMgr.h
index 4ce5837ea4..c4f1936d57 100644
--- a/packages/skia/cpp/api/JsiSkFontMgr.h
+++ b/packages/skia/cpp/api/JsiSkFontMgr.h
@@ -6,7 +6,7 @@
#include
#include "JsiSkFontStyle.h"
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#include "utils/RNSkLog.h"
#include
@@ -21,13 +21,15 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkFontMgr : public JsiSkWrappingSkPtrHostObject {
+class JsiSkFontMgr
+ : public JsiSkWrappingSkPtrNativeObject {
public:
- EXPORT_JSI_API_TYPENAME(JsiSkFontMgr, FontMgr)
+ static constexpr const char *CLASS_NAME = "FontMgr";
JsiSkFontMgr(std::shared_ptr context,
sk_sp fontMgr)
- : JsiSkWrappingSkPtrHostObject(context, fontMgr),
+ : JsiSkWrappingSkPtrNativeObject(context,
+ fontMgr),
_systemFontFamilies(context->getSystemFontFamilies()) {}
JSI_HOST_FUNCTION(countFamilies) {
@@ -63,19 +65,26 @@ class JsiSkFontMgr : public JsiSkWrappingSkPtrHostObject {
auto fontStyle = JsiSkFontStyle::fromValue(runtime, arguments[1]);
auto typeface =
getObject()->matchFamilyStyle(resolvedName.c_str(), *fontStyle);
- auto hostObjectInstance =
- std::make_shared(getContext(), std::move(typeface));
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
- runtime, hostObjectInstance, getContext());
+ return makeJsiObject(runtime, std::make_shared(
+ getContext(), std::move(typeface)));
}
- size_t getMemoryPressure() const override { return 2048; }
+ size_t getMemoryPressure() override { return 2048; }
- std::string getObjectType() const override { return "JsiSkFontMgr"; }
+ static sk_sp fromValue(jsi::Runtime &runtime,
+ const jsi::Value &obj) {
+ return objectFromValue(runtime, obj);
+ }
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkFontMgr, countFamilies),
- JSI_EXPORT_FUNC(JsiSkFontMgr, getFamilyName),
- JSI_EXPORT_FUNC(JsiSkFontMgr, matchFamilyStyle))
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installCommon(runtime, prototype);
+ installHostMethod(runtime, prototype, "countFamilies",
+ &JsiSkFontMgr::countFamilies);
+ installHostMethod(runtime, prototype, "getFamilyName",
+ &JsiSkFontMgr::getFamilyName);
+ installHostMethod(runtime, prototype, "matchFamilyStyle",
+ &JsiSkFontMgr::matchFamilyStyle);
+ }
private:
std::vector _systemFontFamilies;
diff --git a/packages/skia/cpp/api/JsiSkFontMgrFactory.h b/packages/skia/cpp/api/JsiSkFontMgrFactory.h
index d283862b88..879a4d4149 100644
--- a/packages/skia/cpp/api/JsiSkFontMgrFactory.h
+++ b/packages/skia/cpp/api/JsiSkFontMgrFactory.h
@@ -6,7 +6,7 @@
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -20,8 +20,10 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkFontMgrFactory : public JsiSkHostObject {
+class JsiSkFontMgrFactory : public JsiSkNativeObject {
public:
+ static constexpr const char *CLASS_NAME = "FontMgrFactory";
+
static sk_sp
getFontMgr(std::shared_ptr context) {
static SkOnce once;
@@ -32,19 +34,19 @@ class JsiSkFontMgrFactory : public JsiSkHostObject {
JSI_HOST_FUNCTION(System) {
auto fontMgr = JsiSkFontMgrFactory::getFontMgr(getContext());
- auto fontMgrObj = std::make_shared(getContext(), fontMgr);
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, fontMgrObj,
- getContext());
+ return makeJsiObject(runtime,
+ std::make_shared(getContext(), fontMgr));
}
- JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkFontMgrFactory, System))
-
- size_t getMemoryPressure() const override { return 1024; }
+ size_t getMemoryPressure() override { return 1024; }
- std::string getObjectType() const override { return "JsiSkFontMgrFactory"; }
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installHostMethod(runtime, prototype, "System",
+ &JsiSkFontMgrFactory::System);
+ }
explicit JsiSkFontMgrFactory(std::shared_ptr context)
- : JsiSkHostObject(std::move(context)) {}
+ : JsiSkNativeObject(std::move(context)) {}
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkFontStyle.h b/packages/skia/cpp/api/JsiSkFontStyle.h
index ea93f52999..439fd60b39 100644
--- a/packages/skia/cpp/api/JsiSkFontStyle.h
+++ b/packages/skia/cpp/api/JsiSkFontStyle.h
@@ -5,7 +5,7 @@
#include
-#include "JsiSkHostObjects.h"
+#include "JsiSkNativeObjects.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
@@ -18,13 +18,14 @@ namespace RNSkia {
namespace jsi = facebook::jsi;
-class JsiSkFontStyle : public JsiSkWrappingSharedPtrHostObject {
+class JsiSkFontStyle
+ : public JsiSkWrappingSharedPtrNativeObject {
public:
- JSI_API_TYPENAME("FontStyle");
+ static constexpr const char *CLASS_NAME = "FontStyle";
JsiSkFontStyle(std::shared_ptr context,
const SkFontStyle &fontStyle)
- : JsiSkWrappingSharedPtrHostObject(
+ : JsiSkWrappingSharedPtrNativeObject(
std::move(context), std::make_shared(fontStyle)) {}
/**
@@ -33,8 +34,9 @@ class JsiSkFontStyle : public JsiSkWrappingSharedPtrHostObject {
static std::shared_ptr fromValue(jsi::Runtime &runtime,
const jsi::Value &obj) {
const auto &object = obj.asObject(runtime);
- if (object.isHostObject(runtime)) {
- return object.asHostObject(runtime)->getObject();
+ auto fontStyle = tryGetJsiObject(runtime, object);
+ if (fontStyle) {
+ return fontStyle->getObject();
} else {
auto weightProp = object.getProperty(runtime, "weight");
auto weight = static_cast(
@@ -56,11 +58,13 @@ class JsiSkFontStyle : public JsiSkWrappingSharedPtrHostObject {
}
}
- size_t getMemoryPressure() const override {
+ size_t getMemoryPressure() override {
return std::max(sizeof(SkFontStyle), kMinMemoryPressure);
}
- std::string getObjectType() const override { return "JsiSkFontStyle"; }
+ static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
+ installCommon(runtime, prototype);
+ }
/**
Returns the jsi object from a host object of this type
@@ -68,10 +72,8 @@ class JsiSkFontStyle : public JsiSkWrappingSharedPtrHostObject {
static jsi::Value toValue(jsi::Runtime &runtime,
std::shared_ptr context,
const SkFontStyle &fontStyle) {
- auto fontStyleObj =
- std::make_shared(std::move(context), fontStyle);
- return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, fontStyleObj,
- context);
+ return makeJsiObject(runtime, std::make_shared(
+ std::move(context), fontStyle));
}
};
} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkHostObjects.h b/packages/skia/cpp/api/JsiSkHostObjects.h
deleted file mode 100644
index 6d8f2c01e3..0000000000
--- a/packages/skia/cpp/api/JsiSkHostObjects.h
+++ /dev/null
@@ -1,257 +0,0 @@
-#pragma once
-
-#include
-#include
-#include
-#include
-
-#include "jsi/JsiHostObject.h"
-#include "utils/RNSkLog.h"
-#include "rnskia/RNSkPlatformContext.h"
-
-namespace RNSkia {
-
-namespace jsi = facebook::jsi;
-
-// Minimum memory pressure reported for any host object.
-// This accounts for C++ wrapper overhead and ensures dispose() never
-// increases reported memory pressure (which would defeat its purpose).
-static constexpr size_t kMinMemoryPressure = 256;
-
-/**
- * Base class for jsi host objects - these are all implemented as JsiHostObjects
- * and has a pointer to the platform context.
- */
-class JsiSkHostObject : public RNJsi::JsiHostObject {
-public:
- /**
- * Default constructor
- * @param context Platform context
- */
- explicit JsiSkHostObject(std::shared_ptr context)
- : _context(context) {}
-
- /**
- * Override this method to return the memory pressure for the wrapped object.
- * @return The memory pressure in bytes
- */
- virtual size_t getMemoryPressure() const = 0;
-
- /**
- * Returns the type name of the host object.
- */
- virtual std::string getObjectType() const = 0;
-
-protected:
- /**
- * @return A pointer to the platform context
- */
- std::shared_ptr getContext() { return _context; }
-
-private:
- std::shared_ptr _context;
-};
-
-#define JSI_API_TYPENAME(A) \
- JSI_PROPERTY_GET(__typename__) { \
- return jsi::String::createFromUtf8(runtime, #A); \
- }
-
-#define EXPORT_JSI_API_TYPENAME(CLASS, TYPENAME) \
- JSI_API_TYPENAME(TYPENAME) \
- JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(CLASS, __typename__))
-
-// Define this macro to enable memory pressure debug logging
-// #define RNSKIA_DEBUG_MEMORY_PRESSURE
-
-#ifdef RNSKIA_DEBUG_MEMORY_PRESSURE
-// Version with debug logging
-#define JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE( \
- runtime, hostObjectInstance, context) \
- [&]() { \
- auto result = \
- jsi::Object::createFromHostObject(runtime, hostObjectInstance); \
- auto memoryPressure = hostObjectInstance->getMemoryPressure(); \
- const void *hostObjectId = \
- static_cast(hostObjectInstance.get()); \
- const char *mpUnit = "bytes"; \
- double mpValue = static_cast(memoryPressure); \
- if (memoryPressure >= 1024ULL * 1024ULL) { \
- mpUnit = "MB"; \
- mpValue /= (1024.0 * 1024.0); \
- RNSkLogger::logToConsole( \
- "Host object %s (id=%p) memory pressure %.2f %s", \
- hostObjectInstance->getObjectType().c_str(), hostObjectId, mpValue, \
- mpUnit); \
- } else if (memoryPressure >= 1024ULL) { \
- mpUnit = "KB"; \
- mpValue /= 1024.0; \
- RNSkLogger::logToConsole( \
- "Host object %s (id=%p) memory pressure %.2f %s", \
- hostObjectInstance->getObjectType().c_str(), hostObjectId, mpValue, \
- mpUnit); \
- } else { \
- RNSkLogger::logToConsole( \
- "Host object %s (id=%p) memory pressure %zu %s", \
- hostObjectInstance->getObjectType().c_str(), hostObjectId, \
- memoryPressure, mpUnit); \
- } \
- if (memoryPressure > 0) { \
- result.setExternalMemoryPressure(runtime, memoryPressure); \
- } \
- return result; \
- }()
-#else
-// Version without debug logging (optimized)
-#define JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE( \
- runtime, hostObjectInstance, context) \
- [&]() { \
- auto result = \
- jsi::Object::createFromHostObject(runtime, hostObjectInstance); \
- auto memoryPressure = hostObjectInstance->getMemoryPressure(); \
- if (memoryPressure > 0) { \
- result.setExternalMemoryPressure(runtime, memoryPressure); \
- } \
- return result; \
- }()
-#endif
-
-template class JsiSkWrappingHostObject : public JsiSkHostObject {
-public:
- /**
- * Default constructor
- * @param context Platform context
- */
- JsiSkWrappingHostObject(std::shared_ptr context,
- T object)
- : JsiSkHostObject(std::move(context)), _object(std::move(object)) {}
-
- /**
- * Returns the underlying object exposed by this host object. This object
- * should be wrapped in a shared pointer of some kind.
- * Throws if the object has been disposed.
- * @return Underlying object
- */
- T getObject() { return validateObject(); }
- const T getObject() const { return validateObject(); }
-
- /**
- * Updates the inner object with a new version of the object.
- */
- void setObject(T object) { _object = object; }
-
- /**
- * Dispose function that can be exposed to JS by using the JSI_API_TYPENAME
- * macro.
- */
- JSI_HOST_FUNCTION(dispose) {
- if (!isDisposed()) {
- thisValue.asObject(runtime).setExternalMemoryPressure(runtime,
- kMinMemoryPressure);
- }
- safeDispose();
- return jsi::Value::undefined();
- }
-
-protected:
- /**
- * Override to implement disposal of allocated resources like smart pointers.
- * This method will only be called once for each instance of this class.
- */
- virtual void releaseResources() = 0;
-
- /**
- * Returns true if the object has been disposed.
- */
- bool isDisposed() const {
- return _isDisposed.load(std::memory_order_acquire);
- }
-
- /**
- * Returns the underlying object without checking if disposed.
- * Use this in destructors and releaseResources() where we need to access
- * the object even after dispose() was called.
- */
- T getObjectUnchecked() const { return _object; }
-
-private:
- /**
- * Validates that _object was not disposed and returns it.
- */
- T validateObject() const {
- if (_isDisposed.load(std::memory_order_acquire)) {
- throw std::runtime_error("Attempted to access a disposed object.");
- }
- return _object;
- }
-
- void safeDispose() {
- bool expected = false;
- if (_isDisposed.compare_exchange_strong(expected, true,
- std::memory_order_acq_rel)) {
- releaseResources();
- }
- }
-
- /**
- * Wrapped object.
- */
- T _object;
-
- /**
- * Resource disposed flag.
- */
- std::atomic _isDisposed = {false};
-};
-
-template
-class JsiSkWrappingSharedPtrHostObject
- : public JsiSkWrappingHostObject> {
-public:
- JsiSkWrappingSharedPtrHostObject(std::shared_ptr context,
- std::shared_ptr object)
- : JsiSkWrappingHostObject>(std::move(context),
- std::move(object)) {}
-
- /**
- Returns the underlying object from a host object of this type
- */
- static std::shared_ptr fromValue(jsi::Runtime &runtime,
- const jsi::Value &obj) {
- return std::static_pointer_cast(
- obj.asObject(runtime).asHostObject(runtime))
- ->getObject();
- }
-
-protected:
- void releaseResources() override {
- // Clear internally allocated objects
- this->setObject(nullptr);
- }
-};
-
-template
-class JsiSkWrappingSkPtrHostObject : public JsiSkWrappingHostObject> {
-public:
- JsiSkWrappingSkPtrHostObject(std::shared_ptr context,
- sk_sp object)
- : JsiSkWrappingHostObject>(std::move(context),
- std::move(object)) {}
-
- /**
- Returns the underlying object from a host object of this type
- */
- static sk_sp fromValue(jsi::Runtime &runtime, const jsi::Value &obj) {
- return std::static_pointer_cast(
- obj.asObject(runtime).asHostObject(runtime))
- ->getObject();
- }
-
-protected:
- void releaseResources() override {
- // Clear internally allocated objects
- this->setObject(nullptr);
- }
-};
-
-} // namespace RNSkia
diff --git a/packages/skia/cpp/api/JsiSkImage.h b/packages/skia/cpp/api/JsiSkImage.h
index 377324b762..8551a58371 100644
--- a/packages/skia/cpp/api/JsiSkImage.h
+++ b/packages/skia/cpp/api/JsiSkImage.h
@@ -5,9 +5,9 @@
#include
#include "JsiSkDispatcher.h"
-#include "JsiSkHostObjects.h"
#include "JsiSkImageInfo.h"
#include "JsiSkMatrix.h"
+#include "JsiSkNativeObjects.h"
#include "JsiSkShader.h"
#include "api/third_party/base64.h"
@@ -15,8 +15,8 @@
#include "utils/RNSkTypedArray.h"
#if defined(SK_GRAPHITE)
-#include "rnskia/RNDawnContext.h"
#include "include/gpu/graphite/Context.h"
+#include "rnskia/RNDawnContext.h"
#else
#include "include/gpu/ganesh/GrDirectContext.h"
#endif
@@ -140,11 +140,13 @@ inline SkSamplingOptions SamplingOptionsFromValue(jsi::Runtime &runtime,
return samplingOptions;
}
-class JsiSkImage : public JsiSkWrappingSkPtrHostObject {
+class JsiSkImage : public JsiSkWrappingSkPtrNativeObject {
private:
std::shared_ptr _dispatcher;
public:
+ static constexpr const char *CLASS_NAME = "Image";
+
// TODO-API: Properties?
JSI_HOST_FUNCTION(width) { return static_cast