@shopify/react-native-skia 2.2.11 → 2.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/cpp/api/JsiNativeBuffer.h +2 -0
  2. package/cpp/api/JsiSkAnimatedImage.h +6 -2
  3. package/cpp/api/JsiSkAnimatedImageFactory.h +6 -3
  4. package/cpp/api/JsiSkApi.h +2 -0
  5. package/cpp/api/JsiSkCanvas.h +2 -0
  6. package/cpp/api/JsiSkColorFilter.h +2 -0
  7. package/cpp/api/JsiSkColorFilterFactory.h +31 -23
  8. package/cpp/api/JsiSkContourMeasure.h +8 -4
  9. package/cpp/api/JsiSkContourMeasureIter.h +10 -4
  10. package/cpp/api/JsiSkData.h +5 -0
  11. package/cpp/api/JsiSkDataFactory.h +14 -7
  12. package/cpp/api/JsiSkFont.h +17 -10
  13. package/cpp/api/JsiSkFontMgr.h +6 -2
  14. package/cpp/api/JsiSkFontMgrFactory.h +5 -2
  15. package/cpp/api/JsiSkFontStyle.h +6 -3
  16. package/cpp/api/JsiSkHostObjects.h +21 -44
  17. package/cpp/api/JsiSkImage.h +17 -8
  18. package/cpp/api/JsiSkImageFactory.h +26 -13
  19. package/cpp/api/JsiSkImageFilter.h +2 -0
  20. package/cpp/api/JsiSkImageFilterFactory.h +160 -143
  21. package/cpp/api/JsiSkImageInfo.h +6 -3
  22. package/cpp/api/JsiSkMaskFilter.h +2 -0
  23. package/cpp/api/JsiSkMaskFilterFactory.h +7 -5
  24. package/cpp/api/JsiSkMatrix.h +6 -2
  25. package/cpp/api/JsiSkPaint.h +9 -4
  26. package/cpp/api/JsiSkParagraph.h +2 -0
  27. package/cpp/api/JsiSkParagraphBuilder.h +6 -3
  28. package/cpp/api/JsiSkParagraphBuilderFactory.h +6 -3
  29. package/cpp/api/JsiSkPath.h +33 -15
  30. package/cpp/api/JsiSkPathEffect.h +2 -0
  31. package/cpp/api/JsiSkPathEffectFactory.h +37 -32
  32. package/cpp/api/JsiSkPathFactory.h +22 -10
  33. package/cpp/api/JsiSkPicture.h +13 -2
  34. package/cpp/api/JsiSkPictureFactory.h +6 -2
  35. package/cpp/api/JsiSkPictureRecorder.h +14 -6
  36. package/cpp/api/JsiSkPoint.h +9 -5
  37. package/cpp/api/JsiSkRRect.h +13 -8
  38. package/cpp/api/JsiSkRSXform.h +14 -8
  39. package/cpp/api/JsiSkRect.h +13 -8
  40. package/cpp/api/JsiSkRuntimeEffect.h +10 -6
  41. package/cpp/api/JsiSkRuntimeEffectFactory.h +6 -3
  42. package/cpp/api/JsiSkRuntimeShaderBuilder.h +12 -6
  43. package/cpp/api/JsiSkSVG.h +24 -0
  44. package/cpp/api/JsiSkSVGFactory.h +8 -4
  45. package/cpp/api/JsiSkShader.h +2 -0
  46. package/cpp/api/JsiSkShaderFactory.h +34 -24
  47. package/cpp/api/JsiSkSkottie.h +29 -0
  48. package/cpp/api/JsiSkSurface.h +21 -5
  49. package/cpp/api/JsiSkSurfaceFactory.h +10 -6
  50. package/cpp/api/JsiSkTextBlob.h +12 -0
  51. package/cpp/api/JsiSkTextBlobFactory.h +18 -12
  52. package/cpp/api/JsiSkTypeface.h +19 -3
  53. package/cpp/api/JsiSkTypefaceFactory.h +6 -2
  54. package/cpp/api/JsiSkTypefaceFontProvider.h +10 -5
  55. package/cpp/api/JsiSkTypefaceFontProviderFactory.h +6 -3
  56. package/cpp/api/JsiSkVertices.h +16 -5
  57. package/cpp/api/JsiSkiaContext.h +10 -6
  58. package/cpp/api/JsiSkottieFactory.h +6 -3
  59. package/cpp/api/JsiVideo.h +10 -5
  60. package/cpp/api/recorder/JsiRecorder.h +9 -4
  61. package/cpp/rnskia/RNSkJsiViewApi.h +5 -2
  62. package/package.json +1 -1
@@ -36,6 +36,8 @@ public:
36
36
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiNativeBufferFactory, Release),
37
37
  JSI_EXPORT_FUNC(JsiNativeBufferFactory, MakeFromImage))
38
38
 
39
+ size_t getMemoryPressure() const override { return 1024; }
40
+
39
41
  explicit JsiNativeBufferFactory(std::shared_ptr<RNSkPlatformContext> context)
40
42
  : JsiSkHostObject(std::move(context)) {}
41
43
  };
@@ -31,8 +31,10 @@ public:
31
31
  // TODO-API: Properties?
32
32
  JSI_HOST_FUNCTION(getCurrentFrame) {
33
33
  auto image = getObject()->getCurrentFrame();
34
- return jsi::Object::createFromHostObject(
35
- runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
34
+ auto hostObjectInstance =
35
+ std::make_shared<JsiSkImage>(getContext(), std::move(image));
36
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
37
+ runtime, hostObjectInstance, getContext());
36
38
  }
37
39
 
38
40
  JSI_HOST_FUNCTION(getFrameCount) {
@@ -60,6 +62,8 @@ public:
60
62
  const sk_sp<SkAnimatedImage> image)
61
63
  : JsiSkWrappingSkPtrHostObject<SkAnimatedImage>(std::move(context),
62
64
  std::move(image)) {}
65
+
66
+ size_t getMemoryPressure() const override { return 8192; }
63
67
  };
64
68
 
65
69
  } // namespace RNSkia
@@ -23,11 +23,14 @@ public:
23
23
  if (image == nullptr) {
24
24
  return jsi::Value::null();
25
25
  }
26
- return jsi::Object::createFromHostObject(
27
- runtime,
28
- std::make_shared<JsiSkAnimatedImage>(getContext(), std::move(image)));
26
+ auto hostObjectInstance =
27
+ std::make_shared<JsiSkAnimatedImage>(getContext(), std::move(image));
28
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
29
+ runtime, hostObjectInstance, getContext());
29
30
  }
30
31
 
32
+ size_t getMemoryPressure() const override { return 1024; }
33
+
31
34
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkAnimatedImageFactory,
32
35
  MakeAnimatedImageFromEncoded))
33
36
 
@@ -61,6 +61,8 @@ namespace jsi = facebook::jsi;
61
61
 
62
62
  class JsiSkApi : public JsiSkHostObject {
63
63
  public:
64
+ size_t getMemoryPressure() const override { return 8192; }
65
+
64
66
  /**
65
67
  * Constructs the Skia Api object that can be installed into a runtime
66
68
  * and provide functions for accessing and creating the Skia wrapper objects
@@ -681,6 +681,8 @@ public:
681
681
  JSI_EXPORT_FUNC(JsiSkCanvas, drawAtlas),
682
682
  JSI_EXPORT_FUNC(JsiSkCanvas, readPixels))
683
683
 
684
+ size_t getMemoryPressure() const override { return 1024; }
685
+
684
686
  explicit JsiSkCanvas(std::shared_ptr<RNSkPlatformContext> context)
685
687
  : JsiSkHostObject(std::move(context)) {}
686
688
 
@@ -23,6 +23,8 @@ public:
23
23
  : JsiSkWrappingSkPtrHostObject<SkColorFilter>(std::move(context),
24
24
  std::move(colorFilter)) {}
25
25
 
26
+ size_t getMemoryPressure() const override { return 2048; }
27
+
26
28
  EXPORT_JSI_API_TYPENAME(JsiSkColorFilter, ColorFilter)
27
29
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkColorFilter, dispose))
28
30
  };
@@ -30,28 +30,31 @@ public:
30
30
  }
31
31
  }
32
32
  // Return the newly constructed object
33
- return jsi::Object::createFromHostObject(
34
- runtime, std::make_shared<JsiSkColorFilter>(
35
- getContext(), SkColorFilters::Matrix(std::move(matrix))));
33
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
34
+ getContext(), SkColorFilters::Matrix(std::move(matrix)));
35
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
36
+ getContext());
36
37
  }
37
38
 
38
39
  JSI_HOST_FUNCTION(MakeBlend) {
39
40
  auto color = JsiSkColor::fromValue(runtime, arguments[0]);
40
41
  SkBlendMode blend = (SkBlendMode)arguments[1].asNumber();
41
42
  // Return the newly constructed object
42
- return jsi::Object::createFromHostObject(
43
- runtime, std::make_shared<JsiSkColorFilter>(
44
- getContext(), SkColorFilters::Blend(color, blend)));
43
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
44
+ getContext(), SkColorFilters::Blend(color, blend));
45
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
46
+ getContext());
45
47
  }
46
48
 
47
49
  JSI_HOST_FUNCTION(MakeCompose) {
48
50
  auto outer = JsiSkColorFilter::fromValue(runtime, arguments[0]);
49
51
  auto inner = JsiSkColorFilter::fromValue(runtime, arguments[1]);
50
52
  // Return the newly constructed object
51
- return jsi::Object::createFromHostObject(
52
- runtime, std::make_shared<JsiSkColorFilter>(
53
- getContext(), SkColorFilters::Compose(std::move(outer),
54
- std::move(inner))));
53
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
54
+ getContext(),
55
+ SkColorFilters::Compose(std::move(outer), std::move(inner)));
56
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
57
+ getContext());
55
58
  }
56
59
 
57
60
  JSI_HOST_FUNCTION(MakeLerp) {
@@ -59,33 +62,38 @@ public:
59
62
  auto dst = JsiSkColorFilter::fromValue(runtime, arguments[1]);
60
63
  auto src = JsiSkColorFilter::fromValue(runtime, arguments[2]);
61
64
  // Return the newly constructed object
62
- return jsi::Object::createFromHostObject(
63
- runtime, std::make_shared<JsiSkColorFilter>(
64
- getContext(),
65
- SkColorFilters::Lerp(t, std::move(dst), std::move(src))));
65
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
66
+ getContext(), SkColorFilters::Lerp(t, std::move(dst), std::move(src)));
67
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
68
+ getContext());
66
69
  }
67
70
 
68
71
  JSI_HOST_FUNCTION(MakeSRGBToLinearGamma) {
69
72
  // Return the newly constructed object
70
- return jsi::Object::createFromHostObject(
71
- runtime, std::make_shared<JsiSkColorFilter>(
72
- getContext(), SkColorFilters::SRGBToLinearGamma()));
73
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
74
+ getContext(), SkColorFilters::SRGBToLinearGamma());
75
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
76
+ getContext());
73
77
  }
74
78
 
75
79
  JSI_HOST_FUNCTION(MakeLinearToSRGBGamma) {
76
80
  // Return the newly constructed object
77
- return jsi::Object::createFromHostObject(
78
- runtime, std::make_shared<JsiSkColorFilter>(
79
- getContext(), SkColorFilters::LinearToSRGBGamma()));
81
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
82
+ getContext(), SkColorFilters::LinearToSRGBGamma());
83
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
84
+ getContext());
80
85
  }
81
86
 
82
87
  JSI_HOST_FUNCTION(MakeLumaColorFilter) {
83
88
  // Return the newly constructed object
84
- return jsi::Object::createFromHostObject(
85
- runtime, std::make_shared<JsiSkColorFilter>(getContext(),
86
- SkLumaColorFilter::Make()));
89
+ auto colorFilter = std::make_shared<JsiSkColorFilter>(
90
+ getContext(), SkLumaColorFilter::Make());
91
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, colorFilter,
92
+ getContext());
87
93
  }
88
94
 
95
+ size_t getMemoryPressure() const override { return 1024; }
96
+
89
97
  JSI_EXPORT_FUNCTIONS(
90
98
  JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeMatrix),
91
99
  JSI_EXPORT_FUNC(JsiSkColorFilterFactory, MakeBlend),
@@ -37,10 +37,12 @@ public:
37
37
  throw jsi::JSError(runtime, "getPosTan() failed");
38
38
  }
39
39
  auto posTan = jsi::Array(runtime, 2);
40
- auto pos = jsi::Object::createFromHostObject(
41
- runtime, std::make_shared<JsiSkPoint>(getContext(), position));
42
- auto tan = jsi::Object::createFromHostObject(
43
- runtime, std::make_shared<JsiSkPoint>(getContext(), tangent));
40
+ auto posPoint = std::make_shared<JsiSkPoint>(getContext(), position);
41
+ auto pos = JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, posPoint,
42
+ getContext());
43
+ auto tanPoint = std::make_shared<JsiSkPoint>(getContext(), tangent);
44
+ auto tan = JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, tanPoint,
45
+ getContext());
44
46
  posTan.setValueAtIndex(runtime, 0, pos);
45
47
  posTan.setValueAtIndex(runtime, 1, tan);
46
48
  return posTan;
@@ -64,6 +66,8 @@ public:
64
66
  return JsiSkPath::toValue(runtime, getContext(), std::move(path));
65
67
  }
66
68
 
69
+ size_t getMemoryPressure() const override { return 1024; }
70
+
67
71
  EXPORT_JSI_API_TYPENAME(JsiSkContourMeasure, ContourMeasure)
68
72
 
69
73
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkContourMeasure, getPosTan),
@@ -37,7 +37,8 @@ public:
37
37
  auto nextObject =
38
38
  std::make_shared<JsiSkContourMeasure>(getContext(), std::move(next));
39
39
 
40
- return jsi::Object::createFromHostObject(runtime, std::move(nextObject));
40
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, nextObject,
41
+ getContext());
41
42
  }
42
43
 
43
44
  EXPORT_JSI_API_TYPENAME(JsiSkContourMeasureIter, ContourMeasureIter)
@@ -45,6 +46,10 @@ public:
45
46
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkContourMeasureIter, next),
46
47
  JSI_EXPORT_FUNC(JsiSkContourMeasureIter, dispose))
47
48
 
49
+ size_t getMemoryPressure() const override {
50
+ return sizeof(SkContourMeasureIter);
51
+ }
52
+
48
53
  /**
49
54
  * Creates the function for construction a new instance of the
50
55
  * SkContourMeasureIter wrapper
@@ -59,9 +64,10 @@ public:
59
64
  auto forceClosed = arguments[1].getBool();
60
65
  auto resScale = arguments[2].asNumber();
61
66
  // Return the newly constructed object
62
- return jsi::Object::createFromHostObject(
63
- runtime, std::make_shared<JsiSkContourMeasureIter>(
64
- std::move(context), *path, forceClosed, resScale));
67
+ auto iter = std::make_shared<JsiSkContourMeasureIter>(
68
+ std::move(context), *path, forceClosed, resScale);
69
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, iter,
70
+ context);
65
71
  };
66
72
  }
67
73
  };
@@ -24,6 +24,11 @@ public:
24
24
  JsiSkData(std::shared_ptr<RNSkPlatformContext> context, sk_sp<SkData> asset)
25
25
  : JsiSkWrappingSkPtrHostObject(std::move(context), std::move(asset)) {}
26
26
 
27
+ size_t getMemoryPressure() const override {
28
+ auto data = getObject();
29
+ return data ? data->size() : 0;
30
+ }
31
+
27
32
  EXPORT_JSI_API_TYPENAME(JsiSkData, Data)
28
33
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkData, dispose))
29
34
  };
@@ -38,9 +38,10 @@ public:
38
38
  context = std::move(context),
39
39
  promise = std::move(promise),
40
40
  result = std::move(result)]() {
41
- promise->resolve(jsi::Object::createFromHostObject(
42
- runtime, std::make_shared<JsiSkData>(std::move(context),
43
- std::move(result))));
41
+ auto hostObjectInstance =
42
+ std::make_shared<JsiSkData>(context, std::move(result));
43
+ promise->resolve(JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
44
+ runtime, hostObjectInstance, context));
44
45
  });
45
46
  });
46
47
  });
@@ -55,8 +56,10 @@ public:
55
56
 
56
57
  auto data =
57
58
  SkData::MakeWithCopy(buffer.data(runtime), buffer.size(runtime));
58
- return jsi::Object::createFromHostObject(
59
- runtime, std::make_shared<JsiSkData>(getContext(), std::move(data)));
59
+ auto hostObjectInstance =
60
+ std::make_shared<JsiSkData>(getContext(), std::move(data));
61
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
62
+ runtime, hostObjectInstance, getContext());
60
63
  }
61
64
 
62
65
  JSI_HOST_FUNCTION(fromBase64) {
@@ -82,10 +85,14 @@ public:
82
85
  return jsi::Value::undefined();
83
86
  }
84
87
 
85
- return jsi::Object::createFromHostObject(
86
- runtime, std::make_shared<JsiSkData>(getContext(), std::move(data)));
88
+ auto hostObjectInstance =
89
+ std::make_shared<JsiSkData>(getContext(), std::move(data));
90
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
91
+ runtime, hostObjectInstance, getContext());
87
92
  }
88
93
 
94
+ size_t getMemoryPressure() const override { return 1024; }
95
+
89
96
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkDataFactory, fromURI),
90
97
  JSI_EXPORT_FUNC(JsiSkDataFactory, fromBytes),
91
98
  JSI_EXPORT_FUNC(JsiSkDataFactory, fromBase64))
@@ -91,8 +91,9 @@ public:
91
91
  getObject()->measureText(str.c_str(), str.length(), SkTextEncoding::kUTF8,
92
92
  &bounds);
93
93
  }
94
- return jsi::Object::createFromHostObject(
95
- runtime, std::make_shared<JsiSkRect>(getContext(), std::move(bounds)));
94
+ auto rect = std::make_shared<JsiSkRect>(getContext(), std::move(bounds));
95
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, rect,
96
+ getContext());
96
97
  }
97
98
 
98
99
  JSI_HOST_FUNCTION(getMetrics) {
@@ -278,6 +279,8 @@ public:
278
279
  : JsiSkWrappingSharedPtrHostObject(std::move(context),
279
280
  std::make_shared<SkFont>(font)) {}
280
281
 
282
+ size_t getMemoryPressure() const override { return sizeof(SkFont); }
283
+
281
284
  /**
282
285
  * Creates the function for construction a new instance of the SkFont
283
286
  * wrapper
@@ -292,18 +295,22 @@ public:
292
295
  if (count == 2) {
293
296
  auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
294
297
  auto size = arguments[1].asNumber();
295
- return jsi::Object::createFromHostObject(
296
- runtime, std::make_shared<JsiSkFont>(std::move(context),
297
- SkFont(typeface, size)));
298
+ auto hostObjectInstance =
299
+ std::make_shared<JsiSkFont>(context, SkFont(typeface, size));
300
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
301
+ runtime, hostObjectInstance, std::move(context));
298
302
  } else if (count == 1) {
299
303
  auto typeface = JsiSkTypeface::fromValue(runtime, arguments[0]);
300
- return jsi::Object::createFromHostObject(
301
- runtime,
302
- std::make_shared<JsiSkFont>(std::move(context), SkFont(typeface)));
304
+ auto hostObjectInstance =
305
+ std::make_shared<JsiSkFont>(context, SkFont(typeface));
306
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
307
+ runtime, hostObjectInstance, std::move(context));
303
308
  } else {
304
309
  // Return the newly constructed object
305
- return jsi::Object::createFromHostObject(
306
- runtime, std::make_shared<JsiSkFont>(std::move(context), SkFont()));
310
+ auto hostObjectInstance =
311
+ std::make_shared<JsiSkFont>(context, SkFont());
312
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
313
+ runtime, hostObjectInstance, std::move(context));
307
314
  }
308
315
  };
309
316
  }
@@ -42,10 +42,14 @@ public:
42
42
  auto name = arguments[0].asString(runtime).utf8(runtime);
43
43
  auto fontStyle = JsiSkFontStyle::fromValue(runtime, arguments[1]);
44
44
  auto typeface = getObject()->matchFamilyStyle(name.c_str(), *fontStyle);
45
- return jsi::Object::createFromHostObject(
46
- runtime, std::make_shared<JsiSkTypeface>(getContext(), typeface));
45
+ auto hostObjectInstance =
46
+ std::make_shared<JsiSkTypeface>(getContext(), std::move(typeface));
47
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
48
+ runtime, hostObjectInstance, getContext());
47
49
  }
48
50
 
51
+ size_t getMemoryPressure() const override { return 2048; }
52
+
49
53
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkFontMgr, countFamilies),
50
54
  JSI_EXPORT_FUNC(JsiSkFontMgr, getFamilyName),
51
55
  JSI_EXPORT_FUNC(JsiSkFontMgr, matchFamilyStyle))
@@ -32,12 +32,15 @@ public:
32
32
 
33
33
  JSI_HOST_FUNCTION(System) {
34
34
  auto fontMgr = JsiSkFontMgrFactory::getFontMgr(getContext());
35
- return jsi::Object::createFromHostObject(
36
- runtime, std::make_shared<JsiSkFontMgr>(getContext(), fontMgr));
35
+ auto fontMgrObj = std::make_shared<JsiSkFontMgr>(getContext(), fontMgr);
36
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, fontMgrObj,
37
+ getContext());
37
38
  }
38
39
 
39
40
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkFontMgrFactory, System))
40
41
 
42
+ size_t getMemoryPressure() const override { return 1024; }
43
+
41
44
  explicit JsiSkFontMgrFactory(std::shared_ptr<RNSkPlatformContext> context)
42
45
  : JsiSkHostObject(std::move(context)) {}
43
46
  };
@@ -56,15 +56,18 @@ public:
56
56
  }
57
57
  }
58
58
 
59
+ size_t getMemoryPressure() const override { return sizeof(SkFontStyle); }
60
+
59
61
  /**
60
62
  Returns the jsi object from a host object of this type
61
63
  */
62
64
  static jsi::Value toValue(jsi::Runtime &runtime,
63
65
  std::shared_ptr<RNSkPlatformContext> context,
64
66
  const SkFontStyle &fontStyle) {
65
- return jsi::Object::createFromHostObject(
66
- runtime,
67
- std::make_shared<JsiSkFontStyle>(std::move(context), fontStyle));
67
+ auto fontStyleObj =
68
+ std::make_shared<JsiSkFontStyle>(std::move(context), fontStyle);
69
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, fontStyleObj,
70
+ context);
68
71
  }
69
72
  };
70
73
  } // namespace RNSkia
@@ -23,6 +23,12 @@ public:
23
23
  explicit JsiSkHostObject(std::shared_ptr<RNSkPlatformContext> context)
24
24
  : _context(context) {}
25
25
 
26
+ /**
27
+ * Override this method to return the memory pressure for the wrapped object.
28
+ * @return The memory pressure in bytes
29
+ */
30
+ virtual size_t getMemoryPressure() const = 0;
31
+
26
32
  protected:
27
33
  /**
28
34
  * @return A pointer to the platform context
@@ -42,6 +48,18 @@ private:
42
48
  JSI_API_TYPENAME(TYPENAME) \
43
49
  JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(CLASS, __typename__))
44
50
 
51
+ #define JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE( \
52
+ runtime, hostObjectInstance, context) \
53
+ [&]() { \
54
+ auto result = \
55
+ jsi::Object::createFromHostObject(runtime, hostObjectInstance); \
56
+ auto memoryPressure = hostObjectInstance->getMemoryPressure(); \
57
+ if (memoryPressure > 0) { \
58
+ result.setExternalMemoryPressure(runtime, memoryPressure); \
59
+ } \
60
+ return result; \
61
+ }()
62
+
45
63
  template <typename T> class JsiSkWrappingHostObject : public JsiSkHostObject {
46
64
  public:
47
65
  /**
@@ -58,8 +76,8 @@ public:
58
76
  * Throws if the object has been disposed.
59
77
  * @return Underlying object
60
78
  */
61
- T getObject() { return validateObject(); }
62
- const T getObject() const { return validateObject(); }
79
+ T getObject() { return _object; }
80
+ const T getObject() const { return _object; }
63
81
 
64
82
  /**
65
83
  * Updates the inner object with a new version of the object.
@@ -71,44 +89,15 @@ public:
71
89
  * macro.
72
90
  */
73
91
  JSI_HOST_FUNCTION(dispose) {
74
- safeDispose();
92
+ // This is a no-op on native
75
93
  return jsi::Value::undefined();
76
94
  }
77
95
 
78
- protected:
79
- /**
80
- * Override to implement disposal of allocated resources like smart pointers.
81
- * This method will only be called once for each instance of this class.
82
- */
83
- virtual void releaseResources() = 0;
84
-
85
96
  private:
86
- /**
87
- * Validates that _object was not disposed and returns it.
88
- */
89
- T validateObject() const {
90
- if (_isDisposed) {
91
- throw std::runtime_error("Attempted to access a disposed object.");
92
- }
93
- return _object;
94
- }
95
-
96
- void safeDispose() {
97
- if (!_isDisposed) {
98
- _isDisposed = true;
99
- releaseResources();
100
- }
101
- }
102
-
103
97
  /**
104
98
  * Wrapped object.
105
99
  */
106
100
  T _object;
107
-
108
- /**
109
- * Resource disposed flag.
110
- */
111
- std::atomic<bool> _isDisposed = {false};
112
101
  };
113
102
 
114
103
  template <typename T>
@@ -129,12 +118,6 @@ public:
129
118
  obj.asObject(runtime).asHostObject(runtime))
130
119
  ->getObject();
131
120
  }
132
-
133
- protected:
134
- void releaseResources() override {
135
- // Clear internally allocated objects
136
- this->setObject(nullptr);
137
- }
138
121
  };
139
122
 
140
123
  template <typename T>
@@ -153,12 +136,6 @@ public:
153
136
  obj.asObject(runtime).asHostObject(runtime))
154
137
  ->getObject();
155
138
  }
156
-
157
- protected:
158
- void releaseResources() override {
159
- // Clear internally allocated objects
160
- this->setObject(nullptr);
161
- }
162
139
  };
163
140
 
164
141
  } // namespace RNSkia
@@ -83,9 +83,10 @@ public:
83
83
  : nullptr;
84
84
  auto shader =
85
85
  getObject()->makeShader(tmx, tmy, SkSamplingOptions(fm, mm), m);
86
- return jsi::Object::createFromHostObject(
87
- runtime,
88
- std::make_shared<JsiSkShader>(getContext(), std::move(shader)));
86
+ auto shaderObj =
87
+ std::make_shared<JsiSkShader>(getContext(), std::move(shader));
88
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, shaderObj,
89
+ getContext());
89
90
  }
90
91
 
91
92
  JSI_HOST_FUNCTION(makeShaderCubic) {
@@ -98,9 +99,10 @@ public:
98
99
  : nullptr;
99
100
  auto shader =
100
101
  getObject()->makeShader(tmx, tmy, SkSamplingOptions({B, C}), m);
101
- return jsi::Object::createFromHostObject(
102
- runtime,
103
- std::make_shared<JsiSkShader>(getContext(), std::move(shader)));
102
+ auto shaderObj =
103
+ std::make_shared<JsiSkShader>(getContext(), std::move(shader));
104
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(runtime, shaderObj,
105
+ getContext());
104
106
  }
105
107
 
106
108
  sk_sp<SkData> encodeImageData(const jsi::Value *arguments, size_t count) {
@@ -237,8 +239,10 @@ public:
237
239
  auto grContext = getContext()->getDirectContext();
238
240
  auto rasterImage = getObject()->makeRasterImage(grContext);
239
241
  #endif
240
- return jsi::Object::createFromHostObject(
241
- runtime, std::make_shared<JsiSkImage>(getContext(), rasterImage));
242
+ auto hostObjectInstance =
243
+ std::make_shared<JsiSkImage>(getContext(), std::move(rasterImage));
244
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
245
+ runtime, hostObjectInstance, getContext());
242
246
  }
243
247
 
244
248
  JSI_HOST_FUNCTION(getNativeTextureUnstable) {
@@ -268,6 +272,11 @@ public:
268
272
  const sk_sp<SkImage> image)
269
273
  : JsiSkWrappingSkPtrHostObject<SkImage>(std::move(context),
270
274
  std::move(image)) {}
275
+
276
+ size_t getMemoryPressure() const override {
277
+ auto image = getObject();
278
+ return image ? image->imageInfo().computeMinByteSize() : 0;
279
+ }
271
280
  };
272
281
 
273
282
  } // namespace RNSkia
@@ -18,8 +18,10 @@ namespace jsi = facebook::jsi;
18
18
  class JsiSkImageFactory : public JsiSkHostObject {
19
19
  public:
20
20
  JSI_HOST_FUNCTION(MakeNull) {
21
- return jsi::Object::createFromHostObject(
22
- runtime, std::make_shared<JsiSkImage>(getContext(), nullptr));
21
+ auto hostObjectInstance =
22
+ std::make_shared<JsiSkImage>(getContext(), nullptr);
23
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
24
+ runtime, hostObjectInstance, getContext());
23
25
  }
24
26
 
25
27
  JSI_HOST_FUNCTION(MakeImageFromEncoded) {
@@ -28,8 +30,10 @@ public:
28
30
  if (image == nullptr) {
29
31
  return jsi::Value::null();
30
32
  }
31
- return jsi::Object::createFromHostObject(
32
- runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
33
+ auto hostObjectInstance =
34
+ std::make_shared<JsiSkImage>(getContext(), std::move(image));
35
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
36
+ runtime, hostObjectInstance, getContext());
33
37
  }
34
38
 
35
39
  JSI_HOST_FUNCTION(MakeImageFromNativeBuffer) {
@@ -40,8 +44,10 @@ public:
40
44
  if (image == nullptr) {
41
45
  throw std::runtime_error("Failed to convert NativeBuffer to SkImage!");
42
46
  }
43
- return jsi::Object::createFromHostObject(
44
- runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
47
+ auto hostObjectInstance =
48
+ std::make_shared<JsiSkImage>(getContext(), std::move(image));
49
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
50
+ runtime, hostObjectInstance, getContext());
45
51
  }
46
52
 
47
53
  JSI_HOST_FUNCTION(MakeImage) {
@@ -52,8 +58,10 @@ public:
52
58
  if (image == nullptr) {
53
59
  return jsi::Value::null();
54
60
  }
55
- return jsi::Object::createFromHostObject(
56
- runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
61
+ auto hostObjectInstance =
62
+ std::make_shared<JsiSkImage>(getContext(), std::move(image));
63
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
64
+ runtime, hostObjectInstance, getContext());
57
65
  }
58
66
 
59
67
  JSI_HOST_FUNCTION(MakeImageFromViewTag) {
@@ -75,9 +83,10 @@ public:
75
83
  promise->reject("Failed to create image from view tag");
76
84
  return;
77
85
  }
78
- promise->resolve(jsi::Object::createFromHostObject(
79
- runtime, std::make_shared<JsiSkImage>(
80
- std::move(context), std::move(result))));
86
+ auto hostObjectInstance =
87
+ std::make_shared<JsiSkImage>(context, std::move(result));
88
+ promise->resolve(JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
89
+ runtime, hostObjectInstance, context));
81
90
  });
82
91
  });
83
92
  });
@@ -98,10 +107,14 @@ public:
98
107
  jsiImage->setObject(image);
99
108
  return jsi::Value(runtime, arguments[4]);
100
109
  }
101
- return jsi::Object::createFromHostObject(
102
- runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
110
+ auto hostObjectInstance =
111
+ std::make_shared<JsiSkImage>(getContext(), std::move(image));
112
+ return JSI_CREATE_HOST_OBJECT_WITH_MEMORY_PRESSURE(
113
+ runtime, hostObjectInstance, getContext());
103
114
  }
104
115
 
116
+ size_t getMemoryPressure() const override { return 1024; }
117
+
105
118
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImageFromEncoded),
106
119
  JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImageFromViewTag),
107
120
  JSI_EXPORT_FUNC(JsiSkImageFactory,