@shopify/react-native-skia 2.2.14 → 2.2.16

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 (92) hide show
  1. package/apple/MetalContext.h +3 -25
  2. package/apple/MetalContext.mm +7 -3
  3. package/apple/MetalWindowContext.mm +11 -1
  4. package/apple/RNSkMetalCanvasProvider.mm +3 -6
  5. package/cpp/api/JsiSkImage.h +16 -1
  6. package/cpp/api/JsiSkSurface.h +17 -1
  7. package/cpp/api/JsiSkThreadSafeDeletion.h +105 -0
  8. package/cpp/api/recorder/Drawings.h +7 -7
  9. package/cpp/jsi/JsiHostObject.cpp +0 -27
  10. package/cpp/jsi/JsiHostObject.h +5 -5
  11. package/cpp/jsi/ViewProperty.h +34 -2
  12. package/cpp/rnskia/RNSkJsiViewApi.h +14 -24
  13. package/cpp/rnskia/RNSkPictureView.h +25 -2
  14. package/cpp/rnskia/RNSkView.h +7 -0
  15. package/lib/commonjs/Platform/Platform.js +2 -1
  16. package/lib/commonjs/Platform/Platform.js.map +1 -1
  17. package/lib/commonjs/Platform/Platform.web.js +3 -0
  18. package/lib/commonjs/Platform/Platform.web.js.map +1 -1
  19. package/lib/commonjs/__tests__/globalSetup.d.ts +1 -1
  20. package/lib/commonjs/renderer/Canvas.d.ts +1 -1
  21. package/lib/commonjs/renderer/Canvas.js +24 -1
  22. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  23. package/lib/commonjs/renderer/__tests__/setup.d.ts +1 -1
  24. package/lib/commonjs/skia/core/SVG.web.js +1 -1
  25. package/lib/commonjs/skia/core/SVG.web.js.map +1 -1
  26. package/lib/commonjs/skia/types/Data/Data.d.ts +6 -1
  27. package/lib/commonjs/skia/types/Data/Data.js.map +1 -1
  28. package/lib/commonjs/sksg/Container.d.ts +2 -3
  29. package/lib/commonjs/sksg/Container.js +1 -1
  30. package/lib/commonjs/sksg/Container.js.map +1 -1
  31. package/lib/commonjs/sksg/Container.native.d.ts +3 -5
  32. package/lib/commonjs/sksg/Container.native.js +8 -15
  33. package/lib/commonjs/sksg/Container.native.js.map +1 -1
  34. package/lib/commonjs/sksg/Container.web.d.ts +2 -3
  35. package/lib/commonjs/sksg/Container.web.js +1 -1
  36. package/lib/commonjs/sksg/Container.web.js.map +1 -1
  37. package/lib/commonjs/sksg/Reconciler.d.ts +2 -3
  38. package/lib/commonjs/sksg/Reconciler.js +2 -2
  39. package/lib/commonjs/sksg/Reconciler.js.map +1 -1
  40. package/lib/module/Platform/Platform.js +2 -1
  41. package/lib/module/Platform/Platform.js.map +1 -1
  42. package/lib/module/Platform/Platform.web.js +3 -0
  43. package/lib/module/Platform/Platform.web.js.map +1 -1
  44. package/lib/module/__tests__/globalSetup.d.ts +1 -1
  45. package/lib/module/renderer/Canvas.d.ts +1 -1
  46. package/lib/module/renderer/Canvas.js +24 -1
  47. package/lib/module/renderer/Canvas.js.map +1 -1
  48. package/lib/module/renderer/__tests__/setup.d.ts +1 -1
  49. package/lib/module/skia/core/SVG.web.js +1 -1
  50. package/lib/module/skia/core/SVG.web.js.map +1 -1
  51. package/lib/module/skia/types/Data/Data.d.ts +6 -1
  52. package/lib/module/skia/types/Data/Data.js.map +1 -1
  53. package/lib/module/sksg/Container.d.ts +2 -3
  54. package/lib/module/sksg/Container.js +1 -1
  55. package/lib/module/sksg/Container.js.map +1 -1
  56. package/lib/module/sksg/Container.native.d.ts +3 -5
  57. package/lib/module/sksg/Container.native.js +8 -15
  58. package/lib/module/sksg/Container.native.js.map +1 -1
  59. package/lib/module/sksg/Container.web.d.ts +2 -3
  60. package/lib/module/sksg/Container.web.js +1 -1
  61. package/lib/module/sksg/Container.web.js.map +1 -1
  62. package/lib/module/sksg/Reconciler.d.ts +2 -3
  63. package/lib/module/sksg/Reconciler.js +2 -2
  64. package/lib/module/sksg/Reconciler.js.map +1 -1
  65. package/lib/typescript/lib/commonjs/sksg/Container.d.ts +1 -1
  66. package/lib/typescript/lib/commonjs/sksg/Container.native.d.ts +2 -3
  67. package/lib/typescript/lib/commonjs/sksg/Container.web.d.ts +1 -1
  68. package/lib/typescript/lib/commonjs/sksg/Reconciler.d.ts +1 -1
  69. package/lib/typescript/lib/module/sksg/Container.d.ts +1 -1
  70. package/lib/typescript/lib/module/sksg/Container.native.d.ts +2 -3
  71. package/lib/typescript/lib/module/sksg/Container.web.d.ts +1 -1
  72. package/lib/typescript/lib/module/sksg/Reconciler.d.ts +1 -1
  73. package/lib/typescript/src/__tests__/globalSetup.d.ts +1 -1
  74. package/lib/typescript/src/renderer/Canvas.d.ts +1 -1
  75. package/lib/typescript/src/renderer/__tests__/setup.d.ts +1 -1
  76. package/lib/typescript/src/skia/types/Data/Data.d.ts +6 -1
  77. package/lib/typescript/src/sksg/Container.d.ts +2 -3
  78. package/lib/typescript/src/sksg/Container.native.d.ts +3 -5
  79. package/lib/typescript/src/sksg/Container.web.d.ts +2 -3
  80. package/lib/typescript/src/sksg/Reconciler.d.ts +2 -3
  81. package/package.json +1 -1
  82. package/src/Platform/Platform.ts +3 -0
  83. package/src/Platform/Platform.web.tsx +3 -0
  84. package/src/__tests__/globalSetup.ts +5 -3
  85. package/src/renderer/Canvas.tsx +27 -5
  86. package/src/renderer/__tests__/setup.tsx +1 -1
  87. package/src/skia/core/SVG.web.ts +3 -0
  88. package/src/skia/types/Data/Data.ts +7 -1
  89. package/src/sksg/Container.native.ts +10 -27
  90. package/src/sksg/Container.ts +2 -8
  91. package/src/sksg/Container.web.ts +2 -8
  92. package/src/sksg/Reconciler.ts +3 -4
@@ -1,14 +1,12 @@
1
- import type { SharedValue } from "react-native-reanimated";
2
- import type { Skia, SkSize } from "../skia/types";
1
+ import type { Skia } from "../skia/types";
3
2
  import { Container, StaticContainer } from "./StaticContainer";
4
3
  import "../skia/NativeSetup";
5
4
  import "../views/api";
6
5
  declare class NativeReanimatedContainer extends Container {
7
6
  private nativeId;
8
- private onSize?;
9
7
  private mapperId;
10
- constructor(Skia: Skia, nativeId: number, onSize?: SharedValue<SkSize> | undefined);
8
+ constructor(Skia: Skia, nativeId: number);
11
9
  redraw(): void;
12
10
  }
13
- export declare const createContainer: (Skia: Skia, nativeId: number, onSize?: SharedValue<SkSize>) => StaticContainer | NativeReanimatedContainer;
11
+ export declare const createContainer: (Skia: Skia, nativeId: number) => StaticContainer | NativeReanimatedContainer;
14
12
  export {};
@@ -1,5 +1,4 @@
1
- import type { SharedValue } from "react-native-reanimated";
2
- import type { Skia, SkSize } from "../skia/types";
1
+ import type { Skia } from "../skia/types";
3
2
  import { Container, StaticContainer } from "./StaticContainer";
4
3
  import "../skia/NativeSetup";
5
4
  import "../views/api";
@@ -9,5 +8,5 @@ declare class ReanimatedContainer extends Container {
9
8
  constructor(Skia: Skia, nativeId: number);
10
9
  redraw(): void;
11
10
  }
12
- export declare const createContainer: (Skia: Skia, nativeId: number, _onSize?: SharedValue<SkSize>) => StaticContainer | ReanimatedContainer;
11
+ export declare const createContainer: (Skia: Skia, nativeId: number) => StaticContainer | ReanimatedContainer;
13
12
  export {};
@@ -1,13 +1,12 @@
1
1
  import type { ReactNode } from "react";
2
- import type { SharedValue } from "react-native-reanimated";
3
- import type { SkCanvas, Skia, SkSize } from "../skia/types";
2
+ import type { SkCanvas, Skia } from "../skia/types";
4
3
  import { NodeType } from "../dom/types";
5
4
  import "./Elements";
6
5
  export declare class SkiaSGRoot {
7
6
  Skia: Skia;
8
7
  private root;
9
8
  private container;
10
- constructor(Skia: Skia, nativeId?: number, onSize?: SharedValue<SkSize>);
9
+ constructor(Skia: Skia, nativeId?: number);
11
10
  get sg(): {
12
11
  type: NodeType;
13
12
  props: {};
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "setup-skia-web": "scripts/setup-canvaskit.js"
9
9
  },
10
10
  "title": "React Native Skia",
11
- "version": "2.2.14",
11
+ "version": "2.2.16",
12
12
  "description": "High-performance React Native Graphics using Skia",
13
13
  "main": "lib/module/index.js",
14
14
  "react-native": "src/index.ts",
@@ -15,8 +15,11 @@ export const Platform: IPlatform = {
15
15
  OS: RNPlatform.OS,
16
16
  PixelRatio: PixelRatio.get(),
17
17
  resolveAsset: (source: DataModule) => {
18
+ // eslint-disable-next-line no-nested-ternary
18
19
  return isRNModule(source)
19
20
  ? Image.resolveAssetSource(source).uri
21
+ : "uri" in source
22
+ ? source.uri
20
23
  : source.default;
21
24
  },
22
25
  findNodeHandle,
@@ -146,6 +146,9 @@ export const Platform: IPlatform = {
146
146
  "Asset source is a number - this is not supported on the web"
147
147
  );
148
148
  }
149
+ if ("uri" in source) {
150
+ return source.uri;
151
+ }
149
152
  return source.default;
150
153
  },
151
154
  findNodeHandle: () => {
@@ -4,12 +4,14 @@ import { WebSocketServer } from "ws";
4
4
  declare global {
5
5
  var testServer: Server;
6
6
  var testClient: WebSocket;
7
- var testOS: "ios" | "android" | "web" | "node";
7
+ var testOS: "ios" | "android" | "web" | "node" | "macos";
8
8
  var testArch: "paper" | "fabric";
9
9
  }
10
10
 
11
- const isOS = (os: string): os is "android" | "ios" | "web" => {
12
- return ["ios", "android", "web"].indexOf(os) !== -1;
11
+ const isOS = (
12
+ os: string
13
+ ): os is "android" | "ios" | "web" | "node" | "macos" => {
14
+ return ["ios", "android", "web", "node", "macos"].indexOf(os) !== -1;
13
15
  };
14
16
 
15
17
  const isArch = (arc: string): arc is "paper" | "fabric" => {
@@ -15,14 +15,16 @@ import type {
15
15
  View,
16
16
  ViewProps,
17
17
  } from "react-native";
18
- import { type SharedValue } from "react-native-reanimated";
18
+ import type { SharedValue } from "react-native-reanimated";
19
19
 
20
+ import Rea from "../external/reanimated/ReanimatedProxy";
20
21
  import { SkiaViewNativeId } from "../views/SkiaViewNativeId";
21
22
  import SkiaPictureViewNativeComponent from "../specs/SkiaPictureViewNativeComponent";
22
23
  import type { SkImage, SkRect, SkSize } from "../skia/types";
23
24
  import { SkiaSGRoot } from "../sksg/Reconciler";
24
25
  import { Skia } from "../skia";
25
26
  import { Platform } from "../Platform";
27
+ import { HAS_REANIMATED_3 } from "../external";
26
28
 
27
29
  export interface CanvasRef extends FC<CanvasProps> {
28
30
  makeImageSnapshot(rect?: SkRect): SkImage;
@@ -35,6 +37,8 @@ export interface CanvasRef extends FC<CanvasProps> {
35
37
 
36
38
  export const useCanvasRef = () => useRef<CanvasRef>(null);
37
39
 
40
+ const useReanimatedFrame = !HAS_REANIMATED_3 ? () => {} : Rea.useFrameCallback;
41
+
38
42
  export const useCanvasSize = (userRef?: RefObject<CanvasRef | null>) => {
39
43
  const ourRef = useCanvasRef();
40
44
  const ref = userRef ?? ourRef;
@@ -88,10 +92,28 @@ export const Canvas = ({
88
92
  }, []);
89
93
 
90
94
  // Root
91
- const root = useMemo(
92
- () => new SkiaSGRoot(Skia, nativeId, onSize),
93
- [nativeId, onSize]
94
- );
95
+ const root = useMemo(() => new SkiaSGRoot(Skia, nativeId), [nativeId]);
96
+
97
+ useReanimatedFrame(() => {
98
+ "worklet";
99
+ }, !!onSize);
100
+ useEffect(() => {
101
+ if (onSize) {
102
+ Rea.runOnUI(() => {
103
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
104
+ // @ts-expect-error
105
+ global[`__onSize_${nativeId}`] = onSize;
106
+ })();
107
+ return () => {
108
+ Rea.runOnUI(() => {
109
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
110
+ // @ts-expect-error
111
+ delete global[`__onSize_${nativeId}`];
112
+ })();
113
+ };
114
+ }
115
+ return undefined;
116
+ }, [onSize, nativeId]);
95
117
 
96
118
  // Render effects
97
119
  useLayoutEffect(() => {
@@ -21,7 +21,7 @@ import { SkiaObject } from "./e2e/setup";
21
21
 
22
22
  jest.setTimeout(180 * 1000);
23
23
 
24
- type TestOS = "ios" | "android" | "web" | "node";
24
+ type TestOS = "ios" | "android" | "web" | "node" | "macos";
25
25
 
26
26
  declare global {
27
27
  var testServer: Server;
@@ -11,6 +11,9 @@ export const useSVG = (
11
11
  if (
12
12
  typeof source !== "object" ||
13
13
  source instanceof Uint8Array ||
14
+ !("uri" in source) ||
15
+ typeof source.uri !== "string" ||
16
+ !("default" in source) ||
14
17
  typeof source.default !== "string"
15
18
  ) {
16
19
  throw new Error(
@@ -7,7 +7,13 @@ type ESModule = {
7
7
  __esModule: true;
8
8
  default: string;
9
9
  };
10
- export type DataModule = RNModule | ESModule;
10
+ type MetroAsset = {
11
+ uri: string;
12
+ width: number;
13
+ height: number;
14
+ };
15
+
16
+ export type DataModule = RNModule | ESModule | MetroAsset;
11
17
  export type DataSource = DataModule | string | Uint8Array;
12
18
  export type DataSourceParam = DataSource | null | undefined;
13
19
 
@@ -1,7 +1,5 @@
1
- import type { SharedValue } from "react-native-reanimated";
2
-
3
1
  import Rea from "../external/reanimated/ReanimatedProxy";
4
- import type { Skia, SkSize } from "../skia/types";
2
+ import type { Skia } from "../skia/types";
5
3
  import { HAS_REANIMATED_3 } from "../external/reanimated/renderHelpers";
6
4
  import type { JsiRecorder } from "../skia/types/Recorder";
7
5
 
@@ -12,15 +10,9 @@ import { visit } from "./Recorder/Visitor";
12
10
  import "../skia/NativeSetup";
13
11
  import "../views/api";
14
12
 
15
- const nativeDrawOnscreen = (
16
- nativeId: number,
17
- recorder: JsiRecorder,
18
- onSize?: SharedValue<SkSize>
19
- ) => {
13
+ const nativeDrawOnscreen = (nativeId: number, recorder: JsiRecorder) => {
20
14
  "worklet";
21
- if (onSize) {
22
- SkiaViewApi.setJsiProperty(nativeId, "onSize", onSize);
23
- }
15
+
24
16
  //const start = performance.now();
25
17
  const picture = recorder.play();
26
18
  //const end = performance.now();
@@ -31,11 +23,7 @@ const nativeDrawOnscreen = (
31
23
  class NativeReanimatedContainer extends Container {
32
24
  private mapperId: number | null = null;
33
25
 
34
- constructor(
35
- Skia: Skia,
36
- private nativeId: number,
37
- private onSize?: SharedValue<SkSize>
38
- ) {
26
+ constructor(Skia: Skia, private nativeId: number) {
39
27
  super(Skia);
40
28
  }
41
29
 
@@ -51,28 +39,23 @@ class NativeReanimatedContainer extends Container {
51
39
  visit(recorder, this.root);
52
40
  const sharedValues = recorder.getSharedValues();
53
41
  const sharedRecorder = recorder.getRecorder();
54
- Rea.runOnUI((onSize?: SharedValue<SkSize>) => {
42
+ Rea.runOnUI(() => {
55
43
  "worklet";
56
- nativeDrawOnscreen(nativeId, sharedRecorder, onSize);
57
- })(this.onSize);
44
+ nativeDrawOnscreen(nativeId, sharedRecorder);
45
+ })();
58
46
  if (sharedValues.length > 0) {
59
- const { onSize } = this;
60
47
  this.mapperId = Rea.startMapper(() => {
61
48
  "worklet";
62
49
  sharedRecorder.applyUpdates(sharedValues);
63
- nativeDrawOnscreen(nativeId, sharedRecorder, onSize);
50
+ nativeDrawOnscreen(nativeId, sharedRecorder);
64
51
  }, sharedValues);
65
52
  }
66
53
  }
67
54
  }
68
55
 
69
- export const createContainer = (
70
- Skia: Skia,
71
- nativeId: number,
72
- onSize?: SharedValue<SkSize>
73
- ) => {
56
+ export const createContainer = (Skia: Skia, nativeId: number) => {
74
57
  if (HAS_REANIMATED_3 && nativeId !== -1) {
75
- return new NativeReanimatedContainer(Skia, nativeId, onSize);
58
+ return new NativeReanimatedContainer(Skia, nativeId);
76
59
  } else {
77
60
  return new StaticContainer(Skia, nativeId);
78
61
  }
@@ -1,13 +1,7 @@
1
- import type { SharedValue } from "react-native-reanimated";
2
-
3
- import type { Skia, SkSize } from "../skia/types";
1
+ import type { Skia } from "../skia/types";
4
2
 
5
3
  import { StaticContainer } from "./StaticContainer";
6
4
 
7
- export const createContainer = (
8
- Skia: Skia,
9
- nativeId: number,
10
- _onSize?: SharedValue<SkSize>
11
- ) => {
5
+ export const createContainer = (Skia: Skia, nativeId: number) => {
12
6
  return new StaticContainer(Skia, nativeId);
13
7
  };
@@ -1,7 +1,5 @@
1
- import type { SharedValue } from "react-native-reanimated";
2
-
3
1
  import Rea from "../external/reanimated/ReanimatedProxy";
4
- import type { Skia, SkSize } from "../skia/types";
2
+ import type { Skia } from "../skia/types";
5
3
  import { HAS_REANIMATED_3 } from "../external/reanimated/renderHelpers";
6
4
 
7
5
  import type { Recording } from "./Recorder/Recorder";
@@ -64,11 +62,7 @@ class ReanimatedContainer extends Container {
64
62
  }
65
63
  }
66
64
 
67
- export const createContainer = (
68
- Skia: Skia,
69
- nativeId: number,
70
- _onSize?: SharedValue<SkSize>
71
- ) => {
65
+ export const createContainer = (Skia: Skia, nativeId: number) => {
72
66
  if (HAS_REANIMATED_3 && nativeId !== -1) {
73
67
  return new ReanimatedContainer(Skia, nativeId);
74
68
  } else {
@@ -1,9 +1,8 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { OpaqueRoot } from "react-reconciler";
3
- import type { SharedValue } from "react-native-reanimated";
4
3
  import ReactReconciler from "react-reconciler";
5
4
 
6
- import type { SkCanvas, Skia, SkSize } from "../skia/types";
5
+ import type { SkCanvas, Skia } from "../skia/types";
7
6
  import { NodeType } from "../dom/types";
8
7
 
9
8
  import { debug, sksgHostConfig } from "./HostConfig";
@@ -24,8 +23,8 @@ export class SkiaSGRoot {
24
23
  private root: OpaqueRoot;
25
24
  private container: Container;
26
25
 
27
- constructor(public Skia: Skia, nativeId = -1, onSize?: SharedValue<SkSize>) {
28
- this.container = createContainer(Skia, nativeId, onSize);
26
+ constructor(public Skia: Skia, nativeId = -1) {
27
+ this.container = createContainer(Skia, nativeId);
29
28
  this.root = skiaReconciler.createContainer(
30
29
  this.container,
31
30
  0,