@shopify/react-native-skia 1.0.4 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. package/android/CMakeLists.txt +4 -8
  2. package/android/build.gradle +9 -2
  3. package/android/cpp/jni/JniPlatformContext.cpp +7 -7
  4. package/android/cpp/jni/JniSkiaManager.cpp +1 -1
  5. package/android/cpp/jni/include/JniSkiaBaseView.h +2 -2
  6. package/android/cpp/jni/include/JniSkiaDomView.h +4 -4
  7. package/android/cpp/jni/include/JniSkiaManager.h +4 -4
  8. package/android/cpp/jni/include/JniSkiaPictureView.h +4 -4
  9. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.cpp +245 -0
  10. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.h +33 -0
  11. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +3 -3
  12. package/android/cpp/rnskia-android/RNSkAndroidView.h +1 -1
  13. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +2 -2
  14. package/android/cpp/rnskia-android/SkiaOpenGLHelper.h +4 -4
  15. package/android/cpp/rnskia-android/SkiaOpenGLSurfaceFactory.cpp +32 -2
  16. package/android/cpp/rnskia-android/SkiaOpenGLSurfaceFactory.h +7 -4
  17. package/android/src/main/java/com/shopify/reactnative/skia/ViewScreenshotService.java +16 -1
  18. package/cpp/api/JsiSkAnimatedImage.h +1 -1
  19. package/cpp/api/JsiSkCanvas.h +8 -8
  20. package/cpp/api/JsiSkColor.h +1 -1
  21. package/cpp/api/JsiSkColorFilter.h +1 -1
  22. package/cpp/api/JsiSkColorFilterFactory.h +2 -2
  23. package/cpp/api/JsiSkContourMeasure.h +1 -1
  24. package/cpp/api/JsiSkContourMeasureIter.h +1 -1
  25. package/cpp/api/JsiSkData.h +2 -2
  26. package/cpp/api/JsiSkFont.h +2 -2
  27. package/cpp/api/JsiSkFontMgr.h +1 -1
  28. package/cpp/api/JsiSkFontMgrFactory.h +1 -1
  29. package/cpp/api/JsiSkFontStyle.h +2 -2
  30. package/cpp/api/JsiSkImage.h +2 -2
  31. package/cpp/api/JsiSkImageFilter.h +1 -1
  32. package/cpp/api/JsiSkImageFilterFactory.h +1 -1
  33. package/cpp/api/JsiSkImageInfo.h +1 -1
  34. package/cpp/api/JsiSkMaskFilter.h +1 -1
  35. package/cpp/api/JsiSkMaskFilterFactory.h +1 -1
  36. package/cpp/api/JsiSkMatrix.h +1 -1
  37. package/cpp/api/JsiSkPaint.h +1 -1
  38. package/cpp/api/JsiSkParagraph.h +7 -7
  39. package/cpp/api/JsiSkParagraphBuilder.h +9 -9
  40. package/cpp/api/JsiSkParagraphBuilderFactory.h +4 -4
  41. package/cpp/api/JsiSkParagraphStyle.h +3 -3
  42. package/cpp/api/JsiSkPath.h +11 -11
  43. package/cpp/api/JsiSkPathEffect.h +1 -1
  44. package/cpp/api/JsiSkPathEffectFactory.h +4 -4
  45. package/cpp/api/JsiSkPathFactory.h +2 -2
  46. package/cpp/api/JsiSkPicture.h +1 -1
  47. package/cpp/api/JsiSkPictureFactory.h +2 -2
  48. package/cpp/api/JsiSkPictureRecorder.h +2 -2
  49. package/cpp/api/JsiSkPoint.h +1 -1
  50. package/cpp/api/JsiSkRRect.h +1 -1
  51. package/cpp/api/JsiSkRSXform.h +1 -1
  52. package/cpp/api/JsiSkRect.h +1 -1
  53. package/cpp/api/JsiSkRuntimeEffect.h +1 -1
  54. package/cpp/api/JsiSkRuntimeShaderBuilder.h +1 -1
  55. package/cpp/api/JsiSkSVGFactory.h +1 -1
  56. package/cpp/api/JsiSkShader.h +2 -2
  57. package/cpp/api/JsiSkShaderFactory.h +3 -3
  58. package/cpp/api/JsiSkStrutStyle.h +1 -1
  59. package/cpp/api/JsiSkSurface.h +1 -1
  60. package/cpp/api/JsiSkSurfaceFactory.h +1 -1
  61. package/cpp/api/JsiSkTextBlob.h +1 -1
  62. package/cpp/api/JsiSkTextBlobFactory.h +1 -1
  63. package/cpp/api/JsiSkTextStyle.h +2 -2
  64. package/cpp/api/JsiSkTypeface.h +3 -3
  65. package/cpp/api/JsiSkTypefaceFontProvider.h +2 -2
  66. package/cpp/api/JsiSkVertices.h +1 -1
  67. package/cpp/jsi/JsiHostObject.cpp +1 -1
  68. package/cpp/rnskia/RNSkDomView.cpp +1 -1
  69. package/cpp/rnskia/RNSkDomView.h +3 -3
  70. package/cpp/rnskia/RNSkManager.cpp +5 -5
  71. package/cpp/rnskia/RNSkPictureView.h +3 -3
  72. package/cpp/rnskia/RNSkPlatformContext.h +5 -5
  73. package/cpp/rnskia/RNSkView.h +2 -2
  74. package/cpp/rnskia/dom/base/ConcatablePaint.h +6 -6
  75. package/cpp/rnskia/dom/base/Declaration.h +1 -1
  76. package/cpp/rnskia/dom/base/DeclarationContext.h +7 -7
  77. package/cpp/rnskia/dom/base/DrawingContext.h +3 -3
  78. package/cpp/rnskia/dom/nodes/JsiBlurMaskNode.h +2 -2
  79. package/cpp/rnskia/dom/nodes/JsiColorFilterNodes.h +1 -1
  80. package/cpp/rnskia/dom/nodes/JsiImageFilterNodes.h +1 -1
  81. package/cpp/rnskia/dom/nodes/JsiPathEffectNodes.h +1 -1
  82. package/cpp/rnskia/dom/nodes/JsiPathNode.h +1 -1
  83. package/cpp/rnskia/dom/nodes/JsiPointsNode.h +1 -1
  84. package/cpp/rnskia/dom/nodes/JsiShaderNodes.h +1 -1
  85. package/cpp/rnskia/dom/props/BlendModeProp.h +1 -1
  86. package/cpp/rnskia/dom/props/BoxShadowProps.h +1 -1
  87. package/cpp/rnskia/dom/props/CircleProp.h +1 -1
  88. package/cpp/rnskia/dom/props/ClipProp.h +2 -2
  89. package/cpp/rnskia/dom/props/PaintProps.h +1 -1
  90. package/cpp/rnskia/dom/props/PathProp.h +1 -1
  91. package/cpp/rnskia/dom/props/PointProp.h +1 -1
  92. package/cpp/rnskia/dom/props/PointsProp.h +1 -1
  93. package/cpp/rnskia/dom/props/RRectProp.h +2 -2
  94. package/cpp/rnskia/dom/props/RSXformProp.h +1 -1
  95. package/cpp/rnskia/dom/props/RadiusProp.h +1 -1
  96. package/cpp/rnskia/dom/props/RectProp.h +1 -1
  97. package/cpp/rnskia/dom/props/StrokeProps.h +1 -1
  98. package/cpp/rnskia/dom/props/TileModeProp.h +1 -1
  99. package/cpp/rnskia/dom/props/UniformsProp.h +1 -1
  100. package/cpp/rnskia/dom/props/VertexModeProp.h +1 -1
  101. package/cpp/rnskia/dom/props/VerticesProps.h +1 -1
  102. package/cpp/skia/src/gpu/ganesh/gl/GrGLDefines.h +1158 -0
  103. package/cpp/utils/RNSkTypedArray.h +1 -1
  104. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.mm +6 -6
  105. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +3 -3
  106. package/ios/RNSkia-iOS/SkiaDomView.mm +7 -7
  107. package/ios/RNSkia-iOS/SkiaDomViewManager.mm +7 -7
  108. package/ios/RNSkia-iOS/SkiaManager.h +1 -1
  109. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +1 -1
  110. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +5 -5
  111. package/ios/RNSkia-iOS/SkiaPictureView.mm +7 -7
  112. package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +4 -4
  113. package/ios/RNSkia-iOS/SkiaUIView.h +3 -3
  114. package/ios/RNSkia-iOS/SkiaUIView.mm +2 -2
  115. package/ios/RNSkia-iOS/ViewScreenshotService.h +1 -1
  116. package/ios/RNSkia-iOS/ViewScreenshotService.mm +1 -1
  117. package/lib/commonjs/headless/index.d.ts +4 -1
  118. package/lib/commonjs/headless/index.js +12 -2
  119. package/lib/commonjs/headless/index.js.map +1 -1
  120. package/lib/commonjs/renderer/Offscreen.js +1 -0
  121. package/lib/commonjs/renderer/Offscreen.js.map +1 -1
  122. package/lib/commonjs/skia/web/JsiSkImage.d.ts +2 -1
  123. package/lib/commonjs/skia/web/JsiSkImage.js +27 -2
  124. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  125. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +2 -1
  126. package/lib/commonjs/skia/web/JsiSkSurface.js +5 -1
  127. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  128. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.js +13 -2
  129. package/lib/commonjs/skia/web/JsiSkSurfaceFactory.js.map +1 -1
  130. package/lib/module/headless/index.d.ts +4 -1
  131. package/lib/module/headless/index.js +9 -2
  132. package/lib/module/headless/index.js.map +1 -1
  133. package/lib/module/renderer/Offscreen.js +1 -0
  134. package/lib/module/renderer/Offscreen.js.map +1 -1
  135. package/lib/module/skia/web/JsiSkImage.d.ts +2 -1
  136. package/lib/module/skia/web/JsiSkImage.js +27 -2
  137. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  138. package/lib/module/skia/web/JsiSkSurface.d.ts +2 -1
  139. package/lib/module/skia/web/JsiSkSurface.js +5 -1
  140. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  141. package/lib/module/skia/web/JsiSkSurfaceFactory.js +13 -2
  142. package/lib/module/skia/web/JsiSkSurfaceFactory.js.map +1 -1
  143. package/lib/typescript/src/headless/index.d.ts +4 -1
  144. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +2 -1
  145. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +2 -1
  146. package/package.json +1 -1
  147. package/src/headless/index.ts +9 -2
  148. package/src/renderer/Offscreen.tsx +1 -0
  149. package/src/skia/web/JsiSkImage.ts +36 -5
  150. package/src/skia/web/JsiSkSurface.ts +8 -1
  151. package/src/skia/web/JsiSkSurfaceFactory.ts +17 -2
@@ -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,KA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I,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,MAAM,CAACC,YAAY,CAACC,KAAK,CAAC,IAAI,EAAEL,KAAY,CAAC;MACvDH,KAAK,IAAID,UAAU;IACrB;IACA,OAAOU,IAAI,CAACP,MAAM,CAAC;EACrB;AACF,CAAC;AAED,OAAO,MAAMQ,UAAU,SAASnB,UAAU,CAAoC;EAC5EoB,WAAWA,CAACC,SAAoB,EAAEC,GAAU,EAAE;IAC5C,KAAK,CAACD,SAAS,EAAEC,GAAG,EAAE,OAAO,CAAC;IAACC,eAAA,kBAoGvB,MAAM;MACd,IAAI,CAACD,GAAG,CAACE,MAAM,CAAC,CAAC;IACnB,CAAC;EArGD;EAEAC,MAAMA,CAAA,EAAG;IACP,OAAO,IAAI,CAACH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AA,GAAED,SAAS,aAATA,SAAS,uBAATA,SAAS,CAAE7B,KAAK,cAAA8B,gBAAA,cAAAA,gBAAA,GAAI5B,IAAI,CAACF,KAAK;MACrCD,MAAM,GAAAgC,iBAAA,GAAEF,SAAS,aAATA,SAAS,uBAATA,SAAS,CAAE9B,MAAM,cAAAgC,iBAAA,cAAAA,iBAAA,GAAI7B,IAAI,CAACH,MAAM;MACxCM,SAAS,EAAEhC,OAAO,CAChB,IAAI,CAACsB,SAAS,CAACyC,SAAS,EACxB,CAACP,SAAS,aAATA,SAAS,cAATA,SAAS,GAAI3B,IAAI,EAAEG,SACtB,CAAC;MACDF,SAAS,EAAE9B,OAAO,CAChB,IAAI,CAACsB,SAAS,CAAC0C,SAAS,EACxB,CAACR,SAAS,aAATA,SAAS,cAATA,SAAS,GAAI3B,IAAI,EAAEC,SACtB;IACF,CAAC;IACD,OAAO,IAAI,CAACP,GAAG,CAAC8B,UAAU,CAACC,IAAI,aAAJA,IAAI,cAAJA,IAAI,GAAI,CAAC,EAAEC,IAAI,aAAJA,IAAI,cAAJA,IAAI,GAAI,CAAC,EAAEI,MAAM,CAAC;EAC1D;EAMAM,mBAAmBA,CAAA,EAAY;IAC7B,OAAO,IAAI7C,UAAU,CACnB,IAAI,CAACE,SAAS,EACd,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,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,C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AAI,CAAC,EAAEC,IAAI,aAAJA,IAAI,cAAJA,IAAI,GAAI,CAAC,EAAEI,MAAM,CAAC;EAC1D;EASAM,mBAAmBA,CAAA,EAAY;IAC7B,MAAMC,WAAW,GAAG,IAAI,CAAC5C,GAAG,CAACM,YAAY,CAAC,CAAC;IAC3C,MAAMgC,UAAU,GAAG,IAAI,CAACtC,GAAG,CAAC6C,aAAa,CAAC,CAAC;IAC3C,MAAMtC,IAAI,GAAG;MACX,GAAGqC,WAAW;MACdN;IACF,CAAC;IAED,IAAIQ,QAAQ,GAAGvC,IAAI,CAACF,KAAK,GAAGE,IAAI,CAACH,MAAM,GAAG,CAAC;IAC3C,MAAM2C,QAAQ,GAAG,IAAI,CAAChD,SAAS,CAACiD,MAAM,CAACC,UAAU,EAAEH,QAAQ,CAAC;IAC5D,MAAMI,MAAM,GAAG,IAAI,CAAClD,GAAG,CAAC+B,UAAU,CAChC,CAAC,EACD,CAAC,EACDxB,IAAI,EACJwC,QAAQ,EACRxC,IAAI,CAACF,KAAK,GAAG,CACf,CAAsB;IACtB,IAAI,CAAC6C,MAAM,EAAE;MACX,MAAM,IAAIrB,KAAK,CAAC,mCAAmC,CAAC;IACtD;IACA,MAAMsB,GAAG,GAAG,IAAI,CAACpD,SAAS,CAACqD,SAAS,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.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,MAAMC,OAAO,GAAG,IAAI,CAACJ,SAAS,CAACK,WAAW,CAACH,KAAK,EAAEC,MAAM,CAAC;IACzD,IAAI,CAACC,OAAO,EAAE;MACZ,OAAO,IAAI;IACb;IACA,OAAO,IAAIP,YAAY,CAAC,IAAI,CAACG,SAAS,EAAEI,OAAO,CAAC;EAClD;EAEAE,aAAaA,CAACJ,KAAa,EAAEC,MAAc,EAAE;IAC3C;IACA;IACA,MAAMI,EAAE,GAAIC,UAAU,CAASC,eAAe;IAC9C,IAAIL,OAAuB;IAC3B,IAAIG,EAAE,KAAKG,SAAS,EAAE;MACpBN,OAAO,GAAG,IAAI,CAACJ,SAAS,CAACK,WAAW,CAACH,KAAK,EAAEC,MAAM,CAAC;IACrD,CAAC,MAAM;MACL,MAAMQ,SAAS,GAAG,IAAIJ,EAAE,CAACL,KAAK,EAAEC,MAAM,CAAC;MACvC,MAAMS,YAAY,GAAG,IAAI,CAACZ,SAAS,CAACa,eAAe,CAACF,SAAS,CAAC;MAC9D,MAAMG,SAAS,GAAG,IAAI,CAACd,SAAS,CAACe,gBAAgB,CAACH,YAAY,CAAC;MAC/D,IAAI,CAACE,SAAS,EAAE;QACd,MAAM,IAAIE,KAAK,CAAC,mCAAmC,CAAC;MACtD;MACAZ,OAAO,GAAG,IAAI,CAACJ,SAAS,CAACiB,gBAAgB,CAACH,SAAS,EAAEZ,KAAK,EAAEC,MAAM,CAAC;IACrE;IACA,IAAI,CAACC,OAAO,EAAE;MACZ,OAAO,IAAI;IACb;IACA,OAAO,IAAIP,YAAY,CAAC,IAAI,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,GAAG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I,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,8 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { SkSurface } from "../skia";
3
3
  export * from "../renderer/components";
4
- export declare const makeOffscreenSurface: (width: number, height: number) => any;
4
+ export declare const makeOffscreenSurface: (width: number, height: number) => SkSurface;
5
+ export declare const getSkiaExports: () => {
6
+ Skia: import("../skia/types").Skia;
7
+ };
5
8
  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.6",
11
11
  "description": "High-performance React Native Graphics using Skia",
12
12
  "main": "lib/module/index.js",
13
13
  "react-native": "src/index.ts",
@@ -10,8 +10,7 @@ import type { SkSurface } from "../skia";
10
10
 
11
11
  export * from "../renderer/components";
12
12
 
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- let Skia: any;
13
+ let Skia: ReturnType<typeof JsiSkApi>;
15
14
 
16
15
  export const makeOffscreenSurface = (width: number, height: number) => {
17
16
  if (!Skia) {
@@ -24,12 +23,20 @@ export const makeOffscreenSurface = (width: number, height: number) => {
24
23
  return surface;
25
24
  };
26
25
 
26
+ export const getSkiaExports = () => {
27
+ if (!Skia) {
28
+ Skia = JsiSkApi(CanvasKit);
29
+ }
30
+ return { Skia };
31
+ };
32
+
27
33
  export const drawOffscreen = (surface: SkSurface, element: ReactNode) => {
28
34
  const root = new SkiaRoot(Skia, false);
29
35
  root.render(element);
30
36
  const canvas = surface.getCanvas();
31
37
  const ctx = new JsiDrawingContext(Skia, canvas);
32
38
  root.dom.render(ctx);
39
+ root.unmount();
33
40
  surface.flush();
34
41
  return surface.makeImageSnapshot();
35
42
  };
@@ -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) {