@shopify/react-native-skia 2.13.1 → 2.14.0

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 (103) hide show
  1. package/android/CMakeLists.txt +2 -0
  2. package/android/cpp/jni/include/JniSkiaPictureView.h +16 -15
  3. package/apple/RNSkApplePlatformContext.h +3 -4
  4. package/apple/RNSkiaModule.h +11 -3
  5. package/apple/RNSkiaModule.mm +46 -19
  6. package/apple/SkiaPictureView.mm +2 -2
  7. package/apple/SkiaUIView.h +0 -1
  8. package/apple/SkiaUIView.mm +1 -1
  9. package/apple/ViewScreenshotService.h +2 -2
  10. package/apple/ViewScreenshotService.mm +5 -4
  11. package/cpp/api/JsiSkPath.h +0 -431
  12. package/cpp/api/JsiSkSkottie.h +2 -3
  13. package/cpp/api/JsiSkStrutStyle.h +1 -1
  14. package/cpp/api/JsiSkSurfaceFactory.h +41 -0
  15. package/cpp/api/recorder/Convertor.h +152 -131
  16. package/cpp/api/recorder/DataTypes.h +11 -11
  17. package/cpp/api/recorder/ImageFit.h +8 -5
  18. package/cpp/api/recorder/JsiRecorder.h +9 -29
  19. package/cpp/api/recorder/Paint.h +3 -3
  20. package/cpp/api/recorder/RNRecorder.h +56 -0
  21. package/cpp/jsi/ViewProperty.cpp +23 -0
  22. package/cpp/jsi/ViewProperty.h +31 -10
  23. package/cpp/rnskia/RNDawnContext.h +33 -0
  24. package/cpp/rnskia/RNSkJsiViewApi.h +69 -2
  25. package/cpp/rnskia/RNSkPictureView.cpp +58 -0
  26. package/cpp/rnskia/RNSkPictureView.h +110 -24
  27. package/lib/commonjs/Platform/IPlatform.js.map +1 -1
  28. package/lib/commonjs/animation/functions/interpolate.js.map +1 -1
  29. package/lib/commonjs/skia/core/Image.js.map +1 -1
  30. package/lib/commonjs/skia/types/NativeBuffer/NativeBufferFactory.js.map +1 -1
  31. package/lib/commonjs/skia/types/Path/Path.d.ts +2 -336
  32. package/lib/commonjs/skia/types/Path/Path.js.map +1 -1
  33. package/lib/commonjs/skia/types/Recorder.d.ts +10 -0
  34. package/lib/commonjs/skia/types/Recorder.js.map +1 -1
  35. package/lib/commonjs/skia/types/Shader/Shader.js.map +1 -1
  36. package/lib/commonjs/skia/types/Surface/SurfaceFactory.d.ts +16 -0
  37. package/lib/commonjs/skia/types/Surface/SurfaceFactory.js.map +1 -1
  38. package/lib/commonjs/skia/web/JsiSkPath.d.ts +4 -38
  39. package/lib/commonjs/skia/web/JsiSkPath.js +3 -275
  40. package/lib/commonjs/skia/web/JsiSkPath.js.map +1 -1
  41. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.d.ts +2 -1
  42. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.js +3 -0
  43. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.js.map +1 -1
  44. package/lib/commonjs/sksg/Container.native.d.ts +15 -1
  45. package/lib/commonjs/sksg/Container.native.js +34 -33
  46. package/lib/commonjs/sksg/Container.native.js.map +1 -1
  47. package/lib/commonjs/specs/NativeSkiaModule.web.js +4 -0
  48. package/lib/commonjs/specs/NativeSkiaModule.web.js.map +1 -1
  49. package/lib/commonjs/views/types.d.ts +9 -0
  50. package/lib/commonjs/views/types.js.map +1 -1
  51. package/lib/module/Platform/IPlatform.js.map +1 -1
  52. package/lib/module/animation/functions/interpolate.js.map +1 -1
  53. package/lib/module/skia/core/Image.js.map +1 -1
  54. package/lib/module/skia/types/NativeBuffer/NativeBufferFactory.js.map +1 -1
  55. package/lib/module/skia/types/Path/Path.d.ts +2 -336
  56. package/lib/module/skia/types/Path/Path.js.map +1 -1
  57. package/lib/module/skia/types/Recorder.d.ts +10 -0
  58. package/lib/module/skia/types/Recorder.js.map +1 -1
  59. package/lib/module/skia/types/Shader/Shader.js.map +1 -1
  60. package/lib/module/skia/types/Surface/SurfaceFactory.d.ts +16 -0
  61. package/lib/module/skia/types/Surface/SurfaceFactory.js.map +1 -1
  62. package/lib/module/skia/web/JsiSkPath.d.ts +4 -38
  63. package/lib/module/skia/web/JsiSkPath.js +4 -276
  64. package/lib/module/skia/web/JsiSkPath.js.map +1 -1
  65. package/lib/module/skia/web/JsiSkSurfaceFactory.d.ts +2 -1
  66. package/lib/module/skia/web/JsiSkSurfaceFactory.js +4 -1
  67. package/lib/module/skia/web/JsiSkSurfaceFactory.js.map +1 -1
  68. package/lib/module/sksg/Container.native.d.ts +15 -1
  69. package/lib/module/sksg/Container.native.js +34 -33
  70. package/lib/module/sksg/Container.native.js.map +1 -1
  71. package/lib/module/specs/NativeSkiaModule.web.js +4 -0
  72. package/lib/module/specs/NativeSkiaModule.web.js.map +1 -1
  73. package/lib/module/views/types.d.ts +9 -0
  74. package/lib/module/views/types.js.map +1 -1
  75. package/lib/typescript/lib/commonjs/skia/web/JsiSkPath.d.ts +1 -35
  76. package/lib/typescript/lib/commonjs/skia/web/JsiSkSurfaceFactory.d.ts +1 -0
  77. package/lib/typescript/lib/commonjs/sksg/Container.native.d.ts +15 -1
  78. package/lib/typescript/lib/module/skia/web/JsiSkPath.d.ts +3 -37
  79. package/lib/typescript/lib/module/skia/web/JsiSkSurfaceFactory.d.ts +1 -0
  80. package/lib/typescript/lib/module/sksg/Container.native.d.ts +15 -1
  81. package/lib/typescript/src/skia/types/Path/Path.d.ts +2 -336
  82. package/lib/typescript/src/skia/types/Recorder.d.ts +10 -0
  83. package/lib/typescript/src/skia/types/Surface/SurfaceFactory.d.ts +16 -0
  84. package/lib/typescript/src/skia/web/JsiSkPath.d.ts +4 -38
  85. package/lib/typescript/src/skia/web/JsiSkSurfaceFactory.d.ts +2 -1
  86. package/lib/typescript/src/sksg/Container.native.d.ts +15 -1
  87. package/lib/typescript/src/views/types.d.ts +9 -0
  88. package/package.json +2 -2
  89. package/src/Platform/IPlatform.ts +1 -4
  90. package/src/animation/functions/interpolate.ts +1 -4
  91. package/src/skia/core/Image.ts +1 -2
  92. package/src/skia/types/NativeBuffer/NativeBufferFactory.ts +2 -5
  93. package/src/skia/types/Path/Path.ts +2 -420
  94. package/src/skia/types/Recorder.ts +10 -0
  95. package/src/skia/types/Shader/Shader.ts +1 -5
  96. package/src/skia/types/Surface/SurfaceFactory.ts +17 -0
  97. package/src/skia/web/JsiSkPath.ts +4 -495
  98. package/src/skia/web/JsiSkSurfaceFactory.ts +6 -2
  99. package/src/sksg/Container.native.ts +34 -37
  100. package/src/specs/NativeSkiaModule.web.ts +4 -0
  101. package/src/views/types.ts +13 -0
  102. package/apple/SkiaManager.h +0 -23
  103. package/apple/SkiaManager.mm +0 -63
@@ -81,8 +81,10 @@ add_library(
81
81
  "${PROJECT_SOURCE_DIR}/../cpp/jsi/JSICache.cpp"
82
82
  "${PROJECT_SOURCE_DIR}/../cpp/jsi/JsiPromises.cpp"
83
83
  "${PROJECT_SOURCE_DIR}/../cpp/jsi/Promise.cpp"
84
+ "${PROJECT_SOURCE_DIR}/../cpp/jsi/ViewProperty.cpp"
84
85
 
85
86
  "${PROJECT_SOURCE_DIR}/../cpp/rnskia/RNSkManager.cpp"
87
+ "${PROJECT_SOURCE_DIR}/../cpp/rnskia/RNSkPictureView.cpp"
86
88
 
87
89
  "${PROJECT_SOURCE_DIR}/../cpp/api/third_party/CSSColorParser.cpp"
88
90
  "${PROJECT_SOURCE_DIR}/../cpp/api/third_party/base64.cpp"
@@ -69,8 +69,8 @@ protected:
69
69
  highBitDepth);
70
70
  }
71
71
 
72
- void surfaceSizeChanged(jobject surface, int width, int height, bool isSurface,
73
- bool highBitDepth) override {
72
+ void surfaceSizeChanged(jobject surface, int width, int height,
73
+ bool isSurface, bool highBitDepth) override {
74
74
  JniSkiaBaseView::surfaceSizeChanged(surface, width, height, isSurface,
75
75
  highBitDepth);
76
76
  }
@@ -83,7 +83,18 @@ protected:
83
83
  JniSkiaBaseView::registerView(nativeId);
84
84
  }
85
85
 
86
- void unregisterView() override { JniSkiaBaseView::unregisterView(); }
86
+ void unregisterView() override {
87
+ JniSkiaBaseView::unregisterView();
88
+ // React drops the Java view here, but the native view behind it (and the
89
+ // recording it owns) is only destroyed when the Java object is finalized.
90
+ // Release the content now so it does not wait for the Java garbage
91
+ // collector.
92
+ if (_skiaAndroidView != nullptr) {
93
+ auto renderer = std::static_pointer_cast<RNSkia::RNSkPictureRenderer>(
94
+ _skiaAndroidView->getSkiaView()->getRenderer());
95
+ renderer->clear();
96
+ }
97
+ }
87
98
 
88
99
  jni::local_ref<jni::JArrayInt> getBitmap(int width, int height) override {
89
100
  // Get the RNSkPictureView from the android view
@@ -101,9 +112,6 @@ protected:
101
112
  return jni::JArrayInt::newArray(0);
102
113
  }
103
114
 
104
- // Get the SkPicture from the renderer
105
- sk_sp<SkPicture> picture = renderer->getPicture();
106
-
107
115
  const size_t pixelCount =
108
116
  static_cast<size_t>(width) * static_cast<size_t>(height);
109
117
  if (pixelCount == 0) {
@@ -126,15 +134,8 @@ protected:
126
134
  return jni::JArrayInt::newArray(0);
127
135
  }
128
136
 
129
- canvas->clear(SK_ColorTRANSPARENT);
130
-
131
- if (picture) {
132
- auto pd = pictureView->getPixelDensity();
133
- canvas->save();
134
- canvas->scale(pd, pd);
135
- canvas->drawPicture(picture);
136
- canvas->restore();
137
- }
137
+ // Draws whatever the view currently shows: a picture or a recorder.
138
+ renderer->drawInto(canvas, pictureView->getPixelDensity());
138
139
 
139
140
  sk_sp<SkImage> snapshot = surface->makeImageSnapshot();
140
141
  if (!snapshot) {
@@ -1,7 +1,6 @@
1
1
  #pragma once
2
2
 
3
- #import <React/RCTBridge+Private.h>
4
- #import <React/RCTBridge.h>
3
+ #import <React/RCTBridgeModule.h>
5
4
 
6
5
  #include <functional>
7
6
  #include <memory>
@@ -21,7 +20,7 @@ namespace RNSkia {
21
20
  class RNSkApplePlatformContext : public RNSkPlatformContext {
22
21
  public:
23
22
  RNSkApplePlatformContext(
24
- RCTBridge *bridge,
23
+ RCTViewRegistry *viewRegistry,
25
24
  std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
26
25
  #if !TARGET_OS_OSX
27
26
  : RNSkPlatformContext(jsCallInvoker, [[UIScreen mainScreen] scale]) {
@@ -32,7 +31,7 @@ public:
32
31
 
33
32
  // Create screenshot manager
34
33
  _screenshotService =
35
- [[ViewScreenshotService alloc] initWithUiManager:bridge.uiManager];
34
+ [[ViewScreenshotService alloc] initWithViewRegistry:viewRegistry];
36
35
  }
37
36
 
38
37
  ~RNSkApplePlatformContext() = default;
@@ -1,11 +1,19 @@
1
1
  #pragma once
2
2
 
3
+ #import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
3
4
  #import <rnskia/rnskia.h>
4
5
 
5
- #include "SkiaManager.h"
6
+ #include <memory>
6
7
 
7
- @interface RNSkiaModule : NSObject <NativeSkiaModuleSpec>
8
+ #include "RNSkManager.h"
8
9
 
9
- - (SkiaManager *)manager;
10
+ @interface RNSkiaModule
11
+ : NSObject <NativeSkiaModuleSpec, RCTTurboModuleWithJSIBindings>
12
+
13
+ - (std::shared_ptr<RNSkia::RNSkManager>)skManager;
14
+
15
+ // Fabric components do not have a better way to interact with TurboModules.
16
+ // Workaround to get the SkManager instance from singleton.
17
+ + (std::shared_ptr<RNSkia::RNSkManager>)latestActiveSkManager;
10
18
 
11
19
  @end
@@ -1,52 +1,79 @@
1
-
2
1
  #import "RNSkiaModule.h"
2
+
3
3
  #import <ReactCommon/RCTTurboModule.h>
4
4
 
5
+ #import "RNSkApplePlatformContext.h"
6
+
7
+ static __weak RNSkiaModule *sharedInstance = nil;
8
+
5
9
  @implementation RNSkiaModule {
6
- SkiaManager *skiaManager;
7
- std::shared_ptr<facebook::react::CallInvoker> jsInvoker;
10
+ // Handed to us by React Native when JS first requires the module (see
11
+ // installJSIBindingsWithRuntime:callInvoker:), which always happens before
12
+ // JS calls install(). Valid until invalidate.
13
+ facebook::jsi::Runtime *_runtime;
14
+ std::shared_ptr<facebook::react::CallInvoker> _jsInvoker;
15
+ std::shared_ptr<RNSkia::RNSkManager> _skManager;
8
16
  }
9
17
 
10
18
  RCT_EXPORT_MODULE()
11
- @synthesize bridge = _bridge;
19
+ // Injected by React Native (bridgeless included). It is how a module looks up
20
+ // a native view by react tag without touching RCTBridge or RCTUIManager.
21
+ @synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
22
+
23
+ #pragma mark Accessors
12
24
 
13
- #pragma Accessors
25
+ - (std::shared_ptr<RNSkia::RNSkManager>)skManager {
26
+ return _skManager;
27
+ }
14
28
 
15
- - (SkiaManager *)manager {
16
- return skiaManager;
29
+ + (std::shared_ptr<RNSkia::RNSkManager>)latestActiveSkManager {
30
+ if (sharedInstance != nil) {
31
+ return [sharedInstance skManager];
32
+ }
33
+ return nullptr;
17
34
  }
18
35
 
19
- #pragma Setup and invalidation
36
+ #pragma mark Setup and invalidation
20
37
 
21
38
  + (BOOL)requiresMainQueueSetup {
22
39
  return YES;
23
40
  }
24
41
 
42
+ - (void)
43
+ installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime
44
+ callInvoker:
45
+ (const std::shared_ptr<facebook::react::CallInvoker>
46
+ &)callInvoker {
47
+ _runtime = &runtime;
48
+ _jsInvoker = callInvoker;
49
+ }
50
+
25
51
  - (void)invalidate {
26
- if (skiaManager != nil) {
27
- [skiaManager invalidate];
28
- }
29
- skiaManager = nil;
52
+ _skManager = nullptr;
53
+ _runtime = nullptr;
54
+ _jsInvoker = nullptr;
30
55
  }
31
56
 
32
57
  RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
33
- if (skiaManager != nil) {
58
+ if (_skManager != nullptr) {
34
59
  // Already initialized, ignore call.
35
60
  return @true;
36
61
  }
37
- if (!jsInvoker) {
38
- NSLog(@"[RNSkiaModule] Failed to install SkiaManager: jsInvoker is not "
39
- @"initialized.");
62
+ if (_runtime == nullptr || _jsInvoker == nullptr) {
63
+ NSLog(@"[RNSkiaModule] Failed to install: the JS runtime has not been "
64
+ @"provided to the module yet.");
40
65
  return @false;
41
66
  }
42
- skiaManager = [[SkiaManager alloc] initWithBridge:self.bridge
43
- jsInvoker:jsInvoker];
67
+ sharedInstance = self;
68
+ _skManager = std::make_shared<RNSkia::RNSkManager>(
69
+ _runtime, _jsInvoker,
70
+ std::make_shared<RNSkia::RNSkApplePlatformContext>(
71
+ self.viewRegistry_DEPRECATED, _jsInvoker));
44
72
  return @true;
45
73
  }
46
74
 
47
75
  - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
48
76
  (const facebook::react::ObjCTurboModule::InitParams &)params {
49
- jsInvoker = params.jsInvoker;
50
77
  return std::make_shared<facebook::react::NativeSkiaModuleSpecJSI>(params);
51
78
  }
52
79
 
@@ -5,10 +5,10 @@
5
5
  #import "RNSkPlatformContext.h"
6
6
 
7
7
  #import "RNSkiaModule.h"
8
- #import "SkiaManager.h"
9
8
  #import "SkiaUIView.h"
10
9
 
11
10
  #import <React/RCTConversions.h>
11
+ #import <React/RCTConvert.h>
12
12
  #import <React/RCTFabricComponentsPlugins.h>
13
13
 
14
14
  #import <react/renderer/components/rnskia/ComponentDescriptors.h>
@@ -23,7 +23,7 @@ using namespace facebook::react;
23
23
  - (instancetype)initWithFrame:(CGRect)frame {
24
24
  if (self = [super initWithFrame:frame]) {
25
25
  // Pass SkManager as a raw pointer to avoid circular dependencies
26
- auto skManager = [SkiaManager latestActiveSkManager].get();
26
+ auto skManager = [RNSkiaModule latestActiveSkManager].get();
27
27
  [self initCommon:skManager
28
28
  factory:[](std::shared_ptr<RNSkia::RNSkPlatformContext> context) {
29
29
  return std::make_shared<RNSkAppleView<RNSkia::RNSkPictureView>>(
@@ -14,7 +14,6 @@
14
14
 
15
15
  #import "RNSkAppleView.h"
16
16
  #import "RNSkManager.h"
17
- #import "SkiaManager.h"
18
17
 
19
18
  @interface SkiaUIView : RCTViewComponentView
20
19
 
@@ -88,7 +88,7 @@
88
88
  // this flag is only set when the view is inserted and we want to set the
89
89
  // manager here since the view could be recycled or the app could be
90
90
  // refreshed and we would have a stale manager then
91
- _manager = [SkiaManager latestActiveSkManager].get();
91
+ _manager = [RNSkiaModule latestActiveSkManager].get();
92
92
  }
93
93
  }
94
94
 
@@ -7,7 +7,7 @@
7
7
  #import <Appkit/Appkit.h>
8
8
  #endif // !TARGET_OS_OSX
9
9
 
10
- #import <React/RCTUIManager.h>
10
+ #import <React/RCTBridgeModule.h>
11
11
 
12
12
  #pragma clang diagnostic push
13
13
  #pragma clang diagnostic ignored "-Wdocumentation"
@@ -19,7 +19,7 @@
19
19
  @interface ViewScreenshotService : NSObject {
20
20
  }
21
21
 
22
- - (instancetype)initWithUiManager:(RCTUIManager *)uiManager;
22
+ - (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry;
23
23
  - (sk_sp<SkImage>)screenshotOfViewWithTag:(NSNumber *)viewTag;
24
24
 
25
25
  @end
@@ -1,5 +1,6 @@
1
1
  #import "ViewScreenshotService.h"
2
2
  #import <QuartzCore/QuartzCore.h>
3
+ #import <React/RCTUtils.h>
3
4
 
4
5
  #pragma clang diagnostic push
5
6
  #pragma clang diagnostic ignored "-Wdocumentation"
@@ -9,12 +10,12 @@
9
10
  #pragma clang diagnostic pop
10
11
 
11
12
  @implementation ViewScreenshotService {
12
- RCTUIManager *_uiManager;
13
+ __weak RCTViewRegistry *_viewRegistry;
13
14
  }
14
15
 
15
- - (instancetype)initWithUiManager:(RCTUIManager *)uiManager {
16
+ - (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry {
16
17
  if (self = [super init]) {
17
- _uiManager = uiManager;
18
+ _viewRegistry = viewRegistry;
18
19
  }
19
20
  return self;
20
21
  }
@@ -22,7 +23,7 @@
22
23
  - (sk_sp<SkImage>)screenshotOfViewWithTag:(NSNumber *)viewTag {
23
24
  #if !TARGET_OS_OSX
24
25
  // Find view corresponding to the tag
25
- auto view = [_uiManager viewForReactTag:viewTag];
26
+ auto view = [_viewRegistry viewForReactTag:viewTag];
26
27
  if (view == NULL) {
27
28
  RCTFatal(RCTErrorWithMessage(@"Could not find view with tag"));
28
29
  return nullptr;