@shopify/react-native-skia 1.10.2 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. package/cpp/api/JsiSkApi.h +3 -0
  2. package/cpp/api/recorder/ColorFilters.h +133 -0
  3. package/cpp/api/recorder/Command.h +58 -0
  4. package/cpp/api/recorder/Convertor.h +1212 -0
  5. package/cpp/api/recorder/DataTypes.h +234 -0
  6. package/cpp/api/recorder/DrawingCtx.h +187 -0
  7. package/cpp/api/recorder/Drawings.h +949 -0
  8. package/cpp/api/recorder/Image.h +108 -0
  9. package/cpp/api/recorder/ImageFilters.h +292 -0
  10. package/cpp/api/recorder/JsiRecorder.h +314 -0
  11. package/cpp/api/recorder/Paint.h +191 -0
  12. package/cpp/api/recorder/PathEffects.h +194 -0
  13. package/cpp/api/recorder/RNRecorder.h +635 -0
  14. package/cpp/api/recorder/Shaders.h +406 -0
  15. package/cpp/rnskia/dom/nodes/JsiAtlasNode.h +3 -2
  16. package/cpp/rnskia/dom/nodes/JsiImageNode.h +3 -2
  17. package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +4 -8
  18. package/jestSetup.js +8 -0
  19. package/jestSetup.mjs +8 -0
  20. package/lib/commonjs/renderer/components/image/Image.d.ts +1 -1
  21. package/lib/commonjs/renderer/components/image/Image.js +8 -2
  22. package/lib/commonjs/renderer/components/image/Image.js.map +1 -1
  23. package/lib/commonjs/skia/types/Recorder.d.ts +52 -0
  24. package/lib/commonjs/skia/types/Recorder.js +6 -0
  25. package/lib/commonjs/skia/types/Recorder.js.map +1 -0
  26. package/lib/commonjs/skia/types/Skia.d.ts +2 -0
  27. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  28. package/lib/commonjs/skia/types/index.d.ts +1 -0
  29. package/lib/commonjs/skia/types/index.js +11 -0
  30. package/lib/commonjs/skia/types/index.js.map +1 -1
  31. package/lib/commonjs/skia/web/JsiSkia.js +3 -0
  32. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  33. package/lib/commonjs/sksg/Container.d.ts +6 -1
  34. package/lib/commonjs/sksg/Container.js +59 -2
  35. package/lib/commonjs/sksg/Container.js.map +1 -1
  36. package/lib/commonjs/sksg/Recorder/DrawingContext.js +1 -0
  37. package/lib/commonjs/sksg/Recorder/DrawingContext.js.map +1 -1
  38. package/lib/commonjs/sksg/Recorder/ReanimatedRecorder.d.ts +53 -0
  39. package/lib/commonjs/sksg/Recorder/ReanimatedRecorder.js +189 -0
  40. package/lib/commonjs/sksg/Recorder/ReanimatedRecorder.js.map +1 -0
  41. package/lib/commonjs/sksg/Recorder/Recorder.d.ts +2 -2
  42. package/lib/commonjs/sksg/Recorder/Recorder.js.map +1 -1
  43. package/lib/commonjs/sksg/Recorder/Visitor.d.ts +2 -2
  44. package/lib/commonjs/sksg/Recorder/Visitor.js +2 -2
  45. package/lib/commonjs/sksg/Recorder/Visitor.js.map +1 -1
  46. package/lib/module/renderer/components/image/Image.d.ts +1 -1
  47. package/lib/module/renderer/components/image/Image.js +8 -2
  48. package/lib/module/renderer/components/image/Image.js.map +1 -1
  49. package/lib/module/skia/types/Recorder.d.ts +52 -0
  50. package/lib/module/skia/types/Recorder.js +2 -0
  51. package/lib/module/skia/types/Recorder.js.map +1 -0
  52. package/lib/module/skia/types/Skia.d.ts +2 -0
  53. package/lib/module/skia/types/Skia.js.map +1 -1
  54. package/lib/module/skia/types/index.d.ts +1 -0
  55. package/lib/module/skia/types/index.js +1 -0
  56. package/lib/module/skia/types/index.js.map +1 -1
  57. package/lib/module/skia/web/JsiSkia.js +3 -0
  58. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  59. package/lib/module/sksg/Container.d.ts +6 -1
  60. package/lib/module/sksg/Container.js +59 -2
  61. package/lib/module/sksg/Container.js.map +1 -1
  62. package/lib/module/sksg/Recorder/DrawingContext.js +1 -0
  63. package/lib/module/sksg/Recorder/DrawingContext.js.map +1 -1
  64. package/lib/module/sksg/Recorder/ReanimatedRecorder.d.ts +53 -0
  65. package/lib/module/sksg/Recorder/ReanimatedRecorder.js +182 -0
  66. package/lib/module/sksg/Recorder/ReanimatedRecorder.js.map +1 -0
  67. package/lib/module/sksg/Recorder/Recorder.d.ts +2 -2
  68. package/lib/module/sksg/Recorder/Recorder.js.map +1 -1
  69. package/lib/module/sksg/Recorder/Visitor.d.ts +2 -2
  70. package/lib/module/sksg/Recorder/Visitor.js +2 -2
  71. package/lib/module/sksg/Recorder/Visitor.js.map +1 -1
  72. package/lib/typescript/lib/commonjs/renderer/components/image/Image.d.ts +4 -1
  73. package/lib/typescript/lib/commonjs/skia/types/Recorder.d.ts +1 -0
  74. package/lib/typescript/lib/commonjs/skia/web/JsiSkia.d.ts +1 -0
  75. package/lib/typescript/lib/commonjs/sksg/Container.d.ts +5 -1
  76. package/lib/typescript/lib/commonjs/sksg/Reconciler.d.ts +6 -0
  77. package/lib/typescript/lib/commonjs/sksg/Recorder/ReanimatedRecorder.d.ts +47 -0
  78. package/lib/typescript/lib/module/mock/index.d.ts +4 -1
  79. package/lib/typescript/lib/module/renderer/components/image/Image.d.ts +4 -1
  80. package/lib/typescript/lib/module/skia/Skia.web.d.ts +1 -0
  81. package/lib/typescript/lib/module/skia/types/Recorder.d.ts +1 -0
  82. package/lib/typescript/lib/module/skia/types/index.d.ts +1 -0
  83. package/lib/typescript/lib/module/skia/web/JsiSkia.d.ts +1 -0
  84. package/lib/typescript/lib/module/sksg/Container.d.ts +5 -1
  85. package/lib/typescript/lib/module/sksg/Reconciler.d.ts +6 -0
  86. package/lib/typescript/lib/module/sksg/Recorder/ReanimatedRecorder.d.ts +46 -0
  87. package/lib/typescript/src/renderer/components/image/Image.d.ts +1 -1
  88. package/lib/typescript/src/skia/types/Recorder.d.ts +52 -0
  89. package/lib/typescript/src/skia/types/Skia.d.ts +2 -0
  90. package/lib/typescript/src/skia/types/index.d.ts +1 -0
  91. package/lib/typescript/src/sksg/Container.d.ts +6 -1
  92. package/lib/typescript/src/sksg/Recorder/ReanimatedRecorder.d.ts +53 -0
  93. package/lib/typescript/src/sksg/Recorder/Recorder.d.ts +2 -2
  94. package/lib/typescript/src/sksg/Recorder/Visitor.d.ts +2 -2
  95. package/package.json +3 -2
  96. package/src/renderer/components/image/Image.tsx +2 -2
  97. package/src/skia/types/Recorder.ts +91 -0
  98. package/src/skia/types/Skia.ts +2 -0
  99. package/src/skia/types/index.ts +1 -0
  100. package/src/skia/web/JsiSkia.ts +3 -0
  101. package/src/sksg/Container.ts +63 -4
  102. package/src/sksg/Recorder/DrawingContext.ts +1 -0
  103. package/src/sksg/Recorder/ReanimatedRecorder.ts +271 -0
  104. package/src/sksg/Recorder/Recorder.ts +2 -2
  105. package/src/sksg/Recorder/Visitor.ts +17 -12
@@ -0,0 +1,191 @@
1
+ #pragma once
2
+
3
+ #include <optional>
4
+ #include <string>
5
+ #include <variant>
6
+
7
+ #include "Command.h"
8
+ #include "Convertor.h"
9
+ #include "DrawingCtx.h"
10
+
11
+ namespace RNSkia {
12
+
13
+ struct TransformProps {
14
+ std::optional<SkM44> transform;
15
+ std::optional<SkPoint> origin;
16
+ std::optional<SkMatrix> matrix;
17
+ };
18
+
19
+ // TODO: can we avoid a copy here?
20
+ SkMatrix processTransform(std::optional<SkMatrix> &matrix,
21
+ std::optional<SkM44> &transform,
22
+ std::optional<SkPoint> &origin) {
23
+ SkMatrix m3;
24
+ if (matrix.has_value()) {
25
+ m3 = matrix.value();
26
+ if (origin.has_value()) {
27
+ m3.postTranslate(origin.value().x(), origin.value().y());
28
+ m3.preTranslate(-origin.value().x(), -origin.value().y());
29
+ }
30
+ } else if (transform.has_value()) {
31
+ auto m4 = transform.value();
32
+ if (origin.has_value()) {
33
+ m4.postTranslate(origin.value().x(), origin.value().y());
34
+ m4.preTranslate(-origin.value().x(), -origin.value().y());
35
+ }
36
+ m3 = m4.asM33();
37
+ }
38
+ return m3;
39
+ }
40
+
41
+ struct CTMCmdProps : TransformProps {
42
+ std::optional<ClipDef> clip;
43
+ std::optional<bool> invertClip;
44
+ std::optional<Layer> layer;
45
+ };
46
+
47
+ class SaveCTMCmd : public Command {
48
+ private:
49
+ CTMCmdProps props;
50
+
51
+ public:
52
+ SaveCTMCmd(jsi::Runtime &runtime, const jsi::Object &object,
53
+ Variables &variables)
54
+ : Command(CommandType::SaveCTM) {
55
+ convertProperty(runtime, object, "transform", props.transform, variables);
56
+ convertProperty(runtime, object, "origin", props.origin, variables);
57
+ convertProperty(runtime, object, "matrix", props.matrix, variables);
58
+ convertProperty(runtime, object, "clip", props.clip, variables);
59
+ convertProperty(runtime, object, "invertClip", props.invertClip, variables);
60
+ convertProperty(runtime, object, "layer", props.layer, variables);
61
+ }
62
+
63
+ void saveCTM(DrawingCtx *ctx) {
64
+ auto clip = props.clip;
65
+ auto invertClip = props.invertClip;
66
+ auto layer = props.layer;
67
+ auto hasTransform = props.matrix.has_value() || props.transform.has_value();
68
+ auto hasClip = clip.has_value();
69
+ auto op = invertClip.has_value() && invertClip.value()
70
+ ? SkClipOp::kDifference
71
+ : SkClipOp::kIntersect;
72
+ auto shouldSave = hasTransform || hasClip || layer.has_value();
73
+ SkMatrix m3 = processTransform(props.matrix, props.transform, props.origin);
74
+ if (shouldSave) {
75
+ if (layer.has_value()) {
76
+ if (std::holds_alternative<bool>(layer.value())) {
77
+ ctx->canvas->saveLayer(nullptr, nullptr);
78
+ } else {
79
+ auto paint = std::get<SkPaint>(layer.value());
80
+ ctx->canvas->saveLayer(nullptr, &paint);
81
+ }
82
+ } else {
83
+ ctx->canvas->save();
84
+ }
85
+ ctx->canvas->concat(m3);
86
+ }
87
+ if (clip.has_value()) {
88
+ auto c = clip.value();
89
+ if (std::holds_alternative<SkPath>(c)) {
90
+ auto path = std::get<SkPath>(c);
91
+ ctx->canvas->clipPath(path, op);
92
+ } else if (std::holds_alternative<std::string>(c)) {
93
+ auto pathString = std::get<std::string>(c);
94
+ SkPath result;
95
+ if (SkParsePath::FromSVGString(pathString.c_str(), &result)) {
96
+ ctx->canvas->clipPath(result, op);
97
+ } else {
98
+ throw std::runtime_error("Could not parse path from string.");
99
+ }
100
+ } else if (std::holds_alternative<SkRect>(c)) {
101
+ auto rect = std::get<SkRect>(c);
102
+ ctx->canvas->clipRect(rect, op);
103
+ } else if (std::holds_alternative<SkRRect>(c)) {
104
+ auto rrect = std::get<SkRRect>(c);
105
+ ctx->canvas->clipRRect(rrect, op);
106
+ }
107
+ }
108
+ }
109
+ };
110
+
111
+ struct PaintCmdProps {
112
+ std::optional<SkColor> color;
113
+ std::optional<SkBlendMode> blendMode;
114
+ std::optional<SkPaint::Style> style;
115
+ std::optional<SkPaint::Join> strokeJoin;
116
+ std::optional<SkPaint::Cap> strokeCap;
117
+ std::optional<float> strokeMiter;
118
+ std::optional<float> strokeWidth;
119
+ std::optional<float> opacity;
120
+ std::optional<bool> antiAlias;
121
+ std::optional<bool> dither;
122
+ std::optional<SkPaint> paint;
123
+ };
124
+
125
+ class SavePaintCmd : public Command {
126
+ private:
127
+ PaintCmdProps props;
128
+
129
+ public:
130
+ SavePaintCmd(jsi::Runtime &runtime, const jsi::Object &object,
131
+ Variables &variables)
132
+ : Command(CommandType::SavePaint) {
133
+ convertProperty(runtime, object, "color", props.color, variables);
134
+ convertProperty(runtime, object, "blendMode", props.blendMode, variables);
135
+ convertProperty(runtime, object, "style", props.style, variables);
136
+ convertProperty(runtime, object, "strokeJoin", props.strokeJoin, variables);
137
+ convertProperty(runtime, object, "strokeCap", props.strokeCap, variables);
138
+ convertProperty(runtime, object, "strokeMiter", props.strokeMiter,
139
+ variables);
140
+ convertProperty(runtime, object, "strokeWidth", props.strokeWidth,
141
+ variables);
142
+ convertProperty(runtime, object, "opacity", props.opacity, variables);
143
+ convertProperty(runtime, object, "antiAlias", props.antiAlias, variables);
144
+ convertProperty(runtime, object, "dither", props.dither, variables);
145
+ convertProperty(runtime, object, "paint", props.paint, variables);
146
+ }
147
+
148
+ void savePaint(DrawingCtx *ctx) {
149
+ if (props.paint.has_value()) {
150
+ ctx->pushPaint(props.paint.value());
151
+ return;
152
+ }
153
+ ctx->savePaint();
154
+ auto &paint = ctx->getPaint();
155
+ if (props.opacity.has_value()) {
156
+ paint.setAlphaf(paint.getAlphaf() * props.opacity.value());
157
+ }
158
+ if (props.color.has_value()) {
159
+ auto currentOpacity = paint.getAlphaf();
160
+ paint.setShader(nullptr);
161
+ paint.setColor(props.color.value());
162
+ paint.setAlphaf(currentOpacity * paint.getAlphaf());
163
+ }
164
+ if (props.blendMode.has_value()) {
165
+ paint.setBlendMode(props.blendMode.value());
166
+ }
167
+ if (props.style.has_value()) {
168
+ paint.setStyle(props.style.value());
169
+ }
170
+ if (props.strokeJoin.has_value()) {
171
+ paint.setStrokeJoin(props.strokeJoin.value());
172
+ }
173
+ if (props.strokeCap.has_value()) {
174
+ paint.setStrokeCap(props.strokeCap.value());
175
+ }
176
+ if (props.strokeMiter.has_value()) {
177
+ paint.setStrokeMiter(props.strokeMiter.value());
178
+ }
179
+ if (props.strokeWidth.has_value()) {
180
+ paint.setStrokeWidth(props.strokeWidth.value());
181
+ }
182
+ if (props.antiAlias.has_value()) {
183
+ paint.setAntiAlias(props.antiAlias.value());
184
+ }
185
+ if (props.dither.has_value()) {
186
+ paint.setDither(props.dither.value());
187
+ }
188
+ }
189
+ };
190
+
191
+ } // namespace RNSkia
@@ -0,0 +1,194 @@
1
+ #pragma once
2
+
3
+ #include <optional>
4
+ #include <string>
5
+ #include <variant>
6
+
7
+ #include "Command.h"
8
+ #include "Convertor.h"
9
+ #include "DrawingCtx.h"
10
+
11
+ namespace RNSkia {
12
+
13
+ struct DiscretePathEffectProps {
14
+ float length;
15
+ float deviation;
16
+ uint32_t seed;
17
+ };
18
+
19
+ class DiscretePathEffectCmd : public Command {
20
+ private:
21
+ DiscretePathEffectProps props;
22
+
23
+ public:
24
+ DiscretePathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
25
+ Variables &variables)
26
+ : Command(CommandType::PushPathEffect, "skDiscretePathEffect") {
27
+ convertProperty(runtime, object, "length", props.length, variables);
28
+ convertProperty(runtime, object, "deviation", props.deviation, variables);
29
+ convertProperty(runtime, object, "seed", props.seed, variables);
30
+ }
31
+
32
+ void pushPathEffect(DrawingCtx *ctx) {
33
+ auto pe =
34
+ SkDiscretePathEffect::Make(props.length, props.deviation, props.seed);
35
+ ctx->pathEffects.push_back(pe);
36
+ }
37
+ };
38
+
39
+ struct DashPathEffectProps {
40
+ std::vector<float> intervals;
41
+ float phase = 0;
42
+ };
43
+
44
+ class DashPathEffectCmd : public Command {
45
+ private:
46
+ DashPathEffectProps props;
47
+
48
+ public:
49
+ DashPathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
50
+ Variables &variables)
51
+ : Command(CommandType::PushPathEffect, "skDashPathEffect") {
52
+ convertProperty(runtime, object, "intervals", props.intervals, variables);
53
+ convertProperty(runtime, object, "phase", props.phase, variables);
54
+ }
55
+
56
+ void pushPathEffect(DrawingCtx *ctx) {
57
+ auto pe = SkDashPathEffect::Make(props.intervals.data(),
58
+ props.intervals.size(), props.phase);
59
+ ctx->pathEffects.push_back(pe);
60
+ }
61
+ };
62
+
63
+ struct Path1DPathEffectProps {
64
+ SkPath path;
65
+ float advance;
66
+ float phase;
67
+ SkPath1DPathEffect::Style style;
68
+ };
69
+
70
+ class Path1DPathEffectCmd : public Command {
71
+ private:
72
+ Path1DPathEffectProps props;
73
+
74
+ public:
75
+ Path1DPathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
76
+ Variables &variables)
77
+ : Command(CommandType::PushPathEffect, "skPath1DPathEffect") {
78
+ convertProperty(runtime, object, "path", props.path, variables);
79
+ convertProperty(runtime, object, "advance", props.advance, variables);
80
+ convertProperty(runtime, object, "phase", props.phase, variables);
81
+ convertProperty(runtime, object, "style", props.style, variables);
82
+ }
83
+
84
+ void pushPathEffect(DrawingCtx *ctx) {
85
+ auto pe = SkPath1DPathEffect::Make(props.path, props.advance, props.phase,
86
+ props.style);
87
+ if (pe) {
88
+ ctx->pathEffects.push_back(pe);
89
+ }
90
+ }
91
+ };
92
+
93
+ struct Path2DPathEffectProps {
94
+ SkMatrix matrix;
95
+ SkPath path;
96
+ };
97
+
98
+ class Path2DPathEffectCmd : public Command {
99
+ private:
100
+ Path2DPathEffectProps props;
101
+
102
+ public:
103
+ Path2DPathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
104
+ Variables &variables)
105
+ : Command(CommandType::PushPathEffect, "skPath2DPathEffect") {
106
+ convertProperty(runtime, object, "matrix", props.matrix, variables);
107
+ convertProperty(runtime, object, "path", props.path, variables);
108
+ }
109
+
110
+ void pushPathEffect(DrawingCtx *ctx) {
111
+ auto pe = SkPath2DPathEffect::Make(props.matrix, props.path);
112
+ if (pe) {
113
+ ctx->pathEffects.push_back(pe);
114
+ }
115
+ }
116
+ };
117
+
118
+ struct CornerPathEffectProps {
119
+ float r;
120
+ };
121
+
122
+ class CornerPathEffectCmd : public Command {
123
+ private:
124
+ CornerPathEffectProps props;
125
+
126
+ public:
127
+ CornerPathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
128
+ Variables &variables)
129
+ : Command(CommandType::PushPathEffect, "skCornerPathEffect") {
130
+ convertProperty(runtime, object, "r", props.r, variables);
131
+ }
132
+
133
+ void pushPathEffect(DrawingCtx *ctx) {
134
+ auto pe = SkCornerPathEffect::Make(props.r);
135
+ if (pe) {
136
+ ctx->pathEffects.push_back(pe);
137
+ }
138
+ }
139
+ };
140
+
141
+ class SumPathEffectCmd : public Command {
142
+ public:
143
+ SumPathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
144
+ Variables &variables)
145
+ : Command(CommandType::PushPathEffect, "skSumPathEffect") {}
146
+
147
+ void pushPathEffect(DrawingCtx *ctx) {
148
+ // Get all path effects
149
+ auto effects = std::move(ctx->pathEffects);
150
+ ctx->pathEffects.clear(); // Clear the vector since we moved from it
151
+
152
+ // Create composer function for path effects
153
+ Composer<SkPathEffect> pathEffectComposer = [](sk_sp<SkPathEffect> outer,
154
+ sk_sp<SkPathEffect> inner) {
155
+ return SkPathEffect::MakeSum(outer, inner);
156
+ };
157
+
158
+ // Compose all effects
159
+ auto composedEffect =
160
+ composeEffects<SkPathEffect>(effects, pathEffectComposer);
161
+
162
+ // If we got a valid composed effect, push it back
163
+ if (composedEffect) {
164
+ ctx->pathEffects.push_back(std::move(composedEffect));
165
+ }
166
+ }
167
+ };
168
+
169
+ struct Line2DPathEffectProps {
170
+ float width;
171
+ SkMatrix matrix;
172
+ };
173
+
174
+ class Line2DPathEffectCmd : public Command {
175
+ private:
176
+ Line2DPathEffectProps props;
177
+
178
+ public:
179
+ Line2DPathEffectCmd(jsi::Runtime &runtime, const jsi::Object &object,
180
+ Variables &variables)
181
+ : Command(CommandType::PushPathEffect, "skLine2DPathEffect") {
182
+ convertProperty(runtime, object, "width", props.width, variables);
183
+ convertProperty(runtime, object, "matrix", props.matrix, variables);
184
+ }
185
+
186
+ void pushPathEffect(DrawingCtx *ctx) {
187
+ auto pe = SkLine2DPathEffect::Make(props.width, props.matrix);
188
+ if (pe) {
189
+ ctx->pathEffects.push_back(pe);
190
+ }
191
+ }
192
+ };
193
+
194
+ } // namespace RNSkia