@shopify/react-native-skia 2.7.0 → 2.8.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 (110) hide show
  1. package/cpp/api/JsiSkParagraph.h +114 -0
  2. package/lib/commonjs/external/reanimated/useVideoLoading.js +11 -2
  3. package/lib/commonjs/external/reanimated/useVideoLoading.js.map +1 -1
  4. package/lib/commonjs/skia/core/SVG.web.js +4 -5
  5. package/lib/commonjs/skia/core/SVG.web.js.map +1 -1
  6. package/lib/commonjs/skia/types/Paragraph/Paragraph.d.ts +62 -0
  7. package/lib/commonjs/skia/types/Paragraph/Paragraph.js.map +1 -1
  8. package/lib/commonjs/skia/web/Host.js +7 -0
  9. package/lib/commonjs/skia/web/Host.js.map +1 -1
  10. package/lib/commonjs/skia/web/JsiSkPaint.js +5 -0
  11. package/lib/commonjs/skia/web/JsiSkPaint.js.map +1 -1
  12. package/lib/commonjs/skia/web/JsiSkParagraph.d.ts +3 -1
  13. package/lib/commonjs/skia/web/JsiSkParagraph.js +6 -0
  14. package/lib/commonjs/skia/web/JsiSkParagraph.js.map +1 -1
  15. package/lib/commonjs/sksg/Container.web.js +34 -3
  16. package/lib/commonjs/sksg/Container.web.js.map +1 -1
  17. package/lib/commonjs/sksg/Recorder/DrawingContext.d.ts +2 -0
  18. package/lib/commonjs/sksg/Recorder/DrawingContext.js +22 -6
  19. package/lib/commonjs/sksg/Recorder/DrawingContext.js.map +1 -1
  20. package/lib/commonjs/sksg/Recorder/FrameScope.d.ts +18 -0
  21. package/lib/commonjs/sksg/Recorder/FrameScope.js +66 -0
  22. package/lib/commonjs/sksg/Recorder/FrameScope.js.map +1 -0
  23. package/lib/commonjs/sksg/Recorder/FrameScope.native.d.ts +10 -0
  24. package/lib/commonjs/sksg/Recorder/FrameScope.native.js +26 -0
  25. package/lib/commonjs/sksg/Recorder/FrameScope.native.js.map +1 -0
  26. package/lib/commonjs/sksg/Recorder/Player.js +1 -2
  27. package/lib/commonjs/sksg/Recorder/Player.js.map +1 -1
  28. package/lib/commonjs/sksg/Recorder/Recorder.d.ts +3 -1
  29. package/lib/commonjs/sksg/Recorder/Recorder.js +12 -1
  30. package/lib/commonjs/sksg/Recorder/Recorder.js.map +1 -1
  31. package/lib/commonjs/sksg/Recorder/commands/Drawing.js +3 -3
  32. package/lib/commonjs/sksg/Recorder/commands/Drawing.js.map +1 -1
  33. package/lib/commonjs/sksg/Recorder/commands/Shaders.js +3 -3
  34. package/lib/commonjs/sksg/Recorder/commands/Shaders.js.map +1 -1
  35. package/lib/commonjs/sksg/StaticContainer.d.ts +1 -0
  36. package/lib/commonjs/sksg/StaticContainer.js +27 -1
  37. package/lib/commonjs/sksg/StaticContainer.js.map +1 -1
  38. package/lib/module/external/reanimated/useVideoLoading.js +12 -2
  39. package/lib/module/external/reanimated/useVideoLoading.js.map +1 -1
  40. package/lib/module/skia/core/SVG.web.js +4 -5
  41. package/lib/module/skia/core/SVG.web.js.map +1 -1
  42. package/lib/module/skia/types/Paragraph/Paragraph.d.ts +62 -0
  43. package/lib/module/skia/types/Paragraph/Paragraph.js.map +1 -1
  44. package/lib/module/skia/web/Host.js +7 -0
  45. package/lib/module/skia/web/Host.js.map +1 -1
  46. package/lib/module/skia/web/JsiSkPaint.js +5 -0
  47. package/lib/module/skia/web/JsiSkPaint.js.map +1 -1
  48. package/lib/module/skia/web/JsiSkParagraph.d.ts +3 -1
  49. package/lib/module/skia/web/JsiSkParagraph.js +7 -1
  50. package/lib/module/skia/web/JsiSkParagraph.js.map +1 -1
  51. package/lib/module/sksg/Container.web.js +35 -4
  52. package/lib/module/sksg/Container.web.js.map +1 -1
  53. package/lib/module/sksg/Recorder/DrawingContext.d.ts +2 -0
  54. package/lib/module/sksg/Recorder/DrawingContext.js +22 -6
  55. package/lib/module/sksg/Recorder/DrawingContext.js.map +1 -1
  56. package/lib/module/sksg/Recorder/FrameScope.d.ts +18 -0
  57. package/lib/module/sksg/Recorder/FrameScope.js +59 -0
  58. package/lib/module/sksg/Recorder/FrameScope.js.map +1 -0
  59. package/lib/module/sksg/Recorder/FrameScope.native.d.ts +10 -0
  60. package/lib/module/sksg/Recorder/FrameScope.native.js +19 -0
  61. package/lib/module/sksg/Recorder/FrameScope.native.js.map +1 -0
  62. package/lib/module/sksg/Recorder/Player.js +1 -2
  63. package/lib/module/sksg/Recorder/Player.js.map +1 -1
  64. package/lib/module/sksg/Recorder/Recorder.d.ts +3 -1
  65. package/lib/module/sksg/Recorder/Recorder.js +10 -0
  66. package/lib/module/sksg/Recorder/Recorder.js.map +1 -1
  67. package/lib/module/sksg/Recorder/commands/Drawing.js +3 -3
  68. package/lib/module/sksg/Recorder/commands/Drawing.js.map +1 -1
  69. package/lib/module/sksg/Recorder/commands/Shaders.js +3 -3
  70. package/lib/module/sksg/Recorder/commands/Shaders.js.map +1 -1
  71. package/lib/module/sksg/StaticContainer.d.ts +1 -0
  72. package/lib/module/sksg/StaticContainer.js +28 -2
  73. package/lib/module/sksg/StaticContainer.js.map +1 -1
  74. package/lib/typescript/lib/commonjs/skia/web/JsiSkParagraph.d.ts +2 -0
  75. package/lib/typescript/lib/commonjs/sksg/Container.web.d.ts +2 -2
  76. package/lib/typescript/lib/commonjs/sksg/Recorder/DrawingContext.d.ts +2 -0
  77. package/lib/typescript/lib/commonjs/sksg/Recorder/FrameScope.d.ts +17 -0
  78. package/lib/typescript/lib/commonjs/sksg/Recorder/FrameScope.native.d.ts +13 -0
  79. package/lib/typescript/lib/commonjs/sksg/Recorder/Recorder.d.ts +1 -0
  80. package/lib/typescript/lib/commonjs/sksg/StaticContainer.d.ts +2 -2
  81. package/lib/typescript/lib/module/skia/web/JsiSkParagraph.d.ts +2 -0
  82. package/lib/typescript/lib/module/sksg/Container.web.d.ts +2 -2
  83. package/lib/typescript/lib/module/sksg/Recorder/DrawingContext.d.ts +2 -0
  84. package/lib/typescript/lib/module/sksg/Recorder/FrameScope.d.ts +5 -0
  85. package/lib/typescript/lib/module/sksg/Recorder/FrameScope.native.d.ts +5 -0
  86. package/lib/typescript/lib/module/sksg/Recorder/Recorder.d.ts +1 -0
  87. package/lib/typescript/lib/module/sksg/StaticContainer.d.ts +2 -2
  88. package/lib/typescript/src/skia/types/Paragraph/Paragraph.d.ts +62 -0
  89. package/lib/typescript/src/skia/web/JsiSkParagraph.d.ts +3 -1
  90. package/lib/typescript/src/sksg/Recorder/DrawingContext.d.ts +2 -0
  91. package/lib/typescript/src/sksg/Recorder/FrameScope.d.ts +18 -0
  92. package/lib/typescript/src/sksg/Recorder/FrameScope.native.d.ts +10 -0
  93. package/lib/typescript/src/sksg/Recorder/Recorder.d.ts +3 -1
  94. package/lib/typescript/src/sksg/StaticContainer.d.ts +1 -0
  95. package/package.json +1 -1
  96. package/src/external/reanimated/useVideoLoading.ts +12 -2
  97. package/src/skia/core/SVG.web.ts +5 -14
  98. package/src/skia/types/Paragraph/Paragraph.ts +67 -0
  99. package/src/skia/web/Host.ts +7 -0
  100. package/src/skia/web/JsiSkPaint.ts +5 -0
  101. package/src/skia/web/JsiSkParagraph.ts +9 -1
  102. package/src/sksg/Container.web.ts +31 -4
  103. package/src/sksg/Recorder/DrawingContext.ts +19 -6
  104. package/src/sksg/Recorder/FrameScope.native.ts +21 -0
  105. package/src/sksg/Recorder/FrameScope.ts +69 -0
  106. package/src/sksg/Recorder/Player.ts +1 -2
  107. package/src/sksg/Recorder/Recorder.ts +15 -1
  108. package/src/sksg/Recorder/commands/Drawing.ts +3 -3
  109. package/src/sksg/Recorder/commands/Shaders.ts +22 -16
  110. package/src/sksg/StaticContainer.ts +29 -2
@@ -1,6 +1,10 @@
1
1
  import type { SkCanvas } from "../Canvas";
2
+ import type { SkFont } from "../Font";
2
3
  import type { SkJSIInstance } from "../JsiInstance";
4
+ import type { SkPath } from "../Path";
5
+ import type { SkPoint } from "../Point";
3
6
  import type { SkRect } from "../Rect";
7
+ import type { SkSize } from "../Size";
4
8
  import type { TextDirection } from "./ParagraphStyle";
5
9
  export interface LineMetrics {
6
10
  /** The index in the text buffer the line begins. */
@@ -36,6 +40,42 @@ export interface SkRectWithDirection {
36
40
  rect: SkRect;
37
41
  direction: TextDirection;
38
42
  }
43
+ /**
44
+ * Information about a run of glyphs passed to the visitor of
45
+ * `SkParagraph.extendedVisit`. All glyphs in a run share the same font: for
46
+ * text shaped through font fallback, each fallback font produces its own
47
+ * run(s).
48
+ */
49
+ export interface ParagraphVisitorInfo {
50
+ /** The font used to shape this run (the resolved fallback font, if any). */
51
+ font: SkFont;
52
+ /** Origin of the run within the paragraph (baseline corrected). */
53
+ origin: SkPoint;
54
+ /** Advance of the run. */
55
+ advance: SkSize;
56
+ /** The glyph ids of the run. */
57
+ glyphs: number[];
58
+ /**
59
+ * The position of each glyph, relative to the run `origin`. Add `origin`
60
+ * to obtain paragraph coordinates: for a run shaped through font fallback,
61
+ * the run placement is carried by `origin`, not by the positions.
62
+ */
63
+ positions: SkPoint[];
64
+ /**
65
+ * The tight ink bounds of each glyph, relative to the glyph's origin.
66
+ * Combine with `positions` to compute the exact painted bounds of the run.
67
+ */
68
+ bounds: SkRect[];
69
+ /** The UTF-8 text index of the cluster each glyph belongs to. */
70
+ utf8Starts: number[];
71
+ /** Reserved flags (currently always 0). */
72
+ flags: number;
73
+ }
74
+ /**
75
+ * Visitor for `SkParagraph.extendedVisit`. Called once per run of glyphs;
76
+ * `info` is null to signal the end of a line.
77
+ */
78
+ export type ParagraphExtendedVisitor = (lineNumber: number, info: ParagraphVisitorInfo | null) => void;
39
79
  export interface SkParagraph extends SkJSIInstance<"Paragraph"> {
40
80
  /**
41
81
  * Calculates the position of the the glyphs in the paragraph
@@ -106,4 +146,26 @@ export interface SkParagraph extends SkJSIInstance<"Paragraph"> {
106
146
  * to the paragraph.
107
147
  */
108
148
  getRectsForPlaceholders(): SkRectWithDirection[];
149
+ /**
150
+ * Converts the glyphs of the given line into an SkPath, with all font
151
+ * fallbacks already applied. Use `computeTightBounds()` on the resulting
152
+ * path to measure the exact ink bounds of the rendered text (as opposed to
153
+ * the font-metrics based bounds returned by `getRectsForRange`).
154
+ * Note that color glyphs (e.g. emojis) cannot be converted to a path and
155
+ * are skipped. Returns null if the line number is out of bounds.
156
+ * This method requires the layout method to have been called first.
157
+ * Not implemented on React Native Web.
158
+ * @param lineNumber The line number (zero indexed, see `getLineMetrics`)
159
+ */
160
+ getPath(lineNumber: number): SkPath | null;
161
+ /**
162
+ * Visits the laid out paragraph, calling the visitor once for every run of
163
+ * glyphs with the resolved font, glyph ids, positions and per-glyph tight
164
+ * ink bounds. The visitor is called with a null info to signal the end of
165
+ * each line. This exposes the exact layout that would be painted.
166
+ * This method requires the layout method to have been called first.
167
+ * Not implemented on React Native Web.
168
+ * @param visitor Called once per glyph run, and once per line end
169
+ */
170
+ extendedVisit(visitor: ParagraphExtendedVisitor): void;
109
171
  }
@@ -1,5 +1,5 @@
1
1
  import type { CanvasKit, Paragraph } from "canvaskit-wasm";
2
- import type { SkRect, SkRectWithDirection, SkParagraph, LineMetrics } from "../types";
2
+ import type { SkRect, SkRectWithDirection, SkParagraph, SkPath, LineMetrics, ParagraphExtendedVisitor } from "../types";
3
3
  import { HostObject } from "./Host";
4
4
  import type { JsiSkCanvas } from "./JsiSkCanvas";
5
5
  export declare class JsiSkParagraph extends HostObject<Paragraph, "Paragraph"> implements SkParagraph {
@@ -15,4 +15,6 @@ export declare class JsiSkParagraph extends HostObject<Paragraph, "Paragraph"> i
15
15
  getRectsForPlaceholders(): SkRectWithDirection[];
16
16
  getRectsForRange(start: number, end: number): SkRect[];
17
17
  getLineMetrics(): LineMetrics[];
18
+ getPath(_lineNumber: number): SkPath | null;
19
+ extendedVisit(_visitor: ParagraphExtendedVisitor): void;
18
20
  }
@@ -2,6 +2,8 @@ import type { Skia, SkCanvas, SkColorFilter, SkPaint, SkShader, SkImageFilter, S
2
2
  export declare const createDrawingContext: (Skia: Skia, paintPool: SkPaint[], canvas: SkCanvas) => {
3
3
  Skia: Skia;
4
4
  canvas: SkCanvas;
5
+ track: <T>(value: T) => T;
6
+ dispose: () => void;
5
7
  paints: SkPaint[];
6
8
  colorFilters: SkColorFilter[];
7
9
  shaders: SkShader[];
@@ -0,0 +1,18 @@
1
+ import type { Skia } from "../../skia/types";
2
+ export interface FrameScope {
3
+ Skia: Skia;
4
+ track: <T>(value: T) => T;
5
+ dispose: () => void;
6
+ }
7
+ /**
8
+ * Wraps a Skia instance so that every object created through its factories is
9
+ * collected and deleted when the scope is disposed. The renderer only keeps
10
+ * the objects it creates alive for the duration of a frame: once the frame
11
+ * has been recorded they can be deleted. This is required on Web where
12
+ * CanvasKit objects live in WASM memory that the JS garbage collector does
13
+ * not perceive, so it (almost) never reclaims them on its own.
14
+ *
15
+ * Objects passed in via props are user-owned: they are not created through
16
+ * this facade and are therefore never tracked nor disposed.
17
+ */
18
+ export declare const createFrameScope: (Skia: Skia) => FrameScope;
@@ -0,0 +1,10 @@
1
+ import type { Skia } from "../../skia/types";
2
+ import type { FrameScope } from "./FrameScope";
3
+ /**
4
+ * No-op on native. The JSI wrappers report the size of the native objects
5
+ * they hold to the garbage collector (setExternalMemoryPressure), so the GC
6
+ * is aware of the real cost of the objects the renderer creates and reclaims
7
+ * them on its own. The tracking scope is only needed on Web, where CanvasKit
8
+ * objects live in WASM memory the GC cannot see (see FrameScope.ts).
9
+ */
10
+ export declare const createFrameScope: (Skia: Skia) => FrameScope;
@@ -2,12 +2,14 @@ import type { SharedValue } from "react-native-reanimated";
2
2
  import { NodeType } from "../../dom/types";
3
3
  import type { BlurMaskFilterProps, CircleProps, CTMProps, ImageProps, PaintProps, PointsProps, PathProps, RectProps, RoundedRectProps, OvalProps, LineProps, PatchProps, VerticesProps, DiffRectProps, TextProps, TextPathProps, TextBlobProps, GlyphsProps, PictureProps, ImageSVGProps, ParagraphProps, AtlasProps, BoxProps, BoxShadowProps, SkottieProps, DrawingNodeProps } from "../../dom/types";
4
4
  import type { AnimatedProps } from "../../renderer";
5
- import type { SkPaint, BaseRecorder } from "../../skia/types";
5
+ import type { SkPaint, SkPicture, BaseRecorder } from "../../skia/types";
6
6
  import type { Command } from "./Core";
7
7
  export interface Recording {
8
8
  commands: Command[];
9
9
  paintPool: SkPaint[];
10
+ lastPicture?: SkPicture | null;
10
11
  }
12
+ export declare const disposeRecording: (recording: Recording) => void;
11
13
  interface AnimationValues {
12
14
  animationValues: Set<SharedValue<unknown>>;
13
15
  }
@@ -18,5 +18,6 @@ export declare abstract class Container {
18
18
  export declare class StaticContainer extends Container {
19
19
  private nativeId;
20
20
  constructor(Skia: Skia, nativeId: number);
21
+ unmount(): void;
21
22
  redraw(): void;
22
23
  }
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.7.0",
11
+ "version": "2.8.0",
12
12
  "description": "High-performance React Native Graphics using Skia",
13
13
  "main": "lib/module/index.js",
14
14
  "react-native": "src/index.ts",
@@ -1,11 +1,21 @@
1
1
  import { useCallback, useEffect, useState } from "react";
2
+ import type { WorkletRuntime } from "react-native-reanimated";
2
3
 
3
4
  import type { Video } from "../../skia/types";
4
5
  import { Skia } from "../../skia";
5
6
 
6
7
  import Rea from "./ReanimatedProxy";
7
8
 
8
- const runtime = Rea.createWorkletRuntime("video-metadata-runtime");
9
+ // Created lazily: accessing the proxy at module scope would require
10
+ // react-native-reanimated as soon as the package is imported, and would spawn
11
+ // the runtime even for apps that never load a video.
12
+ let runtime: WorkletRuntime | undefined;
13
+ const getRuntime = () => {
14
+ if (runtime === undefined) {
15
+ runtime = Rea.createWorkletRuntime("video-metadata-runtime");
16
+ }
17
+ return runtime;
18
+ };
9
19
 
10
20
  type VideoSource = string | null;
11
21
 
@@ -22,7 +32,7 @@ export const useVideoLoading = (source: VideoSource) => {
22
32
  );
23
33
  useEffect(() => {
24
34
  if (source) {
25
- Rea.runOnRuntime(runtime, cb)(source);
35
+ Rea.runOnRuntime(getRuntime(), cb)(source);
26
36
  }
27
37
  }, [cb, source]);
28
38
  return video;
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
2
2
 
3
3
  import { Skia } from "../Skia";
4
4
  import type { DataSourceParam, SkSVG } from "../types";
5
+ import { Platform } from "../../Platform";
5
6
 
6
7
  export const useSVG = (
7
8
  source: DataSourceParam,
@@ -16,22 +17,12 @@ export const useSVG = (
16
17
  let src: string;
17
18
  if (typeof source === "string") {
18
19
  src = source;
19
- } else if (
20
- typeof source === "object" &&
21
- "default" in source &&
22
- typeof source.default === "string"
23
- ) {
24
- src = source.default;
25
- } else if (typeof source === "object" && "uri" in source) {
26
- src = source.uri;
27
- } else {
20
+ } else if (source instanceof Uint8Array) {
28
21
  throw new Error(
29
- `Invalid svg data source. Make sure that the source resolves to a string. Got: ${JSON.stringify(
30
- source,
31
- null,
32
- 2
33
- )}`
22
+ `Invalid svg data source. Make sure that the source resolves to a string. Got: ${source}`
34
23
  );
24
+ } else {
25
+ src = Platform.resolveAsset(source);
35
26
  }
36
27
  const result = await fetch(src);
37
28
  const svgStr = await result.text();
@@ -1,6 +1,10 @@
1
1
  import type { SkCanvas } from "../Canvas";
2
+ import type { SkFont } from "../Font";
2
3
  import type { SkJSIInstance } from "../JsiInstance";
4
+ import type { SkPath } from "../Path";
5
+ import type { SkPoint } from "../Point";
3
6
  import type { SkRect } from "../Rect";
7
+ import type { SkSize } from "../Size";
4
8
 
5
9
  import type { TextDirection } from "./ParagraphStyle";
6
10
 
@@ -40,6 +44,47 @@ export interface SkRectWithDirection {
40
44
  direction: TextDirection;
41
45
  }
42
46
 
47
+ /**
48
+ * Information about a run of glyphs passed to the visitor of
49
+ * `SkParagraph.extendedVisit`. All glyphs in a run share the same font: for
50
+ * text shaped through font fallback, each fallback font produces its own
51
+ * run(s).
52
+ */
53
+ export interface ParagraphVisitorInfo {
54
+ /** The font used to shape this run (the resolved fallback font, if any). */
55
+ font: SkFont;
56
+ /** Origin of the run within the paragraph (baseline corrected). */
57
+ origin: SkPoint;
58
+ /** Advance of the run. */
59
+ advance: SkSize;
60
+ /** The glyph ids of the run. */
61
+ glyphs: number[];
62
+ /**
63
+ * The position of each glyph, relative to the run `origin`. Add `origin`
64
+ * to obtain paragraph coordinates: for a run shaped through font fallback,
65
+ * the run placement is carried by `origin`, not by the positions.
66
+ */
67
+ positions: SkPoint[];
68
+ /**
69
+ * The tight ink bounds of each glyph, relative to the glyph's origin.
70
+ * Combine with `positions` to compute the exact painted bounds of the run.
71
+ */
72
+ bounds: SkRect[];
73
+ /** The UTF-8 text index of the cluster each glyph belongs to. */
74
+ utf8Starts: number[];
75
+ /** Reserved flags (currently always 0). */
76
+ flags: number;
77
+ }
78
+
79
+ /**
80
+ * Visitor for `SkParagraph.extendedVisit`. Called once per run of glyphs;
81
+ * `info` is null to signal the end of a line.
82
+ */
83
+ export type ParagraphExtendedVisitor = (
84
+ lineNumber: number,
85
+ info: ParagraphVisitorInfo | null
86
+ ) => void;
87
+
43
88
  export interface SkParagraph extends SkJSIInstance<"Paragraph"> {
44
89
  /**
45
90
  * Calculates the position of the the glyphs in the paragraph
@@ -112,4 +157,26 @@ export interface SkParagraph extends SkJSIInstance<"Paragraph"> {
112
157
  * to the paragraph.
113
158
  */
114
159
  getRectsForPlaceholders(): SkRectWithDirection[];
160
+ /**
161
+ * Converts the glyphs of the given line into an SkPath, with all font
162
+ * fallbacks already applied. Use `computeTightBounds()` on the resulting
163
+ * path to measure the exact ink bounds of the rendered text (as opposed to
164
+ * the font-metrics based bounds returned by `getRectsForRange`).
165
+ * Note that color glyphs (e.g. emojis) cannot be converted to a path and
166
+ * are skipped. Returns null if the line number is out of bounds.
167
+ * This method requires the layout method to have been called first.
168
+ * Not implemented on React Native Web.
169
+ * @param lineNumber The line number (zero indexed, see `getLineMetrics`)
170
+ */
171
+ getPath(lineNumber: number): SkPath | null;
172
+ /**
173
+ * Visits the laid out paragraph, calling the visitor once for every run of
174
+ * glyphs with the resolved font, glyph ids, positions and per-glyph tight
175
+ * ink bounds. The visitor is called with a null info to signal the end of
176
+ * each line. This exposes the exact layout that would be painted.
177
+ * This method requires the layout method to have been called first.
178
+ * Not implemented on React Native Web.
179
+ * @param visitor Called once per glyph run, and once per line end
180
+ */
181
+ extendedVisit(visitor: ParagraphExtendedVisitor): void;
115
182
  }
@@ -41,6 +41,13 @@ export abstract class BaseHostObject<T, N extends string>
41
41
  "delete" in this.ref &&
42
42
  typeof this.ref.delete === "function"
43
43
  ) {
44
+ // Renderer-owned and user-owned disposal can overlap (and emscripten
45
+ // throws on double delete) — make disposing an already-deleted object
46
+ // a no-op.
47
+ const ref = this.ref as unknown as { isDeleted?: () => boolean };
48
+ if (typeof ref.isDeleted === "function" && ref.isDeleted()) {
49
+ return;
50
+ }
44
51
  this.ref.delete();
45
52
  }
46
53
  }
@@ -76,11 +76,16 @@ export class JsiSkPaint extends HostObject<Paint, "Paint"> implements SkPaint {
76
76
  }
77
77
 
78
78
  assign(paint: JsiSkPaint) {
79
+ const previous = this.ref;
79
80
  this.ref = paint.ref.copy();
81
+ // The replaced ref is a WASM object the GC will not reclaim.
82
+ previous.delete();
80
83
  }
81
84
 
82
85
  reset() {
86
+ const previous = this.ref;
83
87
  this.ref = new this.CanvasKit.Paint();
88
+ previous.delete();
84
89
  }
85
90
 
86
91
  getAlphaf() {
@@ -4,10 +4,12 @@ import type {
4
4
  SkRect,
5
5
  SkRectWithDirection,
6
6
  SkParagraph,
7
+ SkPath,
7
8
  LineMetrics,
9
+ ParagraphExtendedVisitor,
8
10
  } from "../types";
9
11
 
10
- import { HostObject } from "./Host";
12
+ import { HostObject, throwNotImplementedOnRNWeb } from "./Host";
11
13
  import type { JsiSkCanvas } from "./JsiSkCanvas";
12
14
  import { JsiSkRect } from "./JsiSkRect";
13
15
 
@@ -64,4 +66,10 @@ export class JsiSkParagraph
64
66
  getLineMetrics(): LineMetrics[] {
65
67
  return this.ref.getLineMetrics();
66
68
  }
69
+ getPath(_lineNumber: number): SkPath | null {
70
+ return throwNotImplementedOnRNWeb<SkPath | null>();
71
+ }
72
+ extendedVisit(_visitor: ParagraphExtendedVisitor): void {
73
+ return throwNotImplementedOnRNWeb<void>();
74
+ }
67
75
  }
@@ -3,7 +3,7 @@ import type { Skia } from "../skia/types";
3
3
  import { HAS_REANIMATED_3 } from "../external/reanimated/renderHelpers";
4
4
 
5
5
  import type { Recording } from "./Recorder/Recorder";
6
- import { Recorder } from "./Recorder/Recorder";
6
+ import { Recorder, disposeRecording } from "./Recorder/Recorder";
7
7
  import { visit } from "./Recorder/Visitor";
8
8
  import { replay } from "./Recorder/Player";
9
9
  import { createDrawingContext } from "./Recorder/DrawingContext";
@@ -22,12 +22,24 @@ const drawOnscreen = (Skia: Skia, nativeId: number, recording: Recording) => {
22
22
  //const start = performance.now();
23
23
 
24
24
  const ctx = createDrawingContext(Skia, recording.paintPool, canvas);
25
- replay(ctx, recording.commands);
26
- const picture = rec.finishRecordingAsPicture();
27
- rec.dispose();
25
+ let picture;
26
+ try {
27
+ replay(ctx, recording.commands);
28
+ picture = rec.finishRecordingAsPicture();
29
+ } finally {
30
+ // Frame-scoped objects (shaders, filters, paint copies, ...) are
31
+ // referenced by the picture once it is finished — delete the JS handles,
32
+ // otherwise they leak on Web where the GC cannot see WASM memory.
33
+ ctx.dispose();
34
+ rec.dispose();
35
+ }
28
36
  //const end = performance.now();
29
37
  //console.log("Recording time: ", end - start);
30
38
  SkiaViewApi.setJsiProperty(nativeId, "picture", picture);
39
+ // The view now references the new picture; the previous one is ours to
40
+ // delete.
41
+ recording.lastPicture?.dispose();
42
+ recording.lastPicture = picture;
31
43
  };
32
44
 
33
45
  class ReanimatedContainer extends Container {
@@ -49,6 +61,16 @@ class ReanimatedContainer extends Container {
49
61
  Rea.stopMapper(this.mapperId);
50
62
  this.mapperId = null;
51
63
  }
64
+ const { recording } = this;
65
+ if (recording) {
66
+ this.recording = null;
67
+ // Deferred through the same channel as the draw calls so it runs after
68
+ // any of them that are still queued.
69
+ Rea.runOnUI(() => {
70
+ "worklet";
71
+ disposeRecording(recording);
72
+ })();
73
+ }
52
74
  }
53
75
 
54
76
  redraw() {
@@ -59,6 +81,7 @@ class ReanimatedContainer extends Container {
59
81
  if (this.unmounted) {
60
82
  return;
61
83
  }
84
+ const previousRecording = this.recording;
62
85
  const recorder = new Recorder();
63
86
  visit(recorder, this.root);
64
87
  const record = recorder.getRecording();
@@ -76,6 +99,10 @@ class ReanimatedContainer extends Container {
76
99
  }
77
100
  Rea.runOnUI(() => {
78
101
  "worklet";
102
+ if (previousRecording) {
103
+ // Runs after any still-queued draw of the previous recording.
104
+ disposeRecording(previousRecording);
105
+ }
79
106
  drawOnscreen(Skia, nativeId, recording!);
80
107
  })();
81
108
  }
@@ -8,6 +8,8 @@ import type {
8
8
  SkPathEffect,
9
9
  } from "../../skia/types";
10
10
 
11
+ import { createFrameScope } from "./FrameScope";
12
+
11
13
  export const createDrawingContext = (
12
14
  Skia: Skia,
13
15
  paintPool: SkPaint[],
@@ -15,6 +17,11 @@ export const createDrawingContext = (
15
17
  ) => {
16
18
  "worklet";
17
19
 
20
+ // Everything the renderer creates through frameSkia is frame-scoped and
21
+ // deleted by dispose() once the frame has been recorded. The paint pool is
22
+ // recording-scoped and deliberately allocated with the raw Skia instance.
23
+ const { Skia: frameSkia, track, dispose } = createFrameScope(Skia);
24
+
18
25
  // State (formerly class fields)
19
26
  const paints: SkPaint[] = [];
20
27
  const colorFilters: SkColorFilter[] = [];
@@ -27,7 +34,11 @@ export const createDrawingContext = (
27
34
  let nextPaintIndex = 1;
28
35
 
29
36
  // Initialize first paint and opacity
30
- paintPool[0] = Skia.Paint();
37
+ if (paintPool.length === 0) {
38
+ paintPool.push(Skia.Paint());
39
+ } else {
40
+ paintPool[0].reset();
41
+ }
31
42
  paints.push(paintPool[0]);
32
43
  opacities.push(1);
33
44
 
@@ -61,7 +72,7 @@ export const createDrawingContext = (
61
72
  } else {
62
73
  const cf = colorFilters.pop();
63
74
  if (cf) {
64
- imageFilter = Skia.ImageFilter.MakeColorFilter(cf, null);
75
+ imageFilter = frameSkia.ImageFilter.MakeColorFilter(cf, null);
65
76
  }
66
77
  }
67
78
  canvas.saveLayer(undefined, null, imageFilter);
@@ -83,7 +94,7 @@ export const createDrawingContext = (
83
94
  if (colorFilters.length > 0) {
84
95
  getCurrentPaint().setColorFilter(
85
96
  colorFilters.reduceRight((inner, outer) =>
86
- inner ? Skia.ColorFilter.MakeCompose(outer, inner) : outer
97
+ inner ? frameSkia.ColorFilter.MakeCompose(outer, inner) : outer
87
98
  )
88
99
  );
89
100
  }
@@ -95,7 +106,7 @@ export const createDrawingContext = (
95
106
  if (imageFilters.length > 0) {
96
107
  getCurrentPaint().setImageFilter(
97
108
  imageFilters.reduceRight((inner, outer) =>
98
- inner ? Skia.ImageFilter.MakeCompose(outer, inner) : outer
109
+ inner ? frameSkia.ImageFilter.MakeCompose(outer, inner) : outer
99
110
  )
100
111
  );
101
112
  }
@@ -104,7 +115,7 @@ export const createDrawingContext = (
104
115
  if (pathEffects.length > 0) {
105
116
  getCurrentPaint().setPathEffect(
106
117
  pathEffects.reduceRight((inner, outer) =>
107
- inner ? Skia.PathEffect.MakeCompose(outer, inner) : outer
118
+ inner ? frameSkia.PathEffect.MakeCompose(outer, inner) : outer
108
119
  )
109
120
  );
110
121
  }
@@ -119,8 +130,10 @@ export const createDrawingContext = (
119
130
  // Return an object containing the Skia reference, the canvas, and the methods
120
131
  return {
121
132
  // Public fields
122
- Skia,
133
+ Skia: frameSkia,
123
134
  canvas,
135
+ track,
136
+ dispose,
124
137
  paints,
125
138
  colorFilters,
126
139
  shaders,
@@ -0,0 +1,21 @@
1
+ import type { Skia } from "../../skia/types";
2
+
3
+ import type { FrameScope } from "./FrameScope";
4
+
5
+ /**
6
+ * No-op on native. The JSI wrappers report the size of the native objects
7
+ * they hold to the garbage collector (setExternalMemoryPressure), so the GC
8
+ * is aware of the real cost of the objects the renderer creates and reclaims
9
+ * them on its own. The tracking scope is only needed on Web, where CanvasKit
10
+ * objects live in WASM memory the GC cannot see (see FrameScope.ts).
11
+ */
12
+ export const createFrameScope = (Skia: Skia): FrameScope => {
13
+ "worklet";
14
+ return {
15
+ Skia,
16
+ track: <T>(value: T): T => value,
17
+ dispose: () => {
18
+ // nothing to dispose: no objects are tracked on native
19
+ },
20
+ };
21
+ };
@@ -0,0 +1,69 @@
1
+ import type { Skia } from "../../skia/types";
2
+
3
+ interface Disposable {
4
+ dispose: () => void;
5
+ }
6
+
7
+ const isDisposable = (value: unknown): value is Disposable => {
8
+ "worklet";
9
+ return (
10
+ value !== null &&
11
+ typeof value === "object" &&
12
+ typeof (value as Disposable).dispose === "function"
13
+ );
14
+ };
15
+
16
+ export interface FrameScope {
17
+ Skia: Skia;
18
+ track: <T>(value: T) => T;
19
+ dispose: () => void;
20
+ }
21
+
22
+ /**
23
+ * Wraps a Skia instance so that every object created through its factories is
24
+ * collected and deleted when the scope is disposed. The renderer only keeps
25
+ * the objects it creates alive for the duration of a frame: once the frame
26
+ * has been recorded they can be deleted. This is required on Web where
27
+ * CanvasKit objects live in WASM memory that the JS garbage collector does
28
+ * not perceive, so it (almost) never reclaims them on its own.
29
+ *
30
+ * Objects passed in via props are user-owned: they are not created through
31
+ * this facade and are therefore never tracked nor disposed.
32
+ */
33
+ export const createFrameScope = (Skia: Skia): FrameScope => {
34
+ "worklet";
35
+ const disposables: Disposable[] = [];
36
+ const track = <T>(value: T): T => {
37
+ if (isDisposable(value)) {
38
+ disposables.push(value);
39
+ }
40
+ return value;
41
+ };
42
+ const wrap = <T extends object>(obj: T): T => {
43
+ const cache = new Map<PropertyKey, unknown>();
44
+ return new Proxy(obj, {
45
+ get(target, prop) {
46
+ if (cache.has(prop)) {
47
+ return cache.get(prop);
48
+ }
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ const value = (target as any)[prop];
51
+ let wrapped: unknown = value;
52
+ if (typeof value === "function") {
53
+ wrapped = (...args: unknown[]) => track(value.apply(target, args));
54
+ } else if (value !== null && typeof value === "object") {
55
+ wrapped = wrap(value);
56
+ }
57
+ cache.set(prop, wrapped);
58
+ return wrapped;
59
+ },
60
+ }) as T;
61
+ };
62
+ const dispose = () => {
63
+ for (let i = 0; i < disposables.length; i++) {
64
+ disposables[i].dispose();
65
+ }
66
+ disposables.length = 0;
67
+ };
68
+ return { Skia: wrap(Skia), track, dispose };
69
+ };
@@ -166,9 +166,8 @@ const play = (ctx: DrawingContext, _command: Command) => {
166
166
  } else if (isCommand(command, CommandType.RestoreCTM)) {
167
167
  ctx.canvas.restore();
168
168
  } else {
169
- // TODO: is a copy needed here?
170
169
  // apply opacity to the current paint.
171
- const paint = ctx.paint.copy();
170
+ const paint = ctx.track(ctx.paint.copy());
172
171
  paint.setAlphaf(paint.getAlphaf() * ctx.getOpacity());
173
172
  const paints = [paint, ...ctx.paintDeclarations];
174
173
  ctx.paintDeclarations = [];
@@ -32,7 +32,7 @@ import type {
32
32
  import type { AnimatedProps } from "../../renderer";
33
33
  import { isSharedValue } from "../utils";
34
34
  import { isColorFilter, isImageFilter, isPathEffect, isShader } from "../Node";
35
- import type { SkPaint, BaseRecorder } from "../../skia/types";
35
+ import type { SkPaint, SkPicture, BaseRecorder } from "../../skia/types";
36
36
 
37
37
  import { CommandType } from "./Core";
38
38
  import type { Command } from "./Core";
@@ -40,8 +40,22 @@ import type { Command } from "./Core";
40
40
  export interface Recording {
41
41
  commands: Command[];
42
42
  paintPool: SkPaint[];
43
+ // Last picture produced from this recording. The view keeps its own
44
+ // reference to the picture it displays, so the renderer owns this one and
45
+ // disposes it when the next frame replaces it or the recording is disposed.
46
+ lastPicture?: SkPicture | null;
43
47
  }
44
48
 
49
+ export const disposeRecording = (recording: Recording) => {
50
+ "worklet";
51
+ recording.paintPool.forEach((paint) => paint.dispose());
52
+ recording.paintPool.length = 0;
53
+ if (recording.lastPicture) {
54
+ recording.lastPicture.dispose();
55
+ recording.lastPicture = null;
56
+ }
57
+ };
58
+
45
59
  interface AnimationValues {
46
60
  animationValues: Set<SharedValue<unknown>>;
47
61
  }