@shopify/react-native-skia 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. package/android/src/main/java/com/shopify/reactnative/skia/ViewScreenshotService.java +16 -1
  2. package/lib/commonjs/headless/index.d.ts +4 -2
  3. package/lib/commonjs/headless/index.js +11 -18
  4. package/lib/commonjs/headless/index.js.map +1 -1
  5. package/lib/commonjs/renderer/Offscreen.js +1 -0
  6. package/lib/commonjs/renderer/Offscreen.js.map +1 -1
  7. package/lib/commonjs/skia/web/JsiSkImage.d.ts +2 -1
  8. package/lib/commonjs/skia/web/JsiSkImage.js +27 -2
  9. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  10. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +2 -1
  11. package/lib/commonjs/skia/web/JsiSkSurface.js +5 -1
  12. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  13. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.js +13 -2
  14. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.js.map +1 -1
  15. package/lib/module/headless/index.d.ts +4 -2
  16. package/lib/module/headless/index.js +9 -3
  17. package/lib/module/headless/index.js.map +1 -1
  18. package/lib/module/renderer/Offscreen.js +1 -0
  19. package/lib/module/renderer/Offscreen.js.map +1 -1
  20. package/lib/module/skia/web/JsiSkImage.d.ts +2 -1
  21. package/lib/module/skia/web/JsiSkImage.js +27 -2
  22. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  23. package/lib/module/skia/web/JsiSkSurface.d.ts +2 -1
  24. package/lib/module/skia/web/JsiSkSurface.js +5 -1
  25. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  26. package/lib/module/skia/web/JsiSkSurfaceFactory.js +13 -2
  27. package/lib/module/skia/web/JsiSkSurfaceFactory.js.map +1 -1
  28. package/lib/typescript/src/headless/index.d.ts +4 -2
  29. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +2 -1
  30. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +2 -1
  31. package/package.json +1 -1
  32. package/src/headless/index.ts +9 -4
  33. package/src/renderer/Offscreen.tsx +1 -0
  34. package/src/skia/web/JsiSkImage.ts +36 -5
  35. package/src/skia/web/JsiSkSurface.ts +8 -1
  36. package/src/skia/web/JsiSkSurfaceFactory.ts +17 -2
@@ -14,6 +14,7 @@ import android.view.SurfaceView;
14
14
  import android.view.TextureView;
15
15
  import android.view.View;
16
16
  import android.view.ViewGroup;
17
+ import android.widget.ScrollView;
17
18
  import androidx.annotation.NonNull;
18
19
  import com.facebook.react.bridge.ReactContext;
19
20
  import com.facebook.react.uimanager.UIManagerModule;
@@ -70,6 +71,17 @@ public class ViewScreenshotService {
70
71
  canvas.save();
71
72
  applyTransformations(canvas, view);
72
73
 
74
+ // If the view is a ScrollView or similar, clip to its bounds
75
+ if (view instanceof ScrollView) {
76
+ ScrollView scrollView = (ScrollView) view;
77
+ int clipLeft = scrollView.getScrollX();
78
+ int clipTop = scrollView.getScrollY();
79
+ int clipRight = clipLeft + scrollView.getWidth();
80
+ int clipBottom = clipTop + scrollView.getHeight();
81
+
82
+ canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
83
+ }
84
+
73
85
  if (view instanceof ViewGroup) {
74
86
  ViewGroup group = (ViewGroup) view;
75
87
  drawBackgroundIfPresent(canvas, view, combinedOpacity);
@@ -166,7 +178,10 @@ public class ViewScreenshotService {
166
178
  private static void applyTransformations(final Canvas c, @NonNull final View view) {
167
179
  final Matrix matrix = view.getMatrix();
168
180
  final Matrix translateMatrix = new Matrix();
169
- translateMatrix.setTranslate(view.getLeft() + view.getPaddingLeft(), view.getTop() + view.getPaddingTop());
181
+
182
+ translateMatrix.setTranslate(view.getLeft() + view.getPaddingLeft() - view.getScrollX(),
183
+ view.getTop() + view.getPaddingTop() - view.getScrollY());
184
+
170
185
  c.concat(translateMatrix);
171
186
  c.concat(matrix);
172
187
  }
@@ -1,5 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { SkSurface } from "../skia";
3
- export * from "../renderer/components";
4
- export declare const makeOffscreenSurface: (width: number, height: number) => any;
3
+ export declare const makeOffscreenSurface: (width: number, height: number) => SkSurface;
4
+ export declare const getSkiaExports: () => {
5
+ Skia: import("../skia/types").Skia;
6
+ };
5
7
  export declare const drawOffscreen: (surface: SkSurface, element: ReactNode) => import("../skia").SkImage;
@@ -3,30 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _exportNames = {
7
- makeOffscreenSurface: true,
8
- drawOffscreen: true
9
- };
10
- exports.makeOffscreenSurface = exports.drawOffscreen = void 0;
6
+ exports.makeOffscreenSurface = exports.getSkiaExports = exports.drawOffscreen = void 0;
11
7
  var _web = require("../skia/web");
12
8
  var _Reconciler = require("../renderer/Reconciler");
13
9
  var _types = require("../dom/types");
14
- var _components = require("../renderer/components");
15
- Object.keys(_components).forEach(function (key) {
16
- if (key === "default" || key === "__esModule") return;
17
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
18
- if (key in exports && exports[key] === _components[key]) return;
19
- Object.defineProperty(exports, key, {
20
- enumerable: true,
21
- get: function () {
22
- return _components[key];
23
- }
24
- });
25
- });
26
10
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
27
11
  // @ts-ignore
28
12
 
29
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
13
  let Skia;
31
14
  const makeOffscreenSurface = (width, height) => {
32
15
  if (!Skia) {
@@ -39,12 +22,22 @@ const makeOffscreenSurface = (width, height) => {
39
22
  return surface;
40
23
  };
41
24
  exports.makeOffscreenSurface = makeOffscreenSurface;
25
+ const getSkiaExports = () => {
26
+ if (!Skia) {
27
+ Skia = (0, _web.JsiSkApi)(CanvasKit);
28
+ }
29
+ return {
30
+ Skia
31
+ };
32
+ };
33
+ exports.getSkiaExports = getSkiaExports;
42
34
  const drawOffscreen = (surface, element) => {
43
35
  const root = new _Reconciler.SkiaRoot(Skia, false);
44
36
  root.render(element);
45
37
  const canvas = surface.getCanvas();
46
38
  const ctx = new _types.JsiDrawingContext(Skia, canvas);
47
39
  root.dom.render(ctx);
40
+ root.unmount();
48
41
  surface.flush();
49
42
  return surface.makeImageSnapshot();
50
43
  };
@@ -1 +1 @@
1
- {"version":3,"names":["_web","require","_Reconciler","_types","_components","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","Skia","makeOffscreenSurface","width","height","JsiSkApi","CanvasKit","surface","Surface","MakeOffscreen","Error","drawOffscreen","element","root","SkiaRoot","render","canvas","getCanvas","ctx","JsiDrawingContext","dom","flush","makeImageSnapshot"],"sources":["index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport CanvasKitInit from \"canvaskit-wasm/bin/full/canvaskit\";\nimport type { ReactNode } from \"react\";\n\nimport { JsiSkApi } from \"../skia/web\";\nimport { SkiaRoot } from \"../renderer/Reconciler\";\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkSurface } from \"../skia\";\n\nexport * from \"../renderer/components\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nlet Skia: any;\n\nexport const makeOffscreenSurface = (width: number, height: number) => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n const surface = Skia.Surface.MakeOffscreen(width, height);\n if (surface === null) {\n throw new Error(\"Couldn't create surface!\");\n }\n return surface;\n};\n\nexport const drawOffscreen = (surface: SkSurface, element: ReactNode) => {\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const canvas = surface.getCanvas();\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n surface.flush();\n return surface.makeImageSnapshot();\n};\n"],"mappings":";;;;;;;;;;AAKA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AAGA,IAAAG,WAAA,GAAAH,OAAA;AAAAI,MAAA,CAAAC,IAAA,CAA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AAO,CAACe,iBAAiB,CAAC,CAAC;AACpC,CAAC;AAACzB,OAAA,CAAAc,aAAA,GAAAA,aAAA"}
1
+ {"version":3,"names":["_web","require","_Reconciler","_types","Skia","makeOffscreenSurface","width","height","JsiSkApi","CanvasKit","surface","Surface","MakeOffscreen","Error","exports","getSkiaExports","drawOffscreen","element","root","SkiaRoot","render","canvas","getCanvas","ctx","JsiDrawingContext","dom","unmount","flush","makeImageSnapshot"],"sources":["index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport CanvasKitInit from \"canvaskit-wasm/bin/full/canvaskit\";\nimport type { ReactNode } from \"react\";\n\nimport { JsiSkApi } from \"../skia/web\";\nimport { SkiaRoot } from \"../renderer/Reconciler\";\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkSurface } from \"../skia\";\n\nlet Skia: ReturnType<typeof JsiSkApi>;\n\nexport const makeOffscreenSurface = (width: number, height: number) => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n const surface = Skia.Surface.MakeOffscreen(width, height);\n if (surface === null) {\n throw new Error(\"Couldn't create surface!\");\n }\n return surface;\n};\n\nexport const getSkiaExports = () => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n return { Skia };\n};\n\nexport const drawOffscreen = (surface: SkSurface, element: ReactNode) => {\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const canvas = surface.getCanvas();\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n root.unmount();\n surface.flush();\n return surface.makeImageSnapshot();\n};\n"],"mappings":";;;;;;AAKA,IAAAA,IAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AAPA;AACA;;AASA,IAAIG,IAAiC;AAE9B,MAAMC,oBAAoB,GAAGA,CAACC,KAAa,EAAEC,MAAc,KAAK;EACrE,IAAI,CAACH,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAI,aAAQ,EAACC,SAAS,CAAC;EAC5B;EACA,MAAMC,OAAO,GAAGN,IAAI,CAACO,OAAO,CAACC,aAAa,CAACN,KAAK,EAAEC,MAAM,CAAC;EACzD,IAAIG,OAAO,KAAK,IAAI,EAAE;IACpB,MAAM,IAAIG,KAAK,CAAC,0BAA0B,CAAC;EAC7C;EACA,OAAOH,OAAO;AAChB,CAAC;AAACI,OAAA,CAAAT,oBAAA,GAAAA,oBAAA;AAEK,MAAMU,cAAc,GAAGA,CAAA,KAAM;EAClC,IAAI,CAACX,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAI,aAAQ,EAACC,SAAS,CAAC;EAC5B;EACA,OAAO;IAAEL;EAAK,CAAC;AACjB,CAAC;AAACU,OAAA,CAAAC,cAAA,GAAAA,cAAA;AAEK,MAAMC,aAAa,GAAGA,CAACN,OAAkB,EAAEO,OAAkB,KAAK;EACvE,MAAMC,IAAI,GAAG,IAAIC,oBAAQ,CAACf,IAAI,EAAE,KAAK,CAAC;EACtCc,IAAI,CAACE,MAAM,CAACH,OAAO,CAAC;EACpB,MAAMI,MAAM,GAAGX,OAAO,CAACY,SAAS,CAAC,CAAC;EAClC,MAAMC,GAAG,GAAG,IAAIC,wBAAiB,CAACpB,IAAI,EAAEiB,MAAM,CAAC;EAC/CH,IAAI,CAACO,GAAG,CAACL,MAAM,CAACG,GAAG,CAAC;EACpBL,IAAI,CAACQ,OAAO,CAAC,CAAC;EACdhB,OAAO,CAACiB,KAAK,CAAC,CAAC;EACf,OAAOjB,OAAO,CAACkB,iBAAiB,CAAC,CAAC;AACpC,CAAC;AAACd,OAAA,CAAAE,aAAA,GAAAA,aAAA"}
@@ -23,6 +23,7 @@ const drawAsPicture = element => {
23
23
  const ctx = new _types.JsiDrawingContext(_skia.Skia, canvas);
24
24
  root.dom.render(ctx);
25
25
  const picture = recorder.finishRecordingAsPicture();
26
+ root.unmount();
26
27
  return picture;
27
28
  };
28
29
  exports.drawAsPicture = drawAsPicture;
@@ -1 +1 @@
1
- {"version":3,"names":["_types","require","_skia","_Platform","_Reconciler","isOnMainThread","_WORKLET","Platform","OS","exports","drawAsPicture","element","recorder","Skia","PictureRecorder","canvas","beginRecording","root","SkiaRoot","render","ctx","JsiDrawingContext","dom","picture","finishRecordingAsPicture","drawAsImage","size","drawAsImageFromPicture","pd","surface","Surface","MakeOffscreen","width","height","getCanvas","scale","drawPicture","flush","image","makeImageSnapshot","makeNonTextureImage"],"sources":["Offscreen.tsx"],"sourcesContent":["import type { ReactElement } from \"react\";\n\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkPicture, SkSize } from \"../skia/types\";\nimport { Skia } from \"../skia\";\nimport { Platform } from \"../Platform\";\n\nimport { SkiaRoot } from \"./Reconciler\";\n\n// We call it main thread because on web main is JS thread\nexport const isOnMainThread = () => {\n \"worklet\";\n return (\n (typeof _WORKLET !== \"undefined\" && _WORKLET === true) ||\n Platform.OS === \"web\"\n );\n};\n\nexport const drawAsPicture = (element: ReactElement) => {\n const recorder = Skia.PictureRecorder();\n const canvas = recorder.beginRecording();\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n const picture = recorder.finishRecordingAsPicture();\n return picture;\n};\n\nexport const drawAsImage = (element: ReactElement, size: SkSize) => {\n return drawAsImageFromPicture(drawAsPicture(element), size);\n};\n\n// TODO: We're not sure yet why PixelRatio is not needed here.\nconst pd = 1;\nexport const drawAsImageFromPicture = (picture: SkPicture, size: SkSize) => {\n \"worklet\";\n const surface = Skia.Surface.MakeOffscreen(\n size.width * pd,\n size.height * pd\n )!;\n const canvas = surface.getCanvas();\n canvas.scale(pd, pd);\n canvas.drawPicture(picture);\n surface.flush();\n const image = surface.makeImageSnapshot();\n // If we are not on the main thread or if we are on Web, we need to make the image non-texture.\n if (!isOnMainThread() || Platform.OS === \"web\") {\n return image.makeNonTextureImage();\n } else {\n return image;\n }\n};\n"],"mappings":";;;;;;A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AAI,CAACM,KAAK,GAAGJ,EAAE,EACfF,IAAI,CAACO,MAAM,GAAGL,EAChB,CAAE;EACF,MAAMb,MAAM,GAAGc,OAAO,CAACK,SAAS,CAAC,CAAC;EAClCnB,MAAM,CAACoB,KAAK,CAACP,EAAE,EAAEA,EAAE,CAAC;EACpBb,MAAM,CAACqB,WAAW,CAACb,OAAO,CAAC;EAC3BM,OAAO,CAACQ,KAAK,CAAC,CAAC;EACf,MAAMC,KAAK,GAAGT,OAAO,CAACU,iBAAiB,CAAC,CAAC;EACzC;EACA,IAAI,CAAClC,cAAc,CAAC,CAAC,IAAIE,kBAAQ,CAACC,EAAE,KAAK,KAAK,EAAE;IAC9C,OAAO8B,KAAK,CAACE,mBAAmB,CAAC,CAAC;EACpC,CAAC,MAAM;IACL,OAAOF,KAAK;EACd;AACF,CAAC;AAAC7B,OAAA,CAAAkB,sBAAA,GAAAA,sBAAA"}
1
+ {"version":3,"names":["_types","require","_skia","_Platform","_Reconciler","isOnMainThread","_WORKLET","Platform","OS","exports","drawAsPicture","element","recorder","Skia","PictureRecorder","canvas","beginRecording","root","SkiaRoot","render","ctx","JsiDrawingContext","dom","picture","finishRecordingAsPicture","unmount","drawAsImage","size","drawAsImageFromPicture","pd","surface","Surface","MakeOffscreen","width","height","getCanvas","scale","drawPicture","flush","image","makeImageSnapshot","makeNonTextureImage"],"sources":["Offscreen.tsx"],"sourcesContent":["import type { ReactElement } from \"react\";\n\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkPicture, SkSize } from \"../skia/types\";\nimport { Skia } from \"../skia\";\nimport { Platform } from \"../Platform\";\n\nimport { SkiaRoot } from \"./Reconciler\";\n\n// We call it main thread because on web main is JS thread\nexport const isOnMainThread = () => {\n \"worklet\";\n return (\n (typeof _WORKLET !== \"undefined\" && _WORKLET === true) ||\n Platform.OS === \"web\"\n );\n};\n\nexport const drawAsPicture = (element: ReactElement) => {\n const recorder = Skia.PictureRecorder();\n const canvas = recorder.beginRecording();\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n const picture = recorder.finishRecordingAsPicture();\n root.unmount();\n return picture;\n};\n\nexport const drawAsImage = (element: ReactElement, size: SkSize) => {\n return drawAsImageFromPicture(drawAsPicture(element), size);\n};\n\n// TODO: We're not sure yet why PixelRatio is not needed here.\nconst pd = 1;\nexport const drawAsImageFromPicture = (picture: SkPicture, size: SkSize) => {\n \"worklet\";\n const surface = Skia.Surface.MakeOffscreen(\n size.width * pd,\n size.height * pd\n )!;\n const canvas = surface.getCanvas();\n canvas.scale(pd, pd);\n canvas.drawPicture(picture);\n surface.flush();\n const image = surface.makeImageSnapshot();\n // If we are not on the main thread or if we are on Web, we need to make the image non-texture.\n if (!isOnMainThread() || Platform.OS === \"web\") {\n return image.makeNonTextureImage();\n } else {\n return image;\n }\n};\n"],"mappings":";;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AAEA,IAAAG,WAAA,GAAAH,OAAA;AAEA;AACO,MAAMI,cAAc,GAAGA,CAAA,KAAM;EAClC,SAAS;;EACT,OACG,OAAOC,QAAQ,KAAK,WAAW,IAAIA,QAAQ,KAAK,IAAI,IACrDC,kBAAQ,CAACC,EAAE,KAAK,KAAK;AAEzB,CAAC;AAACC,OAAA,CAAAJ,cAAA,GAAAA,cAAA;AAEK,MAAMK,aAAa,GAAIC,OAAqB,IAAK;EACtD,MAAMC,QAAQ,GAAGC,UAAI,CAACC,eAAe,CAAC,CAAC;EACvC,MAAMC,MAAM,GAAGH,QAAQ,CAACI,cAAc,CAAC,CAAC;EACxC,MAAMC,IAAI,GAAG,IAAIC,oBAAQ,CAACL,UAAI,EAAE,KAAK,CAAC;EACtCI,IAAI,CAACE,MAAM,CAACR,OAAO,CAAC;EACpB,MAAMS,GAAG,GAAG,IAAIC,wBAAiB,CAACR,UAAI,EAAEE,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, Ima
3
3
  import { HostObject } from "./Host";
4
4
  export declare const toBase64String: (bytes: Uint8Array) => string;
5
5
  export declare class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
6
- constructor(CanvasKit: CanvasKit, ref: Image);
6
+ private releaseCtx?;
7
+ constructor(CanvasKit: CanvasKit, ref: Image, releaseCtx?: (() => void) | undefined);
7
8
  height(): number;
8
9
  width(): number;
9
10
  getImageInfo(): ImageInfo;
@@ -38,10 +38,14 @@ const toBase64String = bytes => {
38
38
  };
39
39
  exports.toBase64String = toBase64String;
40
40
  class JsiSkImage extends _Host.HostObject {
41
- constructor(CanvasKit, ref) {
41
+ constructor(CanvasKit, ref, releaseCtx) {
42
42
  super(CanvasKit, ref, "Image");
43
+ this.releaseCtx = releaseCtx;
43
44
  _defineProperty(this, "dispose", () => {
44
45
  this.ref.delete();
46
+ if (this.releaseCtx) {
47
+ this.releaseCtx();
48
+ }
45
49
  });
46
50
  }
47
51
  height() {
@@ -83,6 +87,9 @@ class JsiSkImage extends _Host.HostObject {
83
87
  const bytes = this.encodeToBytes(fmt, quality);
84
88
  return toBase64String(bytes);
85
89
  }
90
+
91
+ // TODO: this is leaking on Web
92
+ // Add signature with allocated buffer
86
93
  readPixels(srcX, srcY, imageInfo) {
87
94
  var _imageInfo$width, _imageInfo$height;
88
95
  const info = this.getImageInfo();
@@ -96,7 +103,25 @@ class JsiSkImage extends _Host.HostObject {
96
103
  return this.ref.readPixels(srcX !== null && srcX !== void 0 ? srcX : 0, srcY !== null && srcY !== void 0 ? srcY : 0, pxInfo);
97
104
  }
98
105
  makeNonTextureImage() {
99
- return new JsiSkImage(this.CanvasKit, this.CanvasKit.MakeImageFromEncoded(this.encodeToBytes()));
106
+ const partialInfo = this.ref.getImageInfo();
107
+ const colorSpace = this.ref.getColorSpace();
108
+ const info = {
109
+ ...partialInfo,
110
+ colorSpace
111
+ };
112
+ var pixelLen = info.width * info.height * 4;
113
+ const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);
114
+ const pixels = this.ref.readPixels(0, 0, info, pixelPtr, info.width * 4);
115
+ if (!pixels) {
116
+ throw new Error("Could not create image from bytes");
117
+ }
118
+ const img = this.CanvasKit.MakeImage(info, pixels, info.width * 4);
119
+ if (!img) {
120
+ throw new Error("Could not create image from bytes");
121
+ }
122
+ return new JsiSkImage(this.CanvasKit, img, () => {
123
+ this.CanvasKit.Free(pixelPtr);
124
+ });
100
125
  }
101
126
  }
102
127
  exports.JsiSkImage = JsiSkImage;
@@ -1 +1 @@
1
- {"version":3,"names":["_Host","require","_JsiSkMatrix","_JsiSkShader","_defineProperty","obj","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","t","i","_toPrimitive","String","r","e","Symbol","toPrimitive","call","TypeError","Number","toBase64String","bytes","Buffer","from","toString","CHUNK_SIZE","index","length","result","slice","Math","min","fromCharCode","apply","btoa","exports","JsiSkImage","HostObject","constructor","CanvasKit","ref","delete","height","width","getImageInfo","info","colorType","alphaType","makeShaderOptions","tx","ty","fm","mm","localMatrix","JsiSkShader","getEnum","TileMode","FilterMode","MipmapMode","JsiSkMatrix","fromValue","undefined","makeShaderCubic","B","C","encodeToBytes","fmt","quality","ImageFormat","Error","encodeToBase64","readPixels","srcX","srcY","imageInfo","_imageInfo$width","_imageInfo$height","pxInfo","colorSpace","ColorSpace","SRGB","AlphaType","ColorType","makeNonTextureImage","MakeImageFromEncoded"],"sources":["JsiSkImage.ts"],"sourcesContent":["import type {\n CanvasKit,\n ImageInfo as CKImageInfo,\n Image,\n} from \"canvaskit-wasm\";\n\nimport type {\n FilterMode,\n MipmapMode,\n SkImage,\n SkMatrix,\n SkShader,\n TileMode,\n ImageFormat,\n ImageInfo,\n} from \"../types\";\n\nimport { getEnum, HostObject } from \"./Host\";\nimport { JsiSkMatrix } from \"./JsiSkMatrix\";\nimport { JsiSkShader } from \"./JsiSkShader\";\n\n// https://github.com/google/skia/blob/1f193df9b393d50da39570dab77a0bb5d28ec8ef/modules/canvaskit/htmlcanvas/util.js\nexport const toBase64String = (bytes: Uint8Array) => {\n if (typeof Buffer !== \"undefined\") {\n // Are we on node?\n return Buffer.from(bytes).toString(\"base64\");\n } else {\n // From https://stackoverflow.com/a/25644409\n // because the naive solution of\n // btoa(String.fromCharCode.apply(null, bytes));\n // would occasionally throw \"Maximum call stack size exceeded\"\n var CHUNK_SIZE = 0x8000; //arbitrary number\n var index = 0;\n var { length } = bytes;\n var result = \"\";\n var slice;\n while (index < length) {\n slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n result += String.fromCharCode.apply(null, slice as any);\n index += CHUNK_SIZE;\n }\n return btoa(result);\n }\n};\n\nexport class JsiSkImage extends HostObject<Image, \"Image\"> implements SkImage {\n constructor(CanvasKit: CanvasKit, ref: Image) {\n super(CanvasKit, ref, \"Image\");\n }\n\n height() {\n return this.ref.height();\n }\n\n width() {\n return this.ref.width();\n }\n\n getImageInfo(): ImageInfo {\n const info = this.ref.getImageInfo();\n return {\n width: info.width,\n height: info.height,\n colorType: info.colorType.value,\n alphaType: info.alphaType.value,\n };\n }\n\n makeShaderOptions(\n tx: TileMode,\n ty: TileMode,\n fm: FilterMode,\n mm: MipmapMode,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderOptions(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n getEnum(this.CanvasKit.FilterMode, fm),\n getEnum(this.CanvasKit.MipmapMode, mm),\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n makeShaderCubic(\n tx: TileMode,\n ty: TileMode,\n B: number,\n C: number,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderCubic(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n B,\n C,\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n encodeToBytes(fmt?: ImageFormat, quality?: number) {\n let result: Uint8Array | null;\n if (fmt && quality) {\n result = this.ref.encodeToBytes(\n getEnum(this.CanvasKit.ImageFormat, fmt),\n quality\n );\n } else if (fmt) {\n result = this.ref.encodeToBytes(getEnum(this.CanvasKit.ImageFormat, fmt));\n } else {\n result = this.ref.encodeToBytes();\n }\n if (!result) {\n throw new Error(\"encodeToBytes failed\");\n }\n return result;\n }\n\n encodeToBase64(fmt?: ImageFormat, quality?: number) {\n const bytes = this.encodeToBytes(fmt, quality);\n return toBase64String(bytes);\n }\n\n readPixels(srcX?: number, srcY?: number, imageInfo?: ImageInfo) {\n const info = this.getImageInfo();\n const pxInfo: CKImageInfo = {\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n width: imageInfo?.width ?? info.width,\n height: imageInfo?.height ?? info.height,\n alphaType: getEnum(\n this.CanvasKit.AlphaType,\n (imageInfo ?? info).alphaType\n ),\n colorType: getEnum(\n this.CanvasKit.ColorType,\n (imageInfo ?? info).colorType\n ),\n };\n return this.ref.readPixels(srcX ?? 0, srcY ?? 0, pxInfo);\n }\n\n dispose = () => {\n this.ref.delete();\n };\n\n makeNonTextureImage(): SkImage {\n return new JsiSkImage(\n this.CanvasKit,\n this.CanvasKit.MakeImageFromEncoded(this.encodeToBytes())!\n );\n }\n}\n"],"mappings":";;;;;;AAiBA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAA4C,SAAAG,gBAAAC,GAAA,EAAAC,GAAA,EAAAC,KAAA,IAAAD,GAAA,GAAAE,cAAA,CAAAF,GAAA,OAAAA,GAAA,IAAAD,GAAA,IAAAI,MAAA,CAAAC,cAAA,CAAAL,GAAA,EAAAC,GAAA,IAAAC,KAAA,EAAAA,KAAA,EAAAI,UAAA,QAAAC,YAAA,QAAAC,QAAA,oBAAAR,GAAA,CAAAC,GAA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AA,CAAAC,UAAA,GAAAA,UAAA"}
1
+ {"version":3,"names":["_Host","require","_JsiSkMatrix","_JsiSkShader","_defineProperty","obj","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","t","i","_toPrimitive","String","r","e","Symbol","toPrimitive","call","TypeError","Number","toBase64String","bytes","Buffer","from","toString","CHUNK_SIZE","index","length","result","slice","Math","min","fromCharCode","apply","btoa","exports","JsiSkImage","HostObject","constructor","CanvasKit","ref","releaseCtx","delete","height","width","getImageInfo","info","colorType","alphaType","makeShaderOptions","tx","ty","fm","mm","localMatrix","JsiSkShader","getEnum","TileMode","FilterMode","MipmapMode","JsiSkMatrix","fromValue","undefined","makeShaderCubic","B","C","encodeToBytes","fmt","quality","ImageFormat","Error","encodeToBase64","readPixels","srcX","srcY","imageInfo","_imageInfo$width","_imageInfo$height","pxInfo","colorSpace","ColorSpace","SRGB","AlphaType","ColorType","makeNonTextureImage","partialInfo","getColorSpace","pixelLen","pixelPtr","Malloc","Uint8Array","pixels","img","MakeImage","Free"],"sources":["JsiSkImage.ts"],"sourcesContent":["import type {\n CanvasKit,\n ImageInfo as CKImageInfo,\n Image,\n} from \"canvaskit-wasm\";\n\nimport type {\n FilterMode,\n MipmapMode,\n SkImage,\n SkMatrix,\n SkShader,\n TileMode,\n ImageFormat,\n ImageInfo,\n} from \"../types\";\n\nimport { getEnum, HostObject } from \"./Host\";\nimport { JsiSkMatrix } from \"./JsiSkMatrix\";\nimport { JsiSkShader } from \"./JsiSkShader\";\n\n// https://github.com/google/skia/blob/1f193df9b393d50da39570dab77a0bb5d28ec8ef/modules/canvaskit/htmlcanvas/util.js\nexport const toBase64String = (bytes: Uint8Array) => {\n if (typeof Buffer !== \"undefined\") {\n // Are we on node?\n return Buffer.from(bytes).toString(\"base64\");\n } else {\n // From https://stackoverflow.com/a/25644409\n // because the naive solution of\n // btoa(String.fromCharCode.apply(null, bytes));\n // would occasionally throw \"Maximum call stack size exceeded\"\n var CHUNK_SIZE = 0x8000; //arbitrary number\n var index = 0;\n var { length } = bytes;\n var result = \"\";\n var slice;\n while (index < length) {\n slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n result += String.fromCharCode.apply(null, slice as any);\n index += CHUNK_SIZE;\n }\n return btoa(result);\n }\n};\n\nexport class JsiSkImage extends HostObject<Image, \"Image\"> implements SkImage {\n constructor(\n CanvasKit: CanvasKit,\n ref: Image,\n private releaseCtx?: () => void\n ) {\n super(CanvasKit, ref, \"Image\");\n }\n\n height() {\n return this.ref.height();\n }\n\n width() {\n return this.ref.width();\n }\n\n getImageInfo(): ImageInfo {\n const info = this.ref.getImageInfo();\n return {\n width: info.width,\n height: info.height,\n colorType: info.colorType.value,\n alphaType: info.alphaType.value,\n };\n }\n\n makeShaderOptions(\n tx: TileMode,\n ty: TileMode,\n fm: FilterMode,\n mm: MipmapMode,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderOptions(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n getEnum(this.CanvasKit.FilterMode, fm),\n getEnum(this.CanvasKit.MipmapMode, mm),\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n makeShaderCubic(\n tx: TileMode,\n ty: TileMode,\n B: number,\n C: number,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderCubic(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n B,\n C,\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n encodeToBytes(fmt?: ImageFormat, quality?: number) {\n let result: Uint8Array | null;\n if (fmt && quality) {\n result = this.ref.encodeToBytes(\n getEnum(this.CanvasKit.ImageFormat, fmt),\n quality\n );\n } else if (fmt) {\n result = this.ref.encodeToBytes(getEnum(this.CanvasKit.ImageFormat, fmt));\n } else {\n result = this.ref.encodeToBytes();\n }\n if (!result) {\n throw new Error(\"encodeToBytes failed\");\n }\n return result;\n }\n\n encodeToBase64(fmt?: ImageFormat, quality?: number) {\n const bytes = this.encodeToBytes(fmt, quality);\n return toBase64String(bytes);\n }\n\n // TODO: this is leaking on Web\n // Add signature with allocated buffer\n readPixels(srcX?: number, srcY?: number, imageInfo?: ImageInfo) {\n const info = this.getImageInfo();\n const pxInfo: CKImageInfo = {\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n width: imageInfo?.width ?? info.width,\n height: imageInfo?.height ?? info.height,\n alphaType: getEnum(\n this.CanvasKit.AlphaType,\n (imageInfo ?? info).alphaType\n ),\n colorType: getEnum(\n this.CanvasKit.ColorType,\n (imageInfo ?? info).colorType\n ),\n };\n return this.ref.readPixels(srcX ?? 0, srcY ?? 0, pxInfo);\n }\n\n dispose = () => {\n this.ref.delete();\n if (this.releaseCtx) {\n this.releaseCtx();\n }\n };\n\n makeNonTextureImage(): SkImage {\n const partialInfo = this.ref.getImageInfo();\n const colorSpace = this.ref.getColorSpace();\n const info = {\n ...partialInfo,\n colorSpace,\n };\n\n var pixelLen = info.width * info.height * 4;\n const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);\n const pixels = this.ref.readPixels(\n 0,\n 0,\n info,\n pixelPtr,\n info.width * 4\n ) as Uint8Array | null;\n if (!pixels) {\n throw new Error(\"Could not create image from bytes\");\n }\n const img = this.CanvasKit.MakeImage(info, pixels, info.width * 4);\n if (!img) {\n throw new Error(\"Could not create image from bytes\");\n }\n return new JsiSkImage(this.CanvasKit, img, () => {\n this.CanvasKit.Free(pixelPtr);\n });\n }\n}\n"],"mappings":";;;;;;AAiBA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAA4C,SAAAG,gBAAAC,GAAA,EAAAC,GAAA,EAAAC,KAAA,IAAAD,GAA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7 +2,8 @@ import type { CanvasKit, Surface } from "canvaskit-wasm";
2
2
  import type { SkCanvas, SkImage, SkRect, SkSurface } from "../types";
3
3
  import { HostObject } from "./Host";
4
4
  export declare class JsiSkSurface extends HostObject<Surface, "Surface"> implements SkSurface {
5
- constructor(CanvasKit: CanvasKit, ref: Surface);
5
+ private releaseCtx?;
6
+ constructor(CanvasKit: CanvasKit, ref: Surface, releaseCtx?: (() => void) | undefined);
6
7
  dispose: () => void;
7
8
  flush(): void;
8
9
  getCanvas(): SkCanvas;
@@ -12,10 +12,14 @@ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key i
12
12
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
13
13
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
14
14
  class JsiSkSurface extends _Host.HostObject {
15
- constructor(CanvasKit, ref) {
15
+ constructor(CanvasKit, ref, releaseCtx) {
16
16
  super(CanvasKit, ref, "Surface");
17
+ this.releaseCtx = releaseCtx;
17
18
  _defineProperty(this, "dispose", () => {
18
19
  this.ref.delete();
20
+ if (this.releaseCtx) {
21
+ this.releaseCtx();
22
+ }
19
23
  });
20
24
  }
21
25
  flush() {
@@ -1 +1 @@
1
- {"version":3,"names":["_Host","require","_JsiSkCanvas","_JsiSkImage","_JsiSkRect","_defineProperty","obj","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","t","i","_toPrimitive","String","r","e","Symbol","toPrimitive","call","TypeError","Number","JsiSkSurface","HostObject","constructor","CanvasKit","ref","delete","flush","getCanvas","JsiSkCanvas","makeImageSnapshot","bounds","image","Array","from","JsiSkRect","fromValue","undefined","JsiSkImage","exports"],"sources":["JsiSkSurface.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SkCanvas, SkImage, SkRect, SkSurface } from \"../types\";\n\nimport { HostObject } from \"./Host\";\nimport { JsiSkCanvas } from \"./JsiSkCanvas\";\nimport { JsiSkImage } from \"./JsiSkImage\";\nimport { JsiSkRect } from \"./JsiSkRect\";\n\nexport class JsiSkSurface\n extends HostObject<Surface, \"Surface\">\n implements SkSurface\n{\n constructor(CanvasKit: CanvasKit, ref: Surface) {\n super(CanvasKit, ref, \"Surface\");\n }\n\n dispose = () => {\n this.ref.delete();\n };\n\n flush() {\n this.ref.flush();\n }\n\n getCanvas(): SkCanvas {\n return new JsiSkCanvas(this.CanvasKit, this.ref.getCanvas());\n }\n\n makeImageSnapshot(bounds?: SkRect): SkImage {\n const image = this.ref.makeImageSnapshot(\n bounds\n ? Array.from(JsiSkRect.fromValue(this.CanvasKit, bounds))\n : undefined\n );\n return new JsiSkImage(this.CanvasKit, image);\n }\n}\n"],"mappings":";;;;;;AAIA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AAAwC,SAAAI,gBAAAC,GAAA,EAAAC,GAAA,EAAAC,KAAA,IAAAD,GAAA,GAAAE,cAAA,CAAAF,GAAA,OAAAA,GAAA,IAAAD,GAAA,IAAAI,MAAA,CAAAC,cAAA,CAAAL,GAAA,EAAAC,GAAA,IAAAC,KAAA,EAAAA,KAAA,EAAAI,UAAA,QAAAC,YAAA,QAAAC,QAAA,oBAAAR,GAAA,CAAAC,GAAA,IAAAC,KAAA,WAAAF,GAAA;AAAA,SAAAG,eAAAM,CAAA,QAAAC,CAAA,GAAAC,YAAA,CAAAF,CAAA,uCAAAC,CAAA,GAAAA,CAAA,GAAAE,MAAA,CAAAF,CAAA;AAAA,SAAAC,aAAAF,CAAA,EAAAI,CAAA,2BAAAJ,CAAA,KAAAA,CAAA,SAAAA,CAAA,MAAAK,CAAA,GAAAL,CAAA,CAAAM,MAAA,CAAAC,WAAA,kBAAAF,CAAA,QAAAJ,CAAA,GAAAI,CAAA,CAAAG,IAAA,CAAAR,CAAA,EAAAI,CAAA,uCAAAH,CAAA,SAAAA,CAAA,YAAAQ,SAAA,yEAAAL,CAAA,GAAAD,MAAA,GAAAO,MAAA,EAAAV,CAAA;AAEjC,MAAMW,YAAY,SACfC,gBAAU,CAEpB;EACEC,WAAWA,CAACC,SAAoB,EAAEC,GAAY,EAAE;IAC9C,KAAK,CAACD,SAAS,EAAEC,GAAG,EAAE,SAAS,CAAC;IAACzB,eAAA,kBAGzB,MAAM;MACd,IAAI,CAACyB,GAAG,CAACC,MAAM,CAAC,CAAC;IACnB,CAAC;EAJD;EAMAC,KAAKA,CAAA,EAAG;IACN,IAAI,CAACF,GAAG,CAACE,KAAK,CAAC,CAAC;EAClB;EAEAC,SAASA,CAAA,EAAa;IACpB,OAAO,IAAIC,wBAAW,CAAC,IAAI,CAACL,SAAS,EAAE,IAAI,CAACC,GAAG,CAACG,SAAS,CAAC,CAAC,CAAC;EAC9D;EAEAE,iBAAiBA,CAACC,MAAe,EAAW;IAC1C,MAAMC,KAAK,GAAG,IAAI,CAACP,GAAG,CAACK,iBAAiB,CACtCC,MAAM,GACFE,KAAK,CAACC,IAAI,CAACC,oBAAS,CAACC,SAAS,CAAC,IAAI,CAACZ,SAAS,EAAEO,MAAM,CAAC,CAAC,GACvDM,SACN,CAAC;IACD,OAAO,IAAIC,sBAAU,CAAC,IAAI,CAACd,SAAS,EAAEQ,KAAK,CAAC;EAC9C;AACF;AAACO,OAAA,CAAAlB,YAAA,GAAAA,YAAA"}
1
+ {"version":3,"names":["_Host","require","_JsiSkCanvas","_JsiSkImage","_JsiSkRect","_defineProperty","obj","key","value","_toPropertyKey","Object","defineProperty","enumerable","configurable","writable","t","i","_toPrimitive","String","r","e","Symbol","toPrimitive","call","TypeError","Number","JsiSkSurface","HostObject","constructor","CanvasKit","ref","releaseCtx","delete","flush","getCanvas","JsiSkCanvas","makeImageSnapshot","bounds","image","Array","from","JsiSkRect","fromValue","undefined","JsiSkImage","exports"],"sources":["JsiSkSurface.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SkCanvas, SkImage, SkRect, SkSurface } from \"../types\";\n\nimport { HostObject } from \"./Host\";\nimport { JsiSkCanvas } from \"./JsiSkCanvas\";\nimport { JsiSkImage } from \"./JsiSkImage\";\nimport { JsiSkRect } from \"./JsiSkRect\";\n\nexport class JsiSkSurface\n extends HostObject<Surface, \"Surface\">\n implements SkSurface\n{\n constructor(\n CanvasKit: CanvasKit,\n ref: Surface,\n private releaseCtx?: () => void\n ) {\n super(CanvasKit, ref, \"Surface\");\n }\n\n dispose = () => {\n this.ref.delete();\n if (this.releaseCtx) {\n this.releaseCtx();\n }\n };\n\n flush() {\n this.ref.flush();\n }\n\n getCanvas(): SkCanvas {\n return new JsiSkCanvas(this.CanvasKit, this.ref.getCanvas());\n }\n\n makeImageSnapshot(bounds?: SkRect): SkImage {\n const image = this.ref.makeImageSnapshot(\n bounds\n ? Array.from(JsiSkRect.fromValue(this.CanvasKit, bounds))\n : undefined\n );\n return new JsiSkImage(this.CanvasKit, image);\n }\n}\n"],"mappings":";;;;;;AAIA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AACA,IAAAG,UAAA,GAAAH,OAAA;AAAwC,SAAAI,gBAAAC,GAAA,EAAAC,GAAA,EAAAC,KAAA,IAAAD,GAAA,GAAAE,cAAA,CAAAF,GAAA,OAAAA,GAAA,IAAAD,GAAA,IAAAI,MAAA,CAAAC,cAAA,CAAAL,GAAA,EAAAC,GAAA,IAAAC,KAAA,EAAAA,KAAA,EAAAI,UAAA,QAAAC,YAAA,QAAAC,QAAA,oBAAAR,GAAA,CAAAC,GAAA,IAAAC,KAAA,WAAAF,GAAA;AAAA,SAAAG,eAAAM,CAAA,QAAAC,CAA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IAAI,CAACf,SAAS,EAAES,KAAK,CAAC;EAC9C;AACF;AAACO,OAAA,CAAAnB,YAAA,GAAAA,YAAA"}
@@ -11,11 +11,22 @@ class JsiSkSurfaceFactory extends _Host.Host {
11
11
  super(CanvasKit);
12
12
  }
13
13
  Make(width, height) {
14
- const surface = this.CanvasKit.MakeSurface(width, height);
14
+ var pixelLen = width * height * 4;
15
+ const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);
16
+ const surface = this.CanvasKit.MakeRasterDirectSurface({
17
+ width: width,
18
+ height: height,
19
+ colorType: this.CanvasKit.ColorType.RGBA_8888,
20
+ alphaType: this.CanvasKit.AlphaType.Unpremul,
21
+ colorSpace: this.CanvasKit.ColorSpace.SRGB
22
+ }, pixelPtr, width * 4);
15
23
  if (!surface) {
16
24
  return null;
17
25
  }
18
- return new _JsiSkSurface.JsiSkSurface(this.CanvasKit, surface);
26
+ surface.getCanvas().clear(this.CanvasKit.TRANSPARENT);
27
+ return new _JsiSkSurface.JsiSkSurface(this.CanvasKit, surface, () => {
28
+ this.CanvasKit.Free(pixelPtr);
29
+ });
19
30
  }
20
31
  MakeOffscreen(width, height) {
21
32
  // OffscreenCanvas may be unvailable in some environments.
@@ -1 +1 @@
1
- {"version":3,"names":["_Host","require","_JsiSkSurface","JsiSkSurfaceFactory","Host","constructor","CanvasKit","Make","width","height","surface","MakeSurface","JsiSkSurface","MakeOffscreen","OC","globalThis","OffscreenCanvas","undefined","offscreen","webglContext","GetWebGLContext","grContext","MakeWebGLContext","Error","MakeRenderTarget","exports"],"sources":["JsiSkSurfaceFactory.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SurfaceFactory } from \"../types\";\n\nimport { Host } from \"./Host\";\nimport { JsiSkSurface } from \"./JsiSkSurface\";\n\nexport class JsiSkSurfaceFactory extends Host implements SurfaceFactory {\n constructor(CanvasKit: CanvasKit) {\n super(CanvasKit);\n }\n\n Make(width: number, height: number) {\n const surface = this.CanvasKit.MakeSurface(width, height);\n if (!surface) {\n return null;\n }\n return new JsiSkSurface(this.CanvasKit, surface);\n }\n\n MakeOffscreen(width: number, height: number) {\n // OffscreenCanvas may be unvailable in some environments.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const OC = (globalThis as any).OffscreenCanvas;\n let surface: Surface | null;\n if (OC === undefined) {\n surface = this.CanvasKit.MakeSurface(width, height);\n } else {\n const offscreen = new OC(width, height);\n const webglContext = this.CanvasKit.GetWebGLContext(offscreen);\n const grContext = this.CanvasKit.MakeWebGLContext(webglContext);\n if (!grContext) {\n throw new Error(\"Could not make a graphics context\");\n }\n surface = this.CanvasKit.MakeRenderTarget(grContext, width, height);\n }\n if (!surface) {\n return null;\n }\n return new JsiSkSurface(this.CanvasKit, surface);\n }\n}\n"],"mappings":";;;;;;AAIA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAD,OAAA;AAEO,MAAME,mBAAmB,SAASC,UAAI,CAA2B;EACtEC,WAAWA,CAACC,SAAoB,EAAE;IAChC,KAAK,CAACA,SAAS,CAAC;EAClB;EAEAC,IAAIA,CAACC,KAAa,EAAEC,MAAc,EAAE;IAClC,MAAMC,OAAO,GAAG,IAAI,CAACJ,SAAS,CAACK,WAAW,CAACH,KAAK,EAAEC,MAAM,CAAC;IACzD,IAAI,CAACC,OAAO,EAAE;MACZ,OAAO,IAAI;IACb;IACA,OAAO,IAAIE,0BAAY,CAAC,IAAI,CAACN,SAAS,EAAEI,OAAO,CAAC;EAClD;EAEAG,aAAaA,CAACL,KAAa,EAAEC,MAAc,EAAE;IAC3C;IACA;IACA,MAAMK,EAAE,GAAIC,UAAU,CAASC,eAAe;IAC9C,IAAIN,OAAuB;IAC3B,IAAII,EAAE,KAAKG,SAAS,EAAE;MACpBP,OAAO,GAAG,IAAI,CAACJ,SAAS,CAACK,WAAW,CAACH,KAAK,EAAEC,MAAM,CAAC;IACrD,CAAC,MAAM;MACL,MAAMS,SAAS,GAAG,IAAIJ,EAAE,CAACN,KAAK,EAAEC,MAAM,CAAC;MACvC,MAAMU,YAAY,GAAG,IAAI,CAACb,SAAS,CAACc,eAAe,CAACF,SAAS,CAAC;MAC9D,MAAMG,SAAS,GAAG,IAAI,CAACf,SAAS,CAACgB,gBAAgB,CAACH,YAAY,CAAC;MAC/D,IAAI,CAACE,SAAS,EAAE;QACd,MAAM,IAAIE,KAAK,CAAC,mCAAmC,CAAC;MACtD;MACAb,OAAO,GAAG,IAAI,CAACJ,SAAS,CAACkB,gBAAgB,CAACH,SAAS,EAAEb,KAAK,EAAEC,MAAM,CAAC;IACrE;IACA,IAAI,CAACC,OAAO,EAAE;MACZ,OAAO,IAAI;IACb;IACA,OAAO,IAAIE,0BAAY,CAAC,IAAI,CAACN,SAAS,EAAEI,OAAO,CAAC;EAClD;AACF;AAACe,OAAA,CAAAtB,mBAAA,GAAAA,mBAAA"}
1
+ {"version":3,"names":["_Host","require","_JsiSkSurface","JsiSkSurfaceFactory","Host","constructor","CanvasKit","Make","width","height","pixelLen","pixelPtr","Malloc","Uint8Array","surface","MakeRasterDirectSurface","colorType","ColorType","RGBA_8888","alphaType","AlphaType","Unpremul","colorSpace","ColorSpace","SRGB","getCanvas","clear","TRANSPARENT","JsiSkSurface","Free","MakeOffscreen","OC","globalThis","OffscreenCanvas","undefined","MakeSurface","offscreen","webglContext","GetWebGLContext","grContext","MakeWebGLContext","Error","MakeRenderTarget","exports"],"sources":["JsiSkSurfaceFactory.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SurfaceFactory } from \"../types\";\n\nimport { Host } from \"./Host\";\nimport { JsiSkSurface } from \"./JsiSkSurface\";\n\nexport class JsiSkSurfaceFactory extends Host implements SurfaceFactory {\n constructor(CanvasKit: CanvasKit) {\n super(CanvasKit);\n }\n\n Make(width: number, height: number) {\n var pixelLen = width * height * 4;\n const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);\n const surface = this.CanvasKit.MakeRasterDirectSurface(\n {\n width: width,\n height: height,\n colorType: this.CanvasKit.ColorType.RGBA_8888,\n alphaType: this.CanvasKit.AlphaType.Unpremul,\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n },\n pixelPtr,\n width * 4\n );\n if (!surface) {\n return null;\n }\n surface.getCanvas().clear(this.CanvasKit.TRANSPARENT);\n return new JsiSkSurface(this.CanvasKit, surface, () => {\n this.CanvasKit.Free(pixelPtr);\n });\n }\n\n MakeOffscreen(width: number, height: number) {\n // OffscreenCanvas may be unvailable in some environments.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const OC = (globalThis as any).OffscreenCanvas;\n let surface: Surface | null;\n if (OC === undefined) {\n surface = this.CanvasKit.MakeSurface(width, height);\n } else {\n const offscreen = new OC(width, height);\n const webglContext = this.CanvasKit.GetWebGLContext(offscreen);\n const grContext = this.CanvasKit.MakeWebGLContext(webglContext);\n if (!grContext) {\n throw new Error(\"Could not make a graphics context\");\n }\n surface = this.CanvasKit.MakeRenderTarget(grContext, width, height);\n }\n if (!surface) {\n return null;\n }\n return new JsiSkSurface(this.CanvasKit, surface);\n }\n}\n"],"mappings":";;;;;;AAIA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAD,OAAA;AAEO,MAAME,mBAAmB,SAASC,UAAI,CAA2B;EACtEC,WAAWA,CAACC,SAAoB,EAAE;IAChC,KAAK,CAACA,SAAS,CAAC;EAClB;EAEAC,IAAIA,CAACC,KAAa,EAAEC,MAAc,EAAE;IAClC,IAAIC,QAAQ,GAAGF,KAAK,GAAGC,MAAM,GAAG,CAAC;IACjC,MAAME,QAAQ,GAAG,IAAI,CAACL,SAAS,CAACM,MAAM,CAACC,UAAU,EAAEH,QAAQ,CAAC;IAC5D,MAAMI,OAAO,GAAG,IAAI,CAACR,SAAS,CAACS,uBAAuB,CACpD;MACEP,KAAK,EAAEA,KAAK;MACZC,MAAM,EAAEA,MAAM;MACdO,SAAS,EAAE,IAAI,CAACV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Z,OAAO,IAAI;IACb;IACA,OAAO,IAAIc,0BAAY,CAAC,IAAI,CAACtB,SAAS,EAAEQ,OAAO,CAAC;EAClD;AACF;AAAC6B,OAAA,CAAAxC,mBAAA,GAAAA,mBAAA"}
@@ -1,5 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { SkSurface } from "../skia";
3
- export * from "../renderer/components";
4
- export declare const makeOffscreenSurface: (width: number, height: number) => any;
3
+ export declare const makeOffscreenSurface: (width: number, height: number) => SkSurface;
4
+ export declare const getSkiaExports: () => {
5
+ Skia: import("../skia/types").Skia;
6
+ };
5
7
  export declare const drawOffscreen: (surface: SkSurface, element: ReactNode) => import("../skia").SkImage;
@@ -4,9 +4,6 @@
4
4
  import { JsiSkApi } from "../skia/web";
5
5
  import { SkiaRoot } from "../renderer/Reconciler";
6
6
  import { JsiDrawingContext } from "../dom/types";
7
- export * from "../renderer/components";
8
-
9
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
7
  let Skia;
11
8
  export const makeOffscreenSurface = (width, height) => {
12
9
  if (!Skia) {
@@ -18,12 +15,21 @@ export const makeOffscreenSurface = (width, height) => {
18
15
  }
19
16
  return surface;
20
17
  };
18
+ export const getSkiaExports = () => {
19
+ if (!Skia) {
20
+ Skia = JsiSkApi(CanvasKit);
21
+ }
22
+ return {
23
+ Skia
24
+ };
25
+ };
21
26
  export const drawOffscreen = (surface, element) => {
22
27
  const root = new SkiaRoot(Skia, false);
23
28
  root.render(element);
24
29
  const canvas = surface.getCanvas();
25
30
  const ctx = new JsiDrawingContext(Skia, canvas);
26
31
  root.dom.render(ctx);
32
+ root.unmount();
27
33
  surface.flush();
28
34
  return surface.makeImageSnapshot();
29
35
  };
@@ -1 +1 @@
1
- {"version":3,"names":["JsiSkApi","SkiaRoot","JsiDrawingContext","Skia","makeOffscreenSurface","width","height","CanvasKit","surface","Surface","MakeOffscreen","Error","drawOffscreen","element","root","render","canvas","getCanvas","ctx","dom","flush","makeImageSnapshot"],"sources":["index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport CanvasKitInit from \"canvaskit-wasm/bin/full/canvaskit\";\nimport type { ReactNode } from \"react\";\n\nimport { JsiSkApi } from \"../skia/web\";\nimport { SkiaRoot } from \"../renderer/Reconciler\";\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkSurface } from \"../skia\";\n\nexport * from \"../renderer/components\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nlet Skia: any;\n\nexport const makeOffscreenSurface = (width: number, height: number) => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n const surface = Skia.Surface.MakeOffscreen(width, height);\n if (surface === null) {\n throw new Error(\"Couldn't create surface!\");\n }\n return surface;\n};\n\nexport const drawOffscreen = (surface: SkSurface, element: ReactNode) => {\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const canvas = surface.getCanvas();\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n surface.flush();\n return surface.makeImageSnapshot();\n};\n"],"mappings":"AAAA;AACA;;AAIA,SAASA,QAAQ,QAAQ,aAAa;AACtC,SAASC,QAAQ,QAAQ,wBAAwB;AACjD,SAASC,iBAAiB,QAAQ,cAAc;AAGhD,cAAc,wBAAwB;;AAEtC;AACA,IAAIC,IAAS;AAEb,OAAO,MAAMC,oBAAoB,GAAGA,CAACC,KAAa,EAAEC,MAAc,KAAK;EACrE,IAAI,CAACH,IAAI,EAAE;IACTA,IAAI,GAAGH,QAAQ,CAACO,SAAS,CAAC;EAC5B;EACA,MAAMC,OAAO,GAAGL,IAAI,CAACM,OAAO,CAACC,aAAa,CAACL,KAAK,EAAEC,MAAM,CAAC;EACzD,IAAIE,OAAO,KAAK,IAAI,EAAE;IACpB,MAAM,IAAIG,KAAK,CAAC,0BAA0B,CAAC;EAC7C;EACA,OAAOH,OAAO;AAChB,CAAC;AAED,OAAO,MAAMI,aAAa,GAAGA,CAACJ,OAAkB,EAAEK,OAAkB,KAAK;EACvE,MAAMC,IAAI,GAAG,IAAIb,QAAQ,CAACE,IAAI,EAAE,KAAK,CAAC;EACtCW,IAAI,CAACC,MAAM,CAACF,OAAO,CAAC;EACpB,MAAMG,MAAM,GAAGR,OAAO,CAACS,SAAS,CAAC,CAAC;EAClC,MAAMC,GAAG,GAAG,IAAIhB,iBAAiB,CAACC,IAAI,EAAEa,MAAM,CAAC;EAC/CF,IAAI,CAACK,GAAG,CAACJ,MAAM,CAACG,GAAG,CAAC;EACpBV,OAAO,CAACY,KAAK,CAAC,CAAC;EACf,OAAOZ,OAAO,CAACa,iBAAiB,CAAC,CAAC;AACpC,CAAC"}
1
+ {"version":3,"names":["JsiSkApi","SkiaRoot","JsiDrawingContext","Skia","makeOffscreenSurface","width","height","CanvasKit","surface","Surface","MakeOffscreen","Error","getSkiaExports","drawOffscreen","element","root","render","canvas","getCanvas","ctx","dom","unmount","flush","makeImageSnapshot"],"sources":["index.ts"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n// @ts-ignore\nimport CanvasKitInit from \"canvaskit-wasm/bin/full/canvaskit\";\nimport type { ReactNode } from \"react\";\n\nimport { JsiSkApi } from \"../skia/web\";\nimport { SkiaRoot } from \"../renderer/Reconciler\";\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkSurface } from \"../skia\";\n\nlet Skia: ReturnType<typeof JsiSkApi>;\n\nexport const makeOffscreenSurface = (width: number, height: number) => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n const surface = Skia.Surface.MakeOffscreen(width, height);\n if (surface === null) {\n throw new Error(\"Couldn't create surface!\");\n }\n return surface;\n};\n\nexport const getSkiaExports = () => {\n if (!Skia) {\n Skia = JsiSkApi(CanvasKit);\n }\n return { Skia };\n};\n\nexport const drawOffscreen = (surface: SkSurface, element: ReactNode) => {\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const canvas = surface.getCanvas();\n const ctx = new JsiDrawingContext(Skia, can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 -17,6 +17,7 @@ export const drawAsPicture = element => {
17
17
  const ctx = new JsiDrawingContext(Skia, canvas);
18
18
  root.dom.render(ctx);
19
19
  const picture = recorder.finishRecordingAsPicture();
20
+ root.unmount();
20
21
  return picture;
21
22
  };
22
23
  export const drawAsImage = (element, size) => {
@@ -1 +1 @@
1
- {"version":3,"names":["JsiDrawingContext","Skia","Platform","SkiaRoot","isOnMainThread","_WORKLET","OS","drawAsPicture","element","recorder","PictureRecorder","canvas","beginRecording","root","render","ctx","dom","picture","finishRecordingAsPicture","drawAsImage","size","drawAsImageFromPicture","pd","surface","Surface","MakeOffscreen","width","height","getCanvas","scale","drawPicture","flush","image","makeImageSnapshot","makeNonTextureImage"],"sources":["Offscreen.tsx"],"sourcesContent":["import type { ReactElement } from \"react\";\n\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkPicture, SkSize } from \"../skia/types\";\nimport { Skia } from \"../skia\";\nimport { Platform } from \"../Platform\";\n\nimport { SkiaRoot } from \"./Reconciler\";\n\n// We call it main thread because on web main is JS thread\nexport const isOnMainThread = () => {\n \"worklet\";\n return (\n (typeof _WORKLET !== \"undefined\" && _WORKLET === true) ||\n Platform.OS === \"web\"\n );\n};\n\nexport const drawAsPicture = (element: ReactElement) => {\n const recorder = Skia.PictureRecorder();\n const canvas = recorder.beginRecording();\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n const picture = recorder.finishRecordingAsPicture();\n return picture;\n};\n\nexport const drawAsImage = (element: ReactElement, size: SkSize) => {\n return drawAsImageFromPicture(drawAsPicture(element), size);\n};\n\n// TODO: We're not sure yet why PixelRatio is not needed here.\nconst pd = 1;\nexport const drawAsImageFromPicture = (picture: SkPicture, size: SkSize) => {\n \"worklet\";\n const surface = Skia.Surface.MakeOffscreen(\n size.width * pd,\n size.height * pd\n )!;\n const canvas = surface.getCanvas();\n canvas.scale(pd, pd);\n canvas.drawPicture(picture);\n surface.flush();\n const image = surface.makeImageSnapshot();\n // If we are not on the main thread or if we are on Web, we need to make the image non-texture.\n if (!isOnMainThread() || Platform.OS === \"web\") {\n return image.makeNonTextureImage();\n } else {\n return image;\n }\n};\n"],"mappings":"AAEA,SAASA,iBAAiB,QAAQ,cAAc;AAEhD,SAASC,IAAI,QAAQ,SAAS;AAC9B,SAASC,QAAQ,QAAQ,aAAa;AAEtC,SAASC,QAAQ,QAAQ,cAAc;;AAEvC;AACA,OAAO,MAAMC,cAAc,GAAGA,CAAA,KAAM;EAClC,SAAS;;EACT,OACG,OAAOC,QAAQ,KAAK,WAAW,IAAIA,QAAQ,KAAK,IAAI,IACrDH,QAAQ,CAACI,EAAE,KAAK,KAAK;AAEzB,CAAC;AAED,OAAO,MAAMC,aAAa,GAAIC,OAAqB,IAAK;EACtD,MAAMC,QAAQ,GAAGR,IAAI,CAACS,eAAe,CAAC,CAAC;EACvC,MAAMC,MAAM,GAAGF,QAAQ,CAACG,cAAc,CAAC,CAAC;EACxC,MAAMC,IAAI,GAAG,IAAIV,QAAQ,CAACF,IAAI,EAAE,KAAK,CAAC;EACtCY,IAAI,CAACC,MAAM,CAACN,OAAO,CAAC;EACpB,MAAMO,GAAG,GAAG,IAAIf,iBAAiB,CAACC,IAAI,EAAEU,MAAM,CAAC;EAC/CE,IAAI,CAACG,GAAG,CAACF,MAAM,CAACC,GAAG,CAAC;EACpB,MAAME,OAAO,GAAGR,QAAQ,CAACS,wBAAwB,CAAC,CAAC;EACnD,OAAOD,OAAO;AAChB,CAAC;AAED,OAAO,MAAME,WAAW,GAAGA,CAACX,OAAqB,EAAEY,IAAY,KAAK;EAClE,OAAOC,sBAAsB,CAACd,aAAa,CAACC,OAAO,CAAC,EAAEY,IAAI,CAAC;AAC7D,CAAC;;AAED;AACA,MAAME,EAAE,GAAG,CAAC;AACZ,OAAO,MAAMD,sBAAsB,GAAGA,CAACJ,OAAkB,EAAEG,IAAY,KAAK;EAC1E,SAAS;;EACT,MAAMG,OAAO,GAAGtB,IAAI,CAACuB,OAAO,CAACC,aAAa,CACxCL,IAAI,CAACM,KAAK,GAAGJ,EAAE,EACfF,IAAI,CAACO,MAAM,GAAGL,EAChB,CAAE;EACF,MAAMX,MAAM,GAAGY,OAAO,CAACK,SAAS,CAAC,CAAC;EAClCjB,MAAM,CAACkB,KAAK,CAACP,EAAE,EAAEA,EAAE,CAAC;EACpBX,MAAM,CAACmB,WAAW,CAACb,OAAO,CAAC;EAC3BM,OAAO,CAACQ,KAAK,CAAC,CAAC;EACf,MAAMC,KAAK,GAAGT,OAAO,CAACU,iBAAiB,CAAC,CAAC;EACzC;EACA,IAAI,CAAC7B,cAAc,CAAC,CAAC,IAAIF,QAAQ,CAACI,EAAE,KAAK,KAAK,EAAE;IAC9C,OAAO0B,KAAK,CAACE,mBAAmB,CAAC,CAAC;EACpC,CAAC,MAAM;IACL,OAAOF,KAAK;EACd;AACF,CAAC"}
1
+ {"version":3,"names":["JsiDrawingContext","Skia","Platform","SkiaRoot","isOnMainThread","_WORKLET","OS","drawAsPicture","element","recorder","PictureRecorder","canvas","beginRecording","root","render","ctx","dom","picture","finishRecordingAsPicture","unmount","drawAsImage","size","drawAsImageFromPicture","pd","surface","Surface","MakeOffscreen","width","height","getCanvas","scale","drawPicture","flush","image","makeImageSnapshot","makeNonTextureImage"],"sources":["Offscreen.tsx"],"sourcesContent":["import type { ReactElement } from \"react\";\n\nimport { JsiDrawingContext } from \"../dom/types\";\nimport type { SkPicture, SkSize } from \"../skia/types\";\nimport { Skia } from \"../skia\";\nimport { Platform } from \"../Platform\";\n\nimport { SkiaRoot } from \"./Reconciler\";\n\n// We call it main thread because on web main is JS thread\nexport const isOnMainThread = () => {\n \"worklet\";\n return (\n (typeof _WORKLET !== \"undefined\" && _WORKLET === true) ||\n Platform.OS === \"web\"\n );\n};\n\nexport const drawAsPicture = (element: ReactElement) => {\n const recorder = Skia.PictureRecorder();\n const canvas = recorder.beginRecording();\n const root = new SkiaRoot(Skia, false);\n root.render(element);\n const ctx = new JsiDrawingContext(Skia, canvas);\n root.dom.render(ctx);\n const picture = recorder.finishRecordingAsPicture();\n root.unmount();\n return picture;\n};\n\nexport const drawAsImage = (element: ReactElement, size: SkSize) => {\n return drawAsImageFromPicture(drawAsPicture(element), size);\n};\n\n// TODO: We're not sure yet why PixelRatio is not needed here.\nconst pd = 1;\nexport const drawAsImageFromPicture = (picture: SkPicture, size: SkSize) => {\n \"worklet\";\n const surface = Skia.Surface.MakeOffscreen(\n size.width * pd,\n size.height * pd\n )!;\n const canvas = surface.getCanvas();\n canvas.scale(pd, pd);\n canvas.drawPicture(picture);\n surface.flush();\n const image = surface.makeImageSnapshot();\n // If we are not on the main thread or if we are on Web, we need to make the image non-texture.\n if (!isOnMainThread() || Platform.OS === \"web\") {\n return image.makeNonTextureImage();\n } else {\n return image;\n }\n};\n"],"mappings":"AAEA,SAASA,iBAAiB,QAAQ,cAAc;AAEhD,SAASC,IAAI,QAAQ,SAAS;AAC9B,SAASC,QAAQ,QAAQ,aAAa;AAEtC,SAASC,QAAQ,QAAQ,cAAc;;AAEvC;AACA,OAAO,MAAMC,cAAc,GAAGA,CAAA,KAAM;EAClC,SAAS;;EACT,OACG,OAAOC,QAAQ,KAAK,WAAW,IAAIA,QAAQ,KAAK,IAAI,IACrDH,QAAQ,CAACI,EAAE,KAAK,KAAK;AAEzB,CAAC;AAED,OAAO,MAAMC,aAAa,GAAIC,OAAqB,IAAK;EACtD,MAAMC,QAAQ,GAAGR,IAAI,CAACS,eAAe,CAAC,CAAC;EACvC,MAAMC,MAAM,GAAGF,QAAQ,CAACG,cAAc,CAAC,CAAC;EACxC,MAAMC,IAAI,GAAG,IAAIV,QAAQ,CAACF,IAAI,EAAE,KAAK,CAAC;EACtCY,IAAI,CAACC,MAAM,CAACN,OAAO,CAAC;EACpB,MAAMO,GAAG,GAAG,IAAIf,iBAAiB,CAACC,IAAI,EAAEU,MAAM,CAAC;EAC/CE,IAAI,CAACG,GAAG,CAACF,MAAM,CAACC,GAAG,CAAC;EACpB,MAAME,OAAO,GAAGR,QAAQ,CAACS,wBAAwB,CAAC,CAAC;EACnDL,IAAI,CAACM,OAAO,CAAC,CAAC;EACd,OAAOF,OAAO;AAChB,CAAC;AAED,OAAO,MAAMG,WAAW,GAAGA,CAACZ,OAAqB,EAAEa,IAAY,KAAK;EAClE,OAAOC,sBAAsB,CAACf,aAAa,CAACC,OAAO,CAAC,EAAEa,IAAI,CAAC;AAC7D,CAAC;;AAED;AACA,MAAME,EAAE,GAAG,CAAC;AACZ,OAAO,MAAMD,sBAAsB,GAAGA,CAACL,OAAkB,EAAEI,IAAY,KAAK;EAC1E,SAAS;;EACT,MAAMG,OAAO,GAAGvB,IAAI,CAACwB,OAAO,CAACC,aAAa,CACxCL,IAAI,CAACM,KAAK,GAAGJ,EAAE,EACfF,IAAI,CAACO,MAAM,GAAGL,EAChB,CAAE;EACF,MAAMZ,MAAM,GAAGa,OAAO,CAACK,SAAS,CAAC,CAAC;EAClClB,MAAM,CAACmB,KAAK,CAACP,EAAE,EAAEA,EAAE,CAAC;EACpBZ,MAAM,CAACoB,WAAW,CAACd,OAAO,CAAC;EAC3BO,OAAO,CAACQ,KAAK,CAAC,CAAC;EACf,MAAMC,KAAK,GAAGT,OAAO,CAACU,iBAAiB,CAAC,CAAC;EACzC;EACA,IAAI,CAAC9B,cAAc,CAAC,CAAC,IAAIF,QAAQ,CAACI,EAAE,KAAK,KAAK,EAAE;IAC9C,OAAO2B,KAAK,CAACE,mBAAmB,CAAC,CAAC;EACpC,CAAC,MAAM;IACL,OAAOF,KAAK;EACd;AACF,CAAC"}
@@ -3,7 +3,8 @@ import type { FilterMode, MipmapMode, SkImage, SkMatrix, SkShader, TileMode, Ima
3
3
  import { HostObject } from "./Host";
4
4
  export declare const toBase64String: (bytes: Uint8Array) => string;
5
5
  export declare class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
6
- constructor(CanvasKit: CanvasKit, ref: Image);
6
+ private releaseCtx?;
7
+ constructor(CanvasKit: CanvasKit, ref: Image, releaseCtx?: (() => void) | undefined);
7
8
  height(): number;
8
9
  width(): number;
9
10
  getImageInfo(): ImageInfo;
@@ -32,10 +32,14 @@ export const toBase64String = bytes => {
32
32
  }
33
33
  };
34
34
  export class JsiSkImage extends HostObject {
35
- constructor(CanvasKit, ref) {
35
+ constructor(CanvasKit, ref, releaseCtx) {
36
36
  super(CanvasKit, ref, "Image");
37
+ this.releaseCtx = releaseCtx;
37
38
  _defineProperty(this, "dispose", () => {
38
39
  this.ref.delete();
40
+ if (this.releaseCtx) {
41
+ this.releaseCtx();
42
+ }
39
43
  });
40
44
  }
41
45
  height() {
@@ -77,6 +81,9 @@ export class JsiSkImage extends HostObject {
77
81
  const bytes = this.encodeToBytes(fmt, quality);
78
82
  return toBase64String(bytes);
79
83
  }
84
+
85
+ // TODO: this is leaking on Web
86
+ // Add signature with allocated buffer
80
87
  readPixels(srcX, srcY, imageInfo) {
81
88
  var _imageInfo$width, _imageInfo$height;
82
89
  const info = this.getImageInfo();
@@ -90,7 +97,25 @@ export class JsiSkImage extends HostObject {
90
97
  return this.ref.readPixels(srcX !== null && srcX !== void 0 ? srcX : 0, srcY !== null && srcY !== void 0 ? srcY : 0, pxInfo);
91
98
  }
92
99
  makeNonTextureImage() {
93
- return new JsiSkImage(this.CanvasKit, this.CanvasKit.MakeImageFromEncoded(this.encodeToBytes()));
100
+ const partialInfo = this.ref.getImageInfo();
101
+ const colorSpace = this.ref.getColorSpace();
102
+ const info = {
103
+ ...partialInfo,
104
+ colorSpace
105
+ };
106
+ var pixelLen = info.width * info.height * 4;
107
+ const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);
108
+ const pixels = this.ref.readPixels(0, 0, info, pixelPtr, info.width * 4);
109
+ if (!pixels) {
110
+ throw new Error("Could not create image from bytes");
111
+ }
112
+ const img = this.CanvasKit.MakeImage(info, pixels, info.width * 4);
113
+ if (!img) {
114
+ throw new Error("Could not create image from bytes");
115
+ }
116
+ return new JsiSkImage(this.CanvasKit, img, () => {
117
+ this.CanvasKit.Free(pixelPtr);
118
+ });
94
119
  }
95
120
  }
96
121
  //# sourceMappingURL=JsiSkImage.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["getEnum","HostObject","JsiSkMatrix","JsiSkShader","toBase64String","bytes","Buffer","from","toString","CHUNK_SIZE","index","length","result","slice","Math","min","String","fromCharCode","apply","btoa","JsiSkImage","constructor","CanvasKit","ref","_defineProperty","delete","height","width","getImageInfo","info","colorType","value","alphaType","makeShaderOptions","tx","ty","fm","mm","localMatrix","TileMode","FilterMode","MipmapMode","fromValue","undefined","makeShaderCubic","B","C","encodeToBytes","fmt","quality","ImageFormat","Error","encodeToBase64","readPixels","srcX","srcY","imageInfo","_imageInfo$width","_imageInfo$height","pxInfo","colorSpace","ColorSpace","SRGB","AlphaType","ColorType","makeNonTextureImage","MakeImageFromEncoded"],"sources":["JsiSkImage.ts"],"sourcesContent":["import type {\n CanvasKit,\n ImageInfo as CKImageInfo,\n Image,\n} from \"canvaskit-wasm\";\n\nimport type {\n FilterMode,\n MipmapMode,\n SkImage,\n SkMatrix,\n SkShader,\n TileMode,\n ImageFormat,\n ImageInfo,\n} from \"../types\";\n\nimport { getEnum, HostObject } from \"./Host\";\nimport { JsiSkMatrix } from \"./JsiSkMatrix\";\nimport { JsiSkShader } from \"./JsiSkShader\";\n\n// https://github.com/google/skia/blob/1f193df9b393d50da39570dab77a0bb5d28ec8ef/modules/canvaskit/htmlcanvas/util.js\nexport const toBase64String = (bytes: Uint8Array) => {\n if (typeof Buffer !== \"undefined\") {\n // Are we on node?\n return Buffer.from(bytes).toString(\"base64\");\n } else {\n // From https://stackoverflow.com/a/25644409\n // because the naive solution of\n // btoa(String.fromCharCode.apply(null, bytes));\n // would occasionally throw \"Maximum call stack size exceeded\"\n var CHUNK_SIZE = 0x8000; //arbitrary number\n var index = 0;\n var { length } = bytes;\n var result = \"\";\n var slice;\n while (index < length) {\n slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n result += String.fromCharCode.apply(null, slice as any);\n index += CHUNK_SIZE;\n }\n return btoa(result);\n }\n};\n\nexport class JsiSkImage extends HostObject<Image, \"Image\"> implements SkImage {\n constructor(CanvasKit: CanvasKit, ref: Image) {\n super(CanvasKit, ref, \"Image\");\n }\n\n height() {\n return this.ref.height();\n }\n\n width() {\n return this.ref.width();\n }\n\n getImageInfo(): ImageInfo {\n const info = this.ref.getImageInfo();\n return {\n width: info.width,\n height: info.height,\n colorType: info.colorType.value,\n alphaType: info.alphaType.value,\n };\n }\n\n makeShaderOptions(\n tx: TileMode,\n ty: TileMode,\n fm: FilterMode,\n mm: MipmapMode,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderOptions(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n getEnum(this.CanvasKit.FilterMode, fm),\n getEnum(this.CanvasKit.MipmapMode, mm),\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n makeShaderCubic(\n tx: TileMode,\n ty: TileMode,\n B: number,\n C: number,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderCubic(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n B,\n C,\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n encodeToBytes(fmt?: ImageFormat, quality?: number) {\n let result: Uint8Array | null;\n if (fmt && quality) {\n result = this.ref.encodeToBytes(\n getEnum(this.CanvasKit.ImageFormat, fmt),\n quality\n );\n } else if (fmt) {\n result = this.ref.encodeToBytes(getEnum(this.CanvasKit.ImageFormat, fmt));\n } else {\n result = this.ref.encodeToBytes();\n }\n if (!result) {\n throw new Error(\"encodeToBytes failed\");\n }\n return result;\n }\n\n encodeToBase64(fmt?: ImageFormat, quality?: number) {\n const bytes = this.encodeToBytes(fmt, quality);\n return toBase64String(bytes);\n }\n\n readPixels(srcX?: number, srcY?: number, imageInfo?: ImageInfo) {\n const info = this.getImageInfo();\n const pxInfo: CKImageInfo = {\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n width: imageInfo?.width ?? info.width,\n height: imageInfo?.height ?? info.height,\n alphaType: getEnum(\n this.CanvasKit.AlphaType,\n (imageInfo ?? info).alphaType\n ),\n colorType: getEnum(\n this.CanvasKit.ColorType,\n (imageInfo ?? info).colorType\n ),\n };\n return this.ref.readPixels(srcX ?? 0, srcY ?? 0, pxInfo);\n }\n\n dispose = () => {\n this.ref.delete();\n };\n\n makeNonTextureImage(): SkImage {\n return new JsiSkImage(\n this.CanvasKit,\n this.CanvasKit.MakeImageFromEncoded(this.encodeToBytes())!\n );\n }\n}\n"],"mappings":";;;AAiBA,SAASA,OAAO,EAAEC,UAAU,QAAQ,QAAQ;AAC5C,SAASC,WAAW,QAAQ,eAAe;AAC3C,SAASC,WAAW,QAAQ,eAAe;;AAE3C;AACA,OAAO,MAAMC,cAAc,GAAIC,KAAiB,IAAK;EACnD,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;IACjC;IACA,OAAOA,MAAM,CAACC,IAAI,CAACF,KAAK,CAAC,CAACG,QAAQ,CAAC,QAAQ,CAAC;EAC9C,CAAC,MAAM;IACL;IACA;IACA;IACA;IACA,IAAIC,UAAU,GAAG,MAAM,CAAC,CAAC;IACzB,IAAIC,KAAK,GAAG,CAAC;IACb,IAAI;MAAEC;IAAO,CAAC,GAAGN,KAAK;IACtB,IAAIO,MAAM,GAAG,EAAE;IACf,IAAIC,KAAK;IACT,OAAOH,KAAK,GAAGC,MAAM,EAAE;MACrBE,KAAK,GAAGR,KAAK,CAACQ,KAAK,CAACH,KAAK,EAAEI,IAAI,CAACC,GAAG,CAACL,KAAK,GAAGD,UAAU,EAAEE,MAAM,CAAC,CAAC;MAChE;MACAC,MAAM,IAAII,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d,IAAI,CAACA,SAAS,CAAC4C,oBAAoB,CAAC,IAAI,CAACnB,aAAa,CAAC,CAAC,CAC1D,CAAC;EACH;AACF"}
1
+ {"version":3,"names":["getEnum","HostObject","JsiSkMatrix","JsiSkShader","toBase64String","bytes","Buffer","from","toString","CHUNK_SIZE","index","length","result","slice","Math","min","String","fromCharCode","apply","btoa","JsiSkImage","constructor","CanvasKit","ref","releaseCtx","_defineProperty","delete","height","width","getImageInfo","info","colorType","value","alphaType","makeShaderOptions","tx","ty","fm","mm","localMatrix","TileMode","FilterMode","MipmapMode","fromValue","undefined","makeShaderCubic","B","C","encodeToBytes","fmt","quality","ImageFormat","Error","encodeToBase64","readPixels","srcX","srcY","imageInfo","_imageInfo$width","_imageInfo$height","pxInfo","colorSpace","ColorSpace","SRGB","AlphaType","ColorType","makeNonTextureImage","partialInfo","getColorSpace","pixelLen","pixelPtr","Malloc","Uint8Array","pixels","img","MakeImage","Free"],"sources":["JsiSkImage.ts"],"sourcesContent":["import type {\n CanvasKit,\n ImageInfo as CKImageInfo,\n Image,\n} from \"canvaskit-wasm\";\n\nimport type {\n FilterMode,\n MipmapMode,\n SkImage,\n SkMatrix,\n SkShader,\n TileMode,\n ImageFormat,\n ImageInfo,\n} from \"../types\";\n\nimport { getEnum, HostObject } from \"./Host\";\nimport { JsiSkMatrix } from \"./JsiSkMatrix\";\nimport { JsiSkShader } from \"./JsiSkShader\";\n\n// https://github.com/google/skia/blob/1f193df9b393d50da39570dab77a0bb5d28ec8ef/modules/canvaskit/htmlcanvas/util.js\nexport const toBase64String = (bytes: Uint8Array) => {\n if (typeof Buffer !== \"undefined\") {\n // Are we on node?\n return Buffer.from(bytes).toString(\"base64\");\n } else {\n // From https://stackoverflow.com/a/25644409\n // because the naive solution of\n // btoa(String.fromCharCode.apply(null, bytes));\n // would occasionally throw \"Maximum call stack size exceeded\"\n var CHUNK_SIZE = 0x8000; //arbitrary number\n var index = 0;\n var { length } = bytes;\n var result = \"\";\n var slice;\n while (index < length) {\n slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n result += String.fromCharCode.apply(null, slice as any);\n index += CHUNK_SIZE;\n }\n return btoa(result);\n }\n};\n\nexport class JsiSkImage extends HostObject<Image, \"Image\"> implements SkImage {\n constructor(\n CanvasKit: CanvasKit,\n ref: Image,\n private releaseCtx?: () => void\n ) {\n super(CanvasKit, ref, \"Image\");\n }\n\n height() {\n return this.ref.height();\n }\n\n width() {\n return this.ref.width();\n }\n\n getImageInfo(): ImageInfo {\n const info = this.ref.getImageInfo();\n return {\n width: info.width,\n height: info.height,\n colorType: info.colorType.value,\n alphaType: info.alphaType.value,\n };\n }\n\n makeShaderOptions(\n tx: TileMode,\n ty: TileMode,\n fm: FilterMode,\n mm: MipmapMode,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderOptions(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n getEnum(this.CanvasKit.FilterMode, fm),\n getEnum(this.CanvasKit.MipmapMode, mm),\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n makeShaderCubic(\n tx: TileMode,\n ty: TileMode,\n B: number,\n C: number,\n localMatrix?: SkMatrix\n ): SkShader {\n return new JsiSkShader(\n this.CanvasKit,\n this.ref.makeShaderCubic(\n getEnum(this.CanvasKit.TileMode, tx),\n getEnum(this.CanvasKit.TileMode, ty),\n B,\n C,\n localMatrix ? JsiSkMatrix.fromValue(localMatrix) : undefined\n )\n );\n }\n\n encodeToBytes(fmt?: ImageFormat, quality?: number) {\n let result: Uint8Array | null;\n if (fmt && quality) {\n result = this.ref.encodeToBytes(\n getEnum(this.CanvasKit.ImageFormat, fmt),\n quality\n );\n } else if (fmt) {\n result = this.ref.encodeToBytes(getEnum(this.CanvasKit.ImageFormat, fmt));\n } else {\n result = this.ref.encodeToBytes();\n }\n if (!result) {\n throw new Error(\"encodeToBytes failed\");\n }\n return result;\n }\n\n encodeToBase64(fmt?: ImageFormat, quality?: number) {\n const bytes = this.encodeToBytes(fmt, quality);\n return toBase64String(bytes);\n }\n\n // TODO: this is leaking on Web\n // Add signature with allocated buffer\n readPixels(srcX?: number, srcY?: number, imageInfo?: ImageInfo) {\n const info = this.getImageInfo();\n const pxInfo: CKImageInfo = {\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n width: imageInfo?.width ?? info.width,\n height: imageInfo?.height ?? info.height,\n alphaType: getEnum(\n this.CanvasKit.AlphaType,\n (imageInfo ?? info).alphaType\n ),\n colorType: getEnum(\n this.CanvasKit.ColorType,\n (imageInfo ?? info).colorType\n ),\n };\n return this.ref.readPixels(srcX ?? 0, srcY ?? 0, pxInfo);\n }\n\n dispose = () => {\n this.ref.delete();\n if (this.releaseCtx) {\n this.releaseCtx();\n }\n };\n\n makeNonTextureImage(): SkImage {\n const partialInfo = this.ref.getImageInfo();\n const colorSpace = this.ref.getColorSpace();\n const info = {\n ...partialInfo,\n colorSpace,\n };\n\n var pixelLen = info.width * info.height * 4;\n const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);\n const pixels = this.ref.readPixels(\n 0,\n 0,\n info,\n pixelPtr,\n info.width * 4\n ) as Uint8Array | null;\n if (!pixels) {\n throw new Error(\"Could not create image from bytes\");\n }\n const img = this.CanvasKit.MakeImage(info, pixels, info.width * 4);\n if (!img) {\n throw new Error(\"Could not create image from bytes\");\n }\n return new JsiSkImage(this.CanvasKit, img, () => {\n this.CanvasKit.Free(pixelPtr);\n });\n }\n}\n"],"mappings":";;;AAiBA,SAASA,OAAO,EAAEC,UAAU,QAAQ,QAAQ;AAC5C,SAASC,WAAW,QAAQ,eAAe;AAC3C,SAASC,WAAW,QAAQ,eAAe;;AAE3C;AACA,OAAO,MAAMC,cAAc,GAAIC,KAAiB,IAAK;EACnD,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE;IACjC;IACA,OA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AAS,CAAC7C,IAAI,EAAE2C,MAAM,EAAE3C,IAAI,CAACF,KAAK,GAAG,CAAC,CAAC;IAClE,IAAI,CAAC8C,GAAG,EAAE;MACR,MAAM,IAAItB,KAAK,CAAC,mCAAmC,CAAC;IACtD;IACA,OAAO,IAAIhC,UAAU,CAAC,IAAI,CAACE,SAAS,EAAEoD,GAAG,EAAE,MAAM;MAC/C,IAAI,CAACpD,SAAS,CAACsD,IAAI,CAACN,QAAQ,CAAC;IAC/B,CAAC,CAAC;EACJ;AACF"}
@@ -2,7 +2,8 @@ import type { CanvasKit, Surface } from "canvaskit-wasm";
2
2
  import type { SkCanvas, SkImage, SkRect, SkSurface } from "../types";
3
3
  import { HostObject } from "./Host";
4
4
  export declare class JsiSkSurface extends HostObject<Surface, "Surface"> implements SkSurface {
5
- constructor(CanvasKit: CanvasKit, ref: Surface);
5
+ private releaseCtx?;
6
+ constructor(CanvasKit: CanvasKit, ref: Surface, releaseCtx?: (() => void) | undefined);
6
7
  dispose: () => void;
7
8
  flush(): void;
8
9
  getCanvas(): SkCanvas;
@@ -6,10 +6,14 @@ import { JsiSkCanvas } from "./JsiSkCanvas";
6
6
  import { JsiSkImage } from "./JsiSkImage";
7
7
  import { JsiSkRect } from "./JsiSkRect";
8
8
  export class JsiSkSurface extends HostObject {
9
- constructor(CanvasKit, ref) {
9
+ constructor(CanvasKit, ref, releaseCtx) {
10
10
  super(CanvasKit, ref, "Surface");
11
+ this.releaseCtx = releaseCtx;
11
12
  _defineProperty(this, "dispose", () => {
12
13
  this.ref.delete();
14
+ if (this.releaseCtx) {
15
+ this.releaseCtx();
16
+ }
13
17
  });
14
18
  }
15
19
  flush() {
@@ -1 +1 @@
1
- {"version":3,"names":["HostObject","JsiSkCanvas","JsiSkImage","JsiSkRect","JsiSkSurface","constructor","CanvasKit","ref","_defineProperty","delete","flush","getCanvas","makeImageSnapshot","bounds","image","Array","from","fromValue","undefined"],"sources":["JsiSkSurface.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SkCanvas, SkImage, SkRect, SkSurface } from \"../types\";\n\nimport { HostObject } from \"./Host\";\nimport { JsiSkCanvas } from \"./JsiSkCanvas\";\nimport { JsiSkImage } from \"./JsiSkImage\";\nimport { JsiSkRect } from \"./JsiSkRect\";\n\nexport class JsiSkSurface\n extends HostObject<Surface, \"Surface\">\n implements SkSurface\n{\n constructor(CanvasKit: CanvasKit, ref: Surface) {\n super(CanvasKit, ref, \"Surface\");\n }\n\n dispose = () => {\n this.ref.delete();\n };\n\n flush() {\n this.ref.flush();\n }\n\n getCanvas(): SkCanvas {\n return new JsiSkCanvas(this.CanvasKit, this.ref.getCanvas());\n }\n\n makeImageSnapshot(bounds?: SkRect): SkImage {\n const image = this.ref.makeImageSnapshot(\n bounds\n ? Array.from(JsiSkRect.fromValue(this.CanvasKit, bounds))\n : undefined\n );\n return new JsiSkImage(this.CanvasKit, image);\n }\n}\n"],"mappings":";;;AAIA,SAASA,UAAU,QAAQ,QAAQ;AACnC,SAASC,WAAW,QAAQ,eAAe;AAC3C,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,SAAS,QAAQ,aAAa;AAEvC,OAAO,MAAMC,YAAY,SACfJ,UAAU,CAEpB;EACEK,WAAWA,CAACC,SAAoB,EAAEC,GAAY,EAAE;IAC9C,KAAK,CAACD,SAAS,EAAEC,GAAG,EAAE,SAAS,CAAC;IAACC,eAAA,kBAGzB,MAAM;MACd,IAAI,CAACD,GAAG,CAACE,MAAM,CAAC,CAAC;IACnB,CAAC;EAJD;EAMAC,KAAKA,CAAA,EAAG;IACN,IAAI,CAACH,GAAG,CAACG,KAAK,CAAC,CAAC;EAClB;EAEAC,SAASA,CAAA,EAAa;IACpB,OAAO,IAAIV,WAAW,CAAC,IAAI,CAACK,SAAS,EAAE,IAAI,CAACC,GAAG,CAACI,SAAS,CAAC,CAAC,CAAC;EAC9D;EAEAC,iBAAiBA,CAACC,MAAe,EAAW;IAC1C,MAAMC,KAAK,GAAG,IAAI,CAACP,GAAG,CAACK,iBAAiB,CACtCC,MAAM,GACFE,KAAK,CAACC,IAAI,CAACb,SAAS,CAACc,SAAS,CAAC,IAAI,CAACX,SAAS,EAAEO,MAAM,CAAC,CAAC,GACvDK,SACN,CAAC;IACD,OAAO,IAAIhB,UAAU,CAAC,IAAI,CAACI,SAAS,EAAEQ,KAAK,CAAC;EAC9C;AACF"}
1
+ {"version":3,"names":["HostObject","JsiSkCanvas","JsiSkImage","JsiSkRect","JsiSkSurface","constructor","CanvasKit","ref","releaseCtx","_defineProperty","delete","flush","getCanvas","makeImageSnapshot","bounds","image","Array","from","fromValue","undefined"],"sources":["JsiSkSurface.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SkCanvas, SkImage, SkRect, SkSurface } from \"../types\";\n\nimport { HostObject } from \"./Host\";\nimport { JsiSkCanvas } from \"./JsiSkCanvas\";\nimport { JsiSkImage } from \"./JsiSkImage\";\nimport { JsiSkRect } from \"./JsiSkRect\";\n\nexport class JsiSkSurface\n extends HostObject<Surface, \"Surface\">\n implements SkSurface\n{\n constructor(\n CanvasKit: CanvasKit,\n ref: Surface,\n private releaseCtx?: () => void\n ) {\n super(CanvasKit, ref, \"Surface\");\n }\n\n dispose = () => {\n this.ref.delete();\n if (this.releaseCtx) {\n this.releaseCtx();\n }\n };\n\n flush() {\n this.ref.flush();\n }\n\n getCanvas(): SkCanvas {\n return new JsiSkCanvas(this.CanvasKit, this.ref.getCanvas());\n }\n\n makeImageSnapshot(bounds?: SkRect): SkImage {\n const image = this.ref.makeImageSnapshot(\n bounds\n ? Array.from(JsiSkRect.fromValue(this.CanvasKit, bounds))\n : undefined\n );\n return new JsiSkImage(this.CanvasKit, image);\n }\n}\n"],"mappings":";;;AAIA,SAASA,UAAU,QAAQ,QAAQ;AACnC,SAASC,WAAW,QAAQ,eAAe;AAC3C,SAASC,UAAU,QAAQ,cAAc;AACzC,SAASC,SAAS,QAAQ,aAAa;AAEvC,OAAO,MAAMC,YAAY,SACfJ,UAAU,CAEpB;EACEK,WAAWA,CACTC,SAAoB,EACpBC,GAAY,EACJC,UAAuB,EAC/B;IACA,KAAK,CAACF,SAAS,EAAEC,GAAG,EAAE,SAAS,CAAC;IAAC,KAFzBC,UAAuB,GAAvBA,UAAuB;IAAAC,eAAA,kBAKvB,MAAM;MACd,IAAI,CAACF,GAAG,CAACG,MAAM,CAAC,CAAC;MACjB,IAAI,IAAI,CAACF,UAAU,EAAE;QACnB,IAAI,CAACA,UAAU,CAAC,CAAC;MACnB;IACF,CAAC;EAPD;EASAG,KAAKA,CAAA,EAAG;IACN,IAAI,CAACJ,GAAG,CAACI,KAAK,CAAC,CAAC;EAClB;EAEAC,SAASA,CAAA,EAAa;IACpB,OAAO,IAAIX,WAAW,CAAC,IAAI,CAACK,SAAS,EAAE,IAAI,CAACC,GAAG,CAACK,SAAS,CAAC,CAAC,CAAC;EAC9D;EAEAC,iBAAiBA,CAACC,MAAe,EAAW;IAC1C,MAAMC,KAAK,GAAG,IAAI,CAACR,GAAG,CAACM,iBAAiB,CACtCC,MAAM,GACFE,KAAK,CAACC,IAAI,CAACd,SAAS,CAACe,SAAS,CAAC,IAAI,CAACZ,SAAS,EAAEQ,MAAM,CAAC,CAAC,GACvDK,SACN,CAAC;IACD,OAAO,IAAIjB,UAAU,CAAC,IAAI,CAACI,SAAS,EAAES,KAAK,CAAC;EAC9C;AACF"}
@@ -5,11 +5,22 @@ export class JsiSkSurfaceFactory extends Host {
5
5
  super(CanvasKit);
6
6
  }
7
7
  Make(width, height) {
8
- const surface = this.CanvasKit.MakeSurface(width, height);
8
+ var pixelLen = width * height * 4;
9
+ const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);
10
+ const surface = this.CanvasKit.MakeRasterDirectSurface({
11
+ width: width,
12
+ height: height,
13
+ colorType: this.CanvasKit.ColorType.RGBA_8888,
14
+ alphaType: this.CanvasKit.AlphaType.Unpremul,
15
+ colorSpace: this.CanvasKit.ColorSpace.SRGB
16
+ }, pixelPtr, width * 4);
9
17
  if (!surface) {
10
18
  return null;
11
19
  }
12
- return new JsiSkSurface(this.CanvasKit, surface);
20
+ surface.getCanvas().clear(this.CanvasKit.TRANSPARENT);
21
+ return new JsiSkSurface(this.CanvasKit, surface, () => {
22
+ this.CanvasKit.Free(pixelPtr);
23
+ });
13
24
  }
14
25
  MakeOffscreen(width, height) {
15
26
  // OffscreenCanvas may be unvailable in some environments.
@@ -1 +1 @@
1
- {"version":3,"names":["Host","JsiSkSurface","JsiSkSurfaceFactory","constructor","CanvasKit","Make","width","height","surface","MakeSurface","MakeOffscreen","OC","globalThis","OffscreenCanvas","undefined","offscreen","webglContext","GetWebGLContext","grContext","MakeWebGLContext","Error","MakeRenderTarget"],"sources":["JsiSkSurfaceFactory.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SurfaceFactory } from \"../types\";\n\nimport { Host } from \"./Host\";\nimport { JsiSkSurface } from \"./JsiSkSurface\";\n\nexport class JsiSkSurfaceFactory extends Host implements SurfaceFactory {\n constructor(CanvasKit: CanvasKit) {\n super(CanvasKit);\n }\n\n Make(width: number, height: number) {\n const surface = this.CanvasKit.MakeSurface(width, height);\n if (!surface) {\n return null;\n }\n return new JsiSkSurface(this.CanvasKit, surface);\n }\n\n MakeOffscreen(width: number, height: number) {\n // OffscreenCanvas may be unvailable in some environments.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const OC = (globalThis as any).OffscreenCanvas;\n let surface: Surface | null;\n if (OC === undefined) {\n surface = this.CanvasKit.MakeSurface(width, height);\n } else {\n const offscreen = new OC(width, height);\n const webglContext = this.CanvasKit.GetWebGLContext(offscreen);\n const grContext = this.CanvasKit.MakeWebGLContext(webglContext);\n if (!grContext) {\n throw new Error(\"Could not make a graphics context\");\n }\n surface = this.CanvasKit.MakeRenderTarget(grContext, width, height);\n }\n if (!surface) {\n return null;\n }\n return new JsiSkSurface(this.Canv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I,CAACG,SAAS,EAAEI,OAAO,CAAC;EAClD;AACF"}
1
+ {"version":3,"names":["Host","JsiSkSurface","JsiSkSurfaceFactory","constructor","CanvasKit","Make","width","height","pixelLen","pixelPtr","Malloc","Uint8Array","surface","MakeRasterDirectSurface","colorType","ColorType","RGBA_8888","alphaType","AlphaType","Unpremul","colorSpace","ColorSpace","SRGB","getCanvas","clear","TRANSPARENT","Free","MakeOffscreen","OC","globalThis","OffscreenCanvas","undefined","MakeSurface","offscreen","webglContext","GetWebGLContext","grContext","MakeWebGLContext","Error","MakeRenderTarget"],"sources":["JsiSkSurfaceFactory.ts"],"sourcesContent":["import type { CanvasKit, Surface } from \"canvaskit-wasm\";\n\nimport type { SurfaceFactory } from \"../types\";\n\nimport { Host } from \"./Host\";\nimport { JsiSkSurface } from \"./JsiSkSurface\";\n\nexport class JsiSkSurfaceFactory extends Host implements SurfaceFactory {\n constructor(CanvasKit: CanvasKit) {\n super(CanvasKit);\n }\n\n Make(width: number, height: number) {\n var pixelLen = width * height * 4;\n const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);\n const surface = this.CanvasKit.MakeRasterDirectSurface(\n {\n width: width,\n height: height,\n colorType: this.CanvasKit.ColorType.RGBA_8888,\n alphaType: this.CanvasKit.AlphaType.Unpremul,\n colorSpace: this.CanvasKit.ColorSpace.SRGB,\n },\n pixelPtr,\n width * 4\n );\n if (!surface) {\n return null;\n }\n surface.getCanvas().clear(this.CanvasKit.TRANSPARENT);\n return new JsiSkSurface(this.CanvasKit, surface, () => {\n this.CanvasKit.Free(pixelPtr);\n });\n }\n\n MakeOffscreen(width: number, height: number) {\n // OffscreenCanvas may be unvailable in some environments.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const OC = (globalThis as any).OffscreenCanvas;\n let surface: Surface | null;\n if (OC === undefined) {\n surface = this.CanvasKit.MakeSurface(width, height);\n } else {\n const offscreen = new OC(width, height);\n const webglContext = this.CanvasKit.GetWebGLContext(offscreen);\n const grContext = this.CanvasKit.MakeWebGLContext(webglContext);\n if (!grContext) {\n throw new Error(\"Could not make a graphics context\");\n }\n surface = this.CanvasKit.MakeRenderTarget(grContext, width, height);\n }\n if (!surface) {\n return null;\n }\n return new JsiSkSurface(this.CanvasKit, surface);\n }\n}\n"],"mappings":"AAIA,SAASA,IAAI,QAAQ,QAAQ;AAC7B,SAASC,YAAY,QAAQ,gBAAgB;AAE7C,OAAO,MAAMC,mBAAmB,SAASF,IAAI,CAA2B;EACtEG,WAAWA,CAACC,SAAoB,EAAE;IAChC,KAAK,CAACA,SAAS,CAAC;EAClB;EAEAC,IAAIA,CAACC,KAAa,EAAEC,MAAc,EAAE;IAClC,IAAIC,QAAQ,GAAGF,KAAK,GAAGC,MAAM,GAAG,CAAC;IACjC,MAAME,QAAQ,GAAG,IAAI,CAACL,SAAS,CAACM,MAAM,CAACC,UAAU,EAAEH,QAAQ,CAAC;IAC5D,MAAMI,OAAO,GAAG,IAAI,CAACR,SAAS,CAACS,uBAAuB,CACpD;MACEP,KAAK,EAAEA,KAAK;MACZC,MAAM,EAAEA,MAAM;MACdO,SAAS,EAAE,IAAI,CAACV,SAAS,CAACW,SAAS,CAACC,SAAS;MAC7CC,SAAS,EAAE,IAAI,CAACb,SAAS,CAACc,SAAS,CAACC,QAAQ;MAC5CC,UAAU,EAAE,IAAI,CAAChB,SAAS,CAACiB,UAAU,CAACC;IACxC,CAAC,EACDb,QAAQ,EACRH,KAAK,GAAG,CACV,CAAC;IACD,IAAI,CAACM,OAAO,EAAE;MACZ,OAAO,IAAI;IACb;IACAA,OAAO,CAACW,SAAS,CAAC,CAAC,CAACC,KAAK,CAAC,IAAI,CAACpB,SAAS,CAACqB,WAAW,CAAC;IACrD,OAAO,IAAIxB,YAAY,CAAC,IAAI,CAACG,SAAS,EAAEQ,OAAO,EAAE,MAAM;MACrD,IAAI,CAACR,SAAS,CAACsB,IAAI,CAACjB,QAAQ,CAAC;IAC/B,CAAC,CAAC;EACJ;EAEAkB,aAAaA,CAACrB,KAAa,EAAEC,MAAc,EAAE;IAC3C;IACA;IACA,MAAMqB,EAAE,GAAIC,UAAU,CAASC,eAAe;IAC9C,IAAIlB,OAAuB;IAC3B,IAAIgB,EAAE,KAAKG,SAAS,EAAE;MACpBnB,OAAO,GAAG,IAAI,CAACR,SAAS,CAAC4B,WAAW,CAAC1B,KAAK,EAAEC,MAAM,CAAC;IACrD,CAAC,MAAM;MACL,MAAM0B,SAAS,GAAG,IAAIL,EAAE,CAACtB,KAAK,EAAEC,MAAM,CAAC;MACvC,MAAM2B,YAAY,GAAG,IAAI,CAAC9B,SAAS,CAAC+B,eAAe,CAACF,SAAS,CAAC;MAC9D,MAAMG,SAAS,GAAG,IAAI,CAAChC,SAAS,CAACiC,gBAAgB,CAACH,YAAY,CAAC;MAC/D,IAAI,CAACE,SAAS,EAAE;QACd,MAAM,IAAIE,KAAK,CAAC,mCAAmC,CAAC;MACtD;MACA1B,OAAO,GAAG,IAAI,CAACR,SAAS,CAACmC,gBAAgB,CAACH,SAAS,EAAE9B,KAAK,EAAEC,MAAM,CAAC;IACrE;IACA,IAAI,CAACK,OAAO,EAAE;MACZ,OAAO,IAAI;IACb;IACA,OAAO,IAAIX,YAAY,CAAC,IAAI,CAACG,SAAS,EAAEQ,OAAO,CAAC;EAClD;AACF"}
@@ -1,5 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { SkSurface } from "../skia";
3
- export * from "../renderer/components";
4
- export declare const makeOffscreenSurface: (width: number, height: number) => any;
3
+ export declare const makeOffscreenSurface: (width: number, height: number) => SkSurface;
4
+ export declare const getSkiaExports: () => {
5
+ Skia: import("../skia/types").Skia;
6
+ };
5
7
  export declare const drawOffscreen: (surface: SkSurface, element: ReactNode) => import("../skia").SkImage;
@@ -3,7 +3,8 @@ import type { FilterMode, MipmapMode, SkImage, SkMatrix, SkShader, TileMode, Ima
3
3
  import { HostObject } from "./Host";
4
4
  export declare const toBase64String: (bytes: Uint8Array) => string;
5
5
  export declare class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
6
- constructor(CanvasKit: CanvasKit, ref: Image);
6
+ private releaseCtx?;
7
+ constructor(CanvasKit: CanvasKit, ref: Image, releaseCtx?: (() => void) | undefined);
7
8
  height(): number;
8
9
  width(): number;
9
10
  getImageInfo(): ImageInfo;
@@ -2,7 +2,8 @@ import type { CanvasKit, Surface } from "canvaskit-wasm";
2
2
  import type { SkCanvas, SkImage, SkRect, SkSurface } from "../types";
3
3
  import { HostObject } from "./Host";
4
4
  export declare class JsiSkSurface extends HostObject<Surface, "Surface"> implements SkSurface {
5
- constructor(CanvasKit: CanvasKit, ref: Surface);
5
+ private releaseCtx?;
6
+ constructor(CanvasKit: CanvasKit, ref: Surface, releaseCtx?: (() => void) | undefined);
6
7
  dispose: () => void;
7
8
  flush(): void;
8
9
  getCanvas(): SkCanvas;
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "setup-skia-web": "./scripts/setup-canvaskit.js"
8
8
  },
9
9
  "title": "React Native Skia",
10
- "version": "1.0.4",
10
+ "version": "1.0.5",
11
11
  "description": "High-performance React Native Graphics using Skia",
12
12
  "main": "lib/module/index.js",
13
13
  "react-native": "src/index.ts",
@@ -8,10 +8,7 @@ import { SkiaRoot } from "../renderer/Reconciler";
8
8
  import { JsiDrawingContext } from "../dom/types";
9
9
  import type { SkSurface } from "../skia";
10
10
 
11
- export * from "../renderer/components";
12
-
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- let Skia: any;
11
+ let Skia: ReturnType<typeof JsiSkApi>;
15
12
 
16
13
  export const makeOffscreenSurface = (width: number, height: number) => {
17
14
  if (!Skia) {
@@ -24,12 +21,20 @@ export const makeOffscreenSurface = (width: number, height: number) => {
24
21
  return surface;
25
22
  };
26
23
 
24
+ export const getSkiaExports = () => {
25
+ if (!Skia) {
26
+ Skia = JsiSkApi(CanvasKit);
27
+ }
28
+ return { Skia };
29
+ };
30
+
27
31
  export const drawOffscreen = (surface: SkSurface, element: ReactNode) => {
28
32
  const root = new SkiaRoot(Skia, false);
29
33
  root.render(element);
30
34
  const canvas = surface.getCanvas();
31
35
  const ctx = new JsiDrawingContext(Skia, canvas);
32
36
  root.dom.render(ctx);
37
+ root.unmount();
33
38
  surface.flush();
34
39
  return surface.makeImageSnapshot();
35
40
  };
@@ -24,6 +24,7 @@ export const drawAsPicture = (element: ReactElement) => {
24
24
  const ctx = new JsiDrawingContext(Skia, canvas);
25
25
  root.dom.render(ctx);
26
26
  const picture = recorder.finishRecordingAsPicture();
27
+ root.unmount();
27
28
  return picture;
28
29
  };
29
30
 
@@ -45,7 +45,11 @@ export const toBase64String = (bytes: Uint8Array) => {
45
45
  };
46
46
 
47
47
  export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
48
- constructor(CanvasKit: CanvasKit, ref: Image) {
48
+ constructor(
49
+ CanvasKit: CanvasKit,
50
+ ref: Image,
51
+ private releaseCtx?: () => void
52
+ ) {
49
53
  super(CanvasKit, ref, "Image");
50
54
  }
51
55
 
@@ -128,6 +132,8 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
128
132
  return toBase64String(bytes);
129
133
  }
130
134
 
135
+ // TODO: this is leaking on Web
136
+ // Add signature with allocated buffer
131
137
  readPixels(srcX?: number, srcY?: number, imageInfo?: ImageInfo) {
132
138
  const info = this.getImageInfo();
133
139
  const pxInfo: CKImageInfo = {
@@ -148,12 +154,37 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
148
154
 
149
155
  dispose = () => {
150
156
  this.ref.delete();
157
+ if (this.releaseCtx) {
158
+ this.releaseCtx();
159
+ }
151
160
  };
152
161
 
153
162
  makeNonTextureImage(): SkImage {
154
- return new JsiSkImage(
155
- this.CanvasKit,
156
- this.CanvasKit.MakeImageFromEncoded(this.encodeToBytes())!
157
- );
163
+ const partialInfo = this.ref.getImageInfo();
164
+ const colorSpace = this.ref.getColorSpace();
165
+ const info = {
166
+ ...partialInfo,
167
+ colorSpace,
168
+ };
169
+
170
+ var pixelLen = info.width * info.height * 4;
171
+ const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);
172
+ const pixels = this.ref.readPixels(
173
+ 0,
174
+ 0,
175
+ info,
176
+ pixelPtr,
177
+ info.width * 4
178
+ ) as Uint8Array | null;
179
+ if (!pixels) {
180
+ throw new Error("Could not create image from bytes");
181
+ }
182
+ const img = this.CanvasKit.MakeImage(info, pixels, info.width * 4);
183
+ if (!img) {
184
+ throw new Error("Could not create image from bytes");
185
+ }
186
+ return new JsiSkImage(this.CanvasKit, img, () => {
187
+ this.CanvasKit.Free(pixelPtr);
188
+ });
158
189
  }
159
190
  }
@@ -11,12 +11,19 @@ export class JsiSkSurface
11
11
  extends HostObject<Surface, "Surface">
12
12
  implements SkSurface
13
13
  {
14
- constructor(CanvasKit: CanvasKit, ref: Surface) {
14
+ constructor(
15
+ CanvasKit: CanvasKit,
16
+ ref: Surface,
17
+ private releaseCtx?: () => void
18
+ ) {
15
19
  super(CanvasKit, ref, "Surface");
16
20
  }
17
21
 
18
22
  dispose = () => {
19
23
  this.ref.delete();
24
+ if (this.releaseCtx) {
25
+ this.releaseCtx();
26
+ }
20
27
  };
21
28
 
22
29
  flush() {
@@ -11,11 +11,26 @@ export class JsiSkSurfaceFactory extends Host implements SurfaceFactory {
11
11
  }
12
12
 
13
13
  Make(width: number, height: number) {
14
- const surface = this.CanvasKit.MakeSurface(width, height);
14
+ var pixelLen = width * height * 4;
15
+ const pixelPtr = this.CanvasKit.Malloc(Uint8Array, pixelLen);
16
+ const surface = this.CanvasKit.MakeRasterDirectSurface(
17
+ {
18
+ width: width,
19
+ height: height,
20
+ colorType: this.CanvasKit.ColorType.RGBA_8888,
21
+ alphaType: this.CanvasKit.AlphaType.Unpremul,
22
+ colorSpace: this.CanvasKit.ColorSpace.SRGB,
23
+ },
24
+ pixelPtr,
25
+ width * 4
26
+ );
15
27
  if (!surface) {
16
28
  return null;
17
29
  }
18
- return new JsiSkSurface(this.CanvasKit, surface);
30
+ surface.getCanvas().clear(this.CanvasKit.TRANSPARENT);
31
+ return new JsiSkSurface(this.CanvasKit, surface, () => {
32
+ this.CanvasKit.Free(pixelPtr);
33
+ });
19
34
  }
20
35
 
21
36
  MakeOffscreen(width: number, height: number) {