@shopify/react-native-skia 1.5.10 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/android/cpp/jni/include/JniSkiaBaseView.h +6 -5
  2. package/android/cpp/jni/include/JniSkiaDomView.h +6 -4
  3. package/android/cpp/jni/include/JniSkiaManager.h +2 -2
  4. package/android/cpp/jni/include/JniSkiaPictureView.h +6 -4
  5. package/android/cpp/rnskia-android/MainThreadDispatcher.h +3 -0
  6. package/android/cpp/rnskia-android/OpenGLContext.h +44 -9
  7. package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +0 -1
  8. package/android/cpp/rnskia-android/OpenGLWindowContext.h +2 -2
  9. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +61 -3
  10. package/android/cpp/rnskia-android/RNSkAndroidView.h +10 -13
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +42 -36
  12. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +2 -2
  13. package/android/cpp/rnskia-android/gl/Display.h +2 -0
  14. package/android/src/main/java/com/shopify/reactnative/skia/SkiaAHBView.java +113 -0
  15. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +45 -54
  16. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseViewManager.java +5 -0
  17. package/android/src/main/java/com/shopify/reactnative/skia/SkiaDomView.java +2 -2
  18. package/android/src/main/java/com/shopify/reactnative/skia/SkiaPictureView.java +2 -2
  19. package/android/src/main/java/com/shopify/reactnative/skia/SkiaSurfaceView.java +42 -0
  20. package/android/src/main/java/com/shopify/reactnative/skia/SkiaTextureView.java +90 -0
  21. package/android/src/main/java/com/shopify/reactnative/skia/SkiaViewAPI.java +16 -0
  22. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDomViewManagerDelegate.java +3 -0
  23. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDomViewManagerInterface.java +1 -0
  24. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaPictureViewManagerDelegate.java +3 -1
  25. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaPictureViewManagerInterface.java +1 -0
  26. package/cpp/api/JsiSkContourMeasure.h +1 -5
  27. package/cpp/api/JsiSkImage.h +11 -0
  28. package/cpp/api/JsiSkImageFactory.h +14 -0
  29. package/cpp/api/JsiSkPicture.h +2 -0
  30. package/cpp/api/JsiSkSurface.h +7 -0
  31. package/cpp/api/JsiTextureInfo.h +53 -0
  32. package/cpp/jsi/ViewProperty.h +48 -0
  33. package/cpp/rnskia/RNSkDomView.cpp +0 -20
  34. package/cpp/rnskia/RNSkDomView.h +4 -9
  35. package/cpp/rnskia/RNSkJsiViewApi.h +3 -3
  36. package/cpp/rnskia/RNSkPictureView.h +11 -28
  37. package/cpp/rnskia/RNSkPlatformContext.h +18 -12
  38. package/cpp/rnskia/RNSkView.h +5 -29
  39. package/ios/RNSkia-iOS/MetalContext.h +101 -15
  40. package/ios/RNSkia-iOS/MetalContext.mm +9 -8
  41. package/ios/RNSkia-iOS/MetalWindowContext.h +39 -0
  42. package/ios/RNSkia-iOS/MetalWindowContext.mm +60 -0
  43. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +13 -25
  44. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +88 -2
  45. package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +9 -2
  46. package/ios/RNSkia-iOS/SkiaDomViewManager.mm +5 -0
  47. package/ios/RNSkia-iOS/SkiaManager.mm +1 -2
  48. package/ios/RNSkia-iOS/SkiaPictureView.mm +1 -0
  49. package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +5 -0
  50. package/ios/RNSkia-iOS/SkiaUIView.h +1 -0
  51. package/ios/RNSkia-iOS/SkiaUIView.mm +21 -14
  52. package/lib/commonjs/skia/types/Image/ColorType.d.ts +21 -0
  53. package/lib/commonjs/skia/types/Image/ColorType.js +29 -0
  54. package/lib/commonjs/skia/types/Image/ColorType.js.map +1 -0
  55. package/lib/commonjs/skia/types/Image/ColorType.web.d.ts +19 -0
  56. package/lib/commonjs/skia/types/Image/ColorType.web.js +27 -0
  57. package/lib/commonjs/skia/types/Image/ColorType.web.js.map +1 -0
  58. package/lib/commonjs/skia/types/Image/Image.d.ts +10 -0
  59. package/lib/commonjs/skia/types/Image/Image.js.map +1 -1
  60. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +19 -19
  61. package/lib/commonjs/skia/types/Image/ImageFactory.js +1 -21
  62. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  63. package/lib/commonjs/skia/types/Image/index.d.ts +1 -0
  64. package/lib/commonjs/skia/types/Image/index.js +11 -0
  65. package/lib/commonjs/skia/types/Image/index.js.map +1 -1
  66. package/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  67. package/lib/commonjs/skia/types/Matrix4.js +69 -1
  68. package/lib/commonjs/skia/types/Matrix4.js.map +1 -1
  69. package/lib/commonjs/skia/types/Surface/Surface.d.ts +11 -0
  70. package/lib/commonjs/skia/types/Surface/Surface.js.map +1 -1
  71. package/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  72. package/lib/commonjs/skia/web/JsiSkImage.js +4 -0
  73. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  74. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  75. package/lib/commonjs/skia/web/JsiSkImageFactory.js +3 -0
  76. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  77. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  78. package/lib/commonjs/skia/web/JsiSkSurface.js +4 -0
  79. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  80. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.d.ts +1 -0
  81. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  82. package/lib/commonjs/views/SkiaDomView.js +3 -1
  83. package/lib/commonjs/views/SkiaDomView.js.map +1 -1
  84. package/lib/commonjs/views/SkiaPictureView.js +5 -1
  85. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  86. package/lib/commonjs/views/types.d.ts +2 -0
  87. package/lib/commonjs/views/types.js.map +1 -1
  88. package/lib/module/skia/types/Image/ColorType.d.ts +21 -0
  89. package/lib/module/skia/types/Image/ColorType.js +23 -0
  90. package/lib/module/skia/types/Image/ColorType.js.map +1 -0
  91. package/lib/module/skia/types/Image/ColorType.web.d.ts +19 -0
  92. package/lib/module/skia/types/Image/ColorType.web.js +21 -0
  93. package/lib/module/skia/types/Image/ColorType.web.js.map +1 -0
  94. package/lib/module/skia/types/Image/Image.d.ts +10 -0
  95. package/lib/module/skia/types/Image/Image.js.map +1 -1
  96. package/lib/module/skia/types/Image/ImageFactory.d.ts +19 -19
  97. package/lib/module/skia/types/Image/ImageFactory.js +0 -20
  98. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  99. package/lib/module/skia/types/Image/index.d.ts +1 -0
  100. package/lib/module/skia/types/Image/index.js +1 -0
  101. package/lib/module/skia/types/Image/index.js.map +1 -1
  102. package/lib/module/skia/types/Matrix4.d.ts +6 -0
  103. package/lib/module/skia/types/Matrix4.js +67 -0
  104. package/lib/module/skia/types/Matrix4.js.map +1 -1
  105. package/lib/module/skia/types/Surface/Surface.d.ts +11 -0
  106. package/lib/module/skia/types/Surface/Surface.js.map +1 -1
  107. package/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  108. package/lib/module/skia/web/JsiSkImage.js +4 -0
  109. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  110. package/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  111. package/lib/module/skia/web/JsiSkImageFactory.js +3 -0
  112. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  113. package/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  114. package/lib/module/skia/web/JsiSkSurface.js +4 -0
  115. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  116. package/lib/module/specs/SkiaPictureViewNativeComponent.d.ts +1 -0
  117. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  118. package/lib/module/views/SkiaDomView.js +3 -1
  119. package/lib/module/views/SkiaDomView.js.map +1 -1
  120. package/lib/module/views/SkiaPictureView.js +5 -1
  121. package/lib/module/views/SkiaPictureView.js.map +1 -1
  122. package/lib/module/views/types.d.ts +2 -0
  123. package/lib/module/views/types.js.map +1 -1
  124. package/lib/typescript/lib/commonjs/skia/types/Image/ColorType.d.ts +2 -0
  125. package/lib/typescript/lib/commonjs/skia/types/Image/ColorType.web.d.ts +2 -0
  126. package/lib/typescript/lib/commonjs/skia/types/Image/ImageFactory.d.ts +0 -1
  127. package/lib/typescript/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  128. package/lib/typescript/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  129. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  130. package/lib/typescript/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  131. package/lib/typescript/lib/module/mock/index.d.ts +1 -0
  132. package/lib/typescript/lib/module/skia/types/Image/ColorType.d.ts +1 -0
  133. package/lib/typescript/lib/module/skia/types/Image/ColorType.web.d.ts +1 -0
  134. package/lib/typescript/lib/module/skia/types/Image/ImageFactory.d.ts +0 -1
  135. package/lib/typescript/lib/module/skia/types/Image/index.d.ts +1 -0
  136. package/lib/typescript/lib/module/skia/types/Matrix4.d.ts +1 -0
  137. package/lib/typescript/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  138. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  139. package/lib/typescript/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  140. package/lib/typescript/src/skia/types/Image/ColorType.d.ts +21 -0
  141. package/lib/typescript/src/skia/types/Image/ColorType.web.d.ts +19 -0
  142. package/lib/typescript/src/skia/types/Image/Image.d.ts +10 -0
  143. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +19 -19
  144. package/lib/typescript/src/skia/types/Image/index.d.ts +1 -0
  145. package/lib/typescript/src/skia/types/Matrix4.d.ts +6 -0
  146. package/lib/typescript/src/skia/types/Surface/Surface.d.ts +11 -0
  147. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +1 -0
  148. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +1 -0
  149. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +1 -0
  150. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.d.ts +1 -0
  151. package/lib/typescript/src/views/types.d.ts +2 -0
  152. package/package.json +1 -1
  153. package/src/renderer/__tests__/e2e/Matrix4.spec.tsx +93 -0
  154. package/src/skia/__tests__/Enums.spec.ts +2 -2
  155. package/src/skia/types/Image/ColorType.ts +21 -0
  156. package/src/skia/types/Image/ColorType.web.ts +19 -0
  157. package/src/skia/types/Image/Image.ts +11 -0
  158. package/src/skia/types/Image/ImageFactory.ts +25 -20
  159. package/src/skia/types/Image/index.ts +1 -0
  160. package/src/skia/types/Matrix4.ts +101 -0
  161. package/src/skia/types/Surface/Surface.ts +12 -0
  162. package/src/skia/web/JsiSkImage.ts +5 -0
  163. package/src/skia/web/JsiSkImageFactory.ts +4 -0
  164. package/src/skia/web/JsiSkSurface.ts +5 -0
  165. package/src/specs/SkiaPictureViewNativeComponent.ts +1 -0
  166. package/src/views/SkiaDomView.tsx +2 -1
  167. package/src/views/SkiaPictureView.tsx +4 -1
  168. package/src/views/types.ts +3 -0
  169. package/cpp/jsi/JsiValueWrapper.h +0 -164
  170. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +0 -128
  171. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +0 -92
@@ -12,6 +12,9 @@ import {
12
12
  toMatrix3,
13
13
  Matrix4,
14
14
  mapPoint3d,
15
+ invert4,
16
+ scale,
17
+ rotateZ,
15
18
  } from "../../../skia/types";
16
19
 
17
20
  const ckPerspective = (d: number) => [
@@ -272,4 +275,94 @@ describe("Matrix4", () => {
272
275
  const result = mapPoint3d(translationMatrix, point);
273
276
  expect(result).toEqual(expectedResult);
274
277
  });
278
+
279
+ const almostEqual = (
280
+ a: number[] | Matrix4 | readonly [number, number, number],
281
+ b: number[] | Matrix4 | readonly [number, number, number],
282
+ epsilon = 1e-10
283
+ ) => {
284
+ expect(a.length).toBe(b.length);
285
+ a.forEach((val, idx) => {
286
+ expect(Math.abs(val - b[idx])).toBeLessThan(epsilon);
287
+ });
288
+ };
289
+
290
+ const matrixEqual = (a: number[] | Matrix4, b: number[] | Matrix4) => {
291
+ expect(a.length).toBe(b.length);
292
+ a.forEach((val, idx) => {
293
+ // Object.is will distinguish -0 from 0, so we use === instead
294
+ const equal = val === b[idx] || (val === 0 && b[idx] === 0);
295
+ expect(equal).toBe(true);
296
+ });
297
+ };
298
+
299
+ it("should return the identity matrix when inverting the identity matrix", () => {
300
+ const identityMatrix = Matrix4();
301
+ const result = invert4(identityMatrix);
302
+ matrixEqual(result, identityMatrix);
303
+ });
304
+
305
+ it("should correctly invert a translation matrix", () => {
306
+ const translationMatrix = translate(100, -50, 25);
307
+ const inverse = invert4(translationMatrix);
308
+ // Inverse of translation(x,y,z) should be translation(-x,-y,-z)
309
+ const expectedInverse = translate(-100, 50, -25);
310
+ matrixEqual(inverse, expectedInverse);
311
+ });
312
+
313
+ it("should correctly invert a scale matrix", () => {
314
+ const scaleMatrix = scale(2, 4, 8);
315
+ const inverse = invert4(scaleMatrix);
316
+ // Inverse of scale(x,y,z) should be scale(1/x,1/y,1/z)
317
+ const expectedInverse = scale(1 / 2, 1 / 4, 1 / 8);
318
+ matrixEqual(inverse, expectedInverse);
319
+ });
320
+
321
+ it("should return identity matrix for non-invertible matrix", () => {
322
+ // A matrix of all zeros is not invertible
323
+ const nonInvertibleMatrix = [
324
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
325
+ ] as Matrix4;
326
+ const result = invert4(nonInvertibleMatrix);
327
+ matrixEqual(result, Matrix4());
328
+ });
329
+
330
+ it("multiplying a matrix by its inverse should give the identity matrix", () => {
331
+ // Create a complex transformation matrix
332
+ const complexMatrix = processTransform3d([
333
+ { scale: 2 },
334
+ { rotateZ: Math.PI / 4 },
335
+ { translate: [100, 100, 50] as const },
336
+ ]);
337
+
338
+ const inverse = invert4(complexMatrix);
339
+ const result = multiply4(complexMatrix, inverse);
340
+
341
+ // Due to floating point arithmetic, we use almostEqual instead of exact equality
342
+ almostEqual(result, Matrix4());
343
+ });
344
+
345
+ it("should correctly transform points when using inverse matrix", () => {
346
+ const transformMatrix = translate(100, 100, 100);
347
+ const inverse = invert4(transformMatrix);
348
+
349
+ const point = [200, 0, 300] as const;
350
+ const transformedPoint = mapPoint3d(inverse, point);
351
+ const expectedPoint = [100, -100, 200] as const;
352
+
353
+ // Using almostEqual for floating point comparison
354
+ almostEqual(transformedPoint, expectedPoint);
355
+ });
356
+
357
+ it("should maintain inverse relationship for rotations", () => {
358
+ const rotationMatrix = rotateZ(Math.PI / 3); // 60 degrees rotation
359
+ const inverse = invert4(rotationMatrix);
360
+ const point = [100, 100, 0] as const;
361
+
362
+ // Transform point forward then backward should give original point
363
+ const transformed = mapPoint3d(rotationMatrix, point);
364
+ const backTransformed = mapPoint3d(inverse, transformed);
365
+
366
+ almostEqual(backTransformed, point);
367
+ });
275
368
  });
@@ -4,7 +4,6 @@ import {
4
4
  AlphaType,
5
5
  BlurStyle,
6
6
  ClipOp,
7
- ColorType,
8
7
  FillType,
9
8
  FilterMode,
10
9
  FontEdging,
@@ -27,6 +26,7 @@ import {
27
26
  import { Path1DEffectStyle } from "../types/PathEffect";
28
27
  import { BlendMode } from "../types/Paint/BlendMode";
29
28
  import { mapKeys } from "../../renderer/typeddash";
29
+ import { ColorTypeCanvasKit } from "../types/Image/ColorType.web";
30
30
 
31
31
  import { setupSkia } from "./setup";
32
32
 
@@ -71,7 +71,7 @@ describe("Enums", () => {
71
71
  });
72
72
  it("Should match Image enums values with CanvasKit", () => {
73
73
  const { CanvasKit } = setupSkia();
74
- checkEnum(ColorType, CanvasKit.ColorType);
74
+ checkEnum(ColorTypeCanvasKit, CanvasKit.ColorType);
75
75
  checkEnum(AlphaType, CanvasKit.AlphaType);
76
76
  checkEnum(ImageFormat, CanvasKit.ImageFormat);
77
77
  checkEnum(MipmapMode, CanvasKit.MipmapMode);
@@ -0,0 +1,21 @@
1
+ export enum ColorType {
2
+ Unknown, // uninitialized
3
+ Alpha_8, // pixel with alpha in 8-bit byte
4
+ RGB_565, // pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
5
+ ARGB_4444, // pixel with 4 bits for alpha, red, green, blue; in 16-bit word
6
+ RGBA_8888, // pixel with 8 bits for red, green, blue, alpha; in 32-bit word
7
+ RGB_888x, // pixel with 8 bits each for red, green, blue; in 32-bit word
8
+ BGRA_8888, // pixel with 8 bits for blue, green, red, alpha; in 32-bit word
9
+ RGBA_1010102, // 10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
10
+ BGRA_1010102, // 10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
11
+ RGB_101010x, // pixel with 10 bits each for red, green, blue; in 32-bit word
12
+ BGR_101010x, // pixel with 10 bits each for blue, green, red; in 32-bit word
13
+ BGR_101010x_XR, // pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
14
+ BGRA_10101010_XR, // pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
15
+ RGBA_10x6, // pixel with 10 used bits (most significant) followed by 6 unused
16
+ Gray_8, // pixel with grayscale level in 8-bit byte
17
+ RGBA_F16Norm, // pixel with half floats in [0,1] for red, green, blue, alpha; in 64-bit word
18
+ RGBA_F16, // pixel with half floats for red, green, blue, alpha; in 64-bit word
19
+ RGB_F16F16F16x, // pixel with half floats for red, green, blue; in 64-bit word
20
+ RGBA_F32, // pixel using C float for red, green, blue, alpha; in 128-bit word
21
+ }
@@ -0,0 +1,19 @@
1
+ export enum ColorTypeCanvasKit {
2
+ Unknown, // uninitialized
3
+ Alpha_8, // pixel with alpha in 8-bit byte
4
+ RGB_565, // pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
5
+ ARGB_4444, // pixel with 4 bits for alpha, red, green, blue; in 16-bit word
6
+ RGBA_8888, // pixel with 8 bits for red, green, blue, alpha; in 32-bit word
7
+ RGB_888x, // pixel with 8 bits each for red, green, blue; in 32-bit word
8
+ BGRA_8888, // pixel with 8 bits for blue, green, red, alpha; in 32-bit word
9
+ RGBA_1010102, // 10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
10
+ BGRA_1010102, // 10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
11
+ RGB_101010x, // pixel with 10 bits each for red, green, blue; in 32-bit word
12
+ BGR_101010x, // pixel with 10 bits each for blue, green, red; in 32-bit word
13
+ BGR_101010x_XR, // pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
14
+ RGBA_10x6, // pixel with 10 used bits (most significant) followed by 6 unused
15
+ Gray_8, // pixel with grayscale level in 8-bit byte
16
+ RGBA_F16Norm, // pixel with half floats in [0,1] for red, green, blue, alpha; in 64-bit word
17
+ RGBA_F16, // pixel with half floats for red, green, blue, alpha; in 64-bit word
18
+ RGBA_F32, // pixel using C float for red, green, blue, alpha; in 128-bit word
19
+ }
@@ -48,6 +48,17 @@ export interface SkImage extends SkJSIInstance<"Image"> {
48
48
  */
49
49
  getImageInfo(): ImageInfo;
50
50
 
51
+ /**
52
+ * Returns the backend texture of the image.
53
+ * The returned object can be used to create a Skia Image object.
54
+ * The returned object is backend specific and should be used with caution.
55
+ * It is the caller's responsibility to ensure that the texture is not used after the image is deleted.
56
+ * The returned object may be null if the image does not have a backend texture.
57
+ *
58
+ * @return backend texture of the image or null
59
+ */
60
+ getNativeTextureUnstable(): unknown;
61
+
51
62
  /**
52
63
  * Returns this image as a shader with the specified tiling. It will use cubic sampling.
53
64
  * @param tx - tile mode in the x direction.
@@ -1,6 +1,7 @@
1
1
  import type { SkData } from "../Data";
2
2
  import type { NativeBuffer } from "../NativeBuffer";
3
3
 
4
+ import type { ColorType } from "./ColorType";
4
5
  import type { SkImage } from "./Image";
5
6
 
6
7
  export enum AlphaType {
@@ -10,26 +11,6 @@ export enum AlphaType {
10
11
  Unpremul,
11
12
  }
12
13
 
13
- export enum ColorType {
14
- Unknown, // uninitialized
15
- Alpha_8, // pixel with alpha in 8-bit byte
16
- RGB_565, // pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
17
- ARGB_4444, // pixel with 4 bits for alpha, red, green, blue; in 16-bit word
18
- RGBA_8888, // pixel with 8 bits for red, green, blue, alpha; in 32-bit word
19
- RGB_888x, // pixel with 8 bits each for red, green, blue; in 32-bit word
20
- BGRA_8888, // pixel with 8 bits for blue, green, red, alpha; in 32-bit word
21
- RGBA_1010102, // 10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
22
- BGRA_1010102, // 10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
23
- RGB_101010x, // pixel with 10 bits each for red, green, blue; in 32-bit word
24
- BGR_101010x, // pixel with 10 bits each for blue, green, red; in 32-bit word
25
- BGR_101010x_XR, // pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
26
- RGBA_10x6, // pixel with 10 used bits (most significant) followed by 6 unused
27
- Gray_8, // pixel with grayscale level in 8-bit byte
28
- RGBA_F16Norm, // pixel with half floats in [0,1] for red, green, blue, alpha; in 64-bit word
29
- RGBA_F16, // pixel with half floats for red, green, blue, alpha; in 64-bit word
30
- RGBA_F32, // pixel using C float for red, green, blue, alpha; in 128-bit word
31
- }
32
-
33
14
  export interface ImageInfo {
34
15
  alphaType: AlphaType;
35
16
  // TODO: add support for color space
@@ -68,6 +49,30 @@ export interface ImageFactory {
68
49
  */
69
50
  MakeImageFromNativeBuffer: (nativeBuffer: NativeBuffer) => SkImage;
70
51
 
52
+ /**
53
+ *
54
+ * Return an Image backed by a given native texture.
55
+ *
56
+ * The native texture must be a valid owning reference.
57
+ *
58
+ * This API might be used to integrate with other libraries using gpu textures,
59
+ * or to transfer images between different threads.
60
+ *
61
+ * @param texture A native texture handle
62
+ * @param width The width of the texture
63
+ * @param height The height of the texture
64
+ * @param mipmapped Whether the texture is mipmapped
65
+ * @throws Throws an error if the Image could not be created, for example when the given native texture is invalid.
66
+ *
67
+ * @returns Returns a valid SkImage, if the texture is invalid, an error is thrown.
68
+ */
69
+ MakeImageFromNativeTextureUnstable: (
70
+ texture: unknown,
71
+ width: number,
72
+ height: number,
73
+ mipmapped?: boolean
74
+ ) => SkImage;
75
+
71
76
  /**
72
77
  * Returns an image that will be a screenshot of the view represented by
73
78
  * the view tag
@@ -1,2 +1,3 @@
1
1
  export * from "./Image";
2
2
  export * from "./ImageFactory";
3
+ export * from "./ColorType";
@@ -370,3 +370,104 @@ export const convertToAffineMatrix = (m4: Matrix4) => {
370
370
  // Returning the 6-element affine transformation matrix
371
371
  return [a, b, c, d, tx, ty];
372
372
  };
373
+
374
+ /**
375
+ * Calculates the determinant of a 3x3 matrix
376
+ * @worklet
377
+ */
378
+ const det3x3 = (
379
+ a00: number,
380
+ a01: number,
381
+ a02: number,
382
+ a10: number,
383
+ a11: number,
384
+ a12: number,
385
+ a20: number,
386
+ a21: number,
387
+ a22: number
388
+ ): number => {
389
+ "worklet";
390
+ return (
391
+ a00 * (a11 * a22 - a12 * a21) +
392
+ a01 * (a12 * a20 - a10 * a22) +
393
+ a02 * (a10 * a21 - a11 * a20)
394
+ );
395
+ };
396
+
397
+ /**
398
+ * Inverts a 4x4 matrix
399
+ * @worklet
400
+ * @returns The inverted matrix, or the identity matrix if the input is not invertible
401
+ */
402
+ export const invert4 = (m: Matrix4): Matrix4 => {
403
+ "worklet";
404
+
405
+ const a00 = m[0],
406
+ a01 = m[1],
407
+ a02 = m[2],
408
+ a03 = m[3];
409
+ const a10 = m[4],
410
+ a11 = m[5],
411
+ a12 = m[6],
412
+ a13 = m[7];
413
+ const a20 = m[8],
414
+ a21 = m[9],
415
+ a22 = m[10],
416
+ a23 = m[11];
417
+ const a30 = m[12],
418
+ a31 = m[13],
419
+ a32 = m[14],
420
+ a33 = m[15];
421
+
422
+ // Calculate cofactors
423
+ const b00 = det3x3(a11, a12, a13, a21, a22, a23, a31, a32, a33);
424
+ const b01 = -det3x3(a10, a12, a13, a20, a22, a23, a30, a32, a33);
425
+ const b02 = det3x3(a10, a11, a13, a20, a21, a23, a30, a31, a33);
426
+ const b03 = -det3x3(a10, a11, a12, a20, a21, a22, a30, a31, a32);
427
+
428
+ const b10 = -det3x3(a01, a02, a03, a21, a22, a23, a31, a32, a33);
429
+ const b11 = det3x3(a00, a02, a03, a20, a22, a23, a30, a32, a33);
430
+ const b12 = -det3x3(a00, a01, a03, a20, a21, a23, a30, a31, a33);
431
+ const b13 = det3x3(a00, a01, a02, a20, a21, a22, a30, a31, a32);
432
+
433
+ const b20 = det3x3(a01, a02, a03, a11, a12, a13, a31, a32, a33);
434
+ const b21 = -det3x3(a00, a02, a03, a10, a12, a13, a30, a32, a33);
435
+ const b22 = det3x3(a00, a01, a03, a10, a11, a13, a30, a31, a33);
436
+ const b23 = -det3x3(a00, a01, a02, a10, a11, a12, a30, a31, a32);
437
+
438
+ const b30 = -det3x3(a01, a02, a03, a11, a12, a13, a21, a22, a23);
439
+ const b31 = det3x3(a00, a02, a03, a10, a12, a13, a20, a22, a23);
440
+ const b32 = -det3x3(a00, a01, a03, a10, a11, a13, a20, a21, a23);
441
+ const b33 = det3x3(a00, a01, a02, a10, a11, a12, a20, a21, a22);
442
+
443
+ // Calculate determinant
444
+ const det = a00 * b00 + a01 * b01 + a02 * b02 + a03 * b03;
445
+
446
+ // Check if matrix is invertible
447
+ if (Math.abs(det) < 1e-8) {
448
+ // Return identity matrix if not invertible
449
+ return Matrix4();
450
+ }
451
+
452
+ const invDet = 1.0 / det;
453
+
454
+ // Calculate inverse matrix
455
+ return [
456
+ b00 * invDet,
457
+ b10 * invDet,
458
+ b20 * invDet,
459
+ b30 * invDet,
460
+ b01 * invDet,
461
+ b11 * invDet,
462
+ b21 * invDet,
463
+ b31 * invDet,
464
+ b02 * invDet,
465
+ b12 * invDet,
466
+ b22 * invDet,
467
+ b32 * invDet,
468
+ b03 * invDet,
469
+ b13 * invDet,
470
+ b23 * invDet,
471
+ b33 * invDet,
472
+ ] as Matrix4;
473
+ };
@@ -14,6 +14,18 @@ export interface SkSurface extends SkJSIInstance<"Surface"> {
14
14
  */
15
15
  getCanvas(): SkCanvas;
16
16
 
17
+ /**
18
+ * Returns the backend texture of the surface.
19
+ * The returned object can be used to create a Skia Image object.
20
+ * The returned object is backend specific and should be used with caution.
21
+ * It is the caller's responsibility to ensure that the texture is not used after the surface is deleted
22
+ * or draw operations are performed on the surface.
23
+ * The returned object may be null if the surface does not have a backend texture.
24
+ *
25
+ * @return backend texture of the surface or null
26
+ */
27
+ getNativeTextureUnstable(): unknown;
28
+
17
29
  /** Returns Image capturing Surface contents. Subsequent drawing to
18
30
  Surface contents are not captured.
19
31
 
@@ -170,4 +170,9 @@ export class JsiSkImage extends HostObject<Image, "Image"> implements SkImage {
170
170
  }
171
171
  return new JsiSkImage(this.CanvasKit, img);
172
172
  }
173
+
174
+ getNativeTextureUnstable(): unknown {
175
+ console.warn("getBackendTexture is not implemented on Web");
176
+ return null;
177
+ }
173
178
  }
@@ -75,6 +75,10 @@ export class JsiSkImageFactory extends Host implements ImageFactory {
75
75
  return new JsiSkImage(this.CanvasKit, image);
76
76
  }
77
77
 
78
+ MakeImageFromNativeTextureUnstable(): SkImage {
79
+ throw new Error("MakeImageFromNativeTexture is not implemented on web");
80
+ }
81
+
78
82
  MakeImage(info: ImageInfo, data: SkData, bytesPerRow: number) {
79
83
  // see toSkImageInfo() from canvaskit
80
84
  const image = this.CanvasKit.MakeImage(
@@ -43,4 +43,9 @@ export class JsiSkSurface
43
43
  );
44
44
  return new JsiSkImage(this.CanvasKit, image);
45
45
  }
46
+
47
+ getNativeTextureUnstable(): unknown {
48
+ console.warn("getBackendTexture is not implemented on Web");
49
+ return null;
50
+ }
46
51
  }
@@ -3,6 +3,7 @@ import type { ViewProps } from "react-native";
3
3
 
4
4
  export interface NativeProps extends ViewProps {
5
5
  debug?: boolean;
6
+ opaque?: boolean;
6
7
  }
7
8
 
8
9
  // eslint-disable-next-line import/no-default-export
@@ -97,12 +97,13 @@ export class SkiaDomView extends React.Component<SkiaDomViewProps> {
97
97
  }
98
98
 
99
99
  render() {
100
- const { debug = false, ...viewProps } = this.props;
100
+ const { debug = false, opaque = false, ...viewProps } = this.props;
101
101
  return (
102
102
  <NativeSkiaDomView
103
103
  collapsable={false}
104
104
  nativeID={`${this._nativeId}`}
105
105
  debug={debug}
106
+ opaque={opaque}
106
107
  {...viewProps}
107
108
  />
108
109
  );
@@ -38,6 +38,7 @@ export class SkiaPictureView extends React.Component<SkiaPictureViewProps> {
38
38
  }
39
39
 
40
40
  componentDidUpdate(prevProps: SkiaPictureViewProps) {
41
+ console.log("componentDidUpdate");
41
42
  const { picture, onSize } = this.props;
42
43
  if (picture !== prevProps.picture) {
43
44
  assertSkiaViewApi();
@@ -78,16 +79,18 @@ export class SkiaPictureView extends React.Component<SkiaPictureViewProps> {
78
79
  */
79
80
  public redraw() {
80
81
  assertSkiaViewApi();
82
+ console.log("Request redraw: ", this._nativeId);
81
83
  SkiaViewApi.requestRedraw(this._nativeId);
82
84
  }
83
85
 
84
86
  render() {
85
- const { mode, debug = false, ...viewProps } = this.props;
87
+ const { mode, debug = false, opaque = false, ...viewProps } = this.props;
86
88
  return (
87
89
  <NativeSkiaPictureView
88
90
  collapsable={false}
89
91
  nativeID={`${this._nativeId}`}
90
92
  debug={debug}
93
+ opaque={opaque}
91
94
  {...viewProps}
92
95
  />
93
96
  );
@@ -6,6 +6,7 @@ import type { SharedValueType } from "../renderer/processors/Animations/Animatio
6
6
 
7
7
  export type NativeSkiaViewProps = ViewProps & {
8
8
  debug?: boolean;
9
+ opaque?: boolean;
9
10
  };
10
11
 
11
12
  export interface DrawingInfo {
@@ -32,6 +33,8 @@ export interface SkiaBaseViewProps extends ViewProps {
32
33
  * the Skia view is resized.
33
34
  */
34
35
  onSize?: SharedValueType<SkSize>;
36
+
37
+ opaque?: boolean;
35
38
  }
36
39
 
37
40
  export interface SkiaPictureViewNativeProps extends SkiaBaseViewProps {
@@ -1,164 +0,0 @@
1
- #pragma once
2
-
3
- #include <jsi/jsi.h>
4
- #include <memory>
5
- #include <string>
6
-
7
- namespace RNJsi {
8
- namespace jsi = facebook::jsi;
9
-
10
- enum JsiWrapperValueType {
11
- NonInitialized,
12
- Undefined,
13
- Null,
14
- Bool,
15
- Number,
16
- String,
17
- Object,
18
- Function,
19
- Array,
20
- HostObject,
21
- Unknown
22
- };
23
-
24
- /**
25
- Implements a simple wrapper class for JSI values where the value can be read
26
- without asking the runtime for any assistance Meaning that we can access
27
- members without being on the JS thread.
28
- */
29
- class JsiValueWrapper {
30
- public:
31
- explicit JsiValueWrapper(jsi::Runtime &runtime)
32
- : _type(JsiWrapperValueType::Undefined) {}
33
-
34
- JsiValueWrapper(jsi::Runtime &runtime, const jsi::Value &value)
35
- : JsiValueWrapper(runtime) {
36
- setCurrent(runtime, value);
37
- }
38
-
39
- void setCurrent(jsi::Runtime &runtime, const jsi::Value &value) {
40
- if (value.isNumber()) {
41
- _type = JsiWrapperValueType::Number;
42
- _numberValue = value.asNumber();
43
- } else if (value.isBool()) {
44
- _type = JsiWrapperValueType::Bool;
45
- _boolValue = value.getBool();
46
- } else if (value.isString()) {
47
- _type = JsiWrapperValueType::String;
48
- _stringValue = value.asString(runtime).utf8(runtime);
49
- } else if (value.isUndefined()) {
50
- _type = JsiWrapperValueType::Undefined;
51
- } else if (value.isNull()) {
52
- _type = JsiWrapperValueType::Null;
53
- } else if (value.isObject()) {
54
- _type = JsiWrapperValueType::Object;
55
- _objectValue = std::make_shared<jsi::Object>(value.asObject(runtime));
56
- if (_objectValue->isFunction(runtime)) {
57
- _type = JsiWrapperValueType::Function;
58
- _functionValue =
59
- std::make_shared<jsi::Function>(_objectValue->asFunction(runtime));
60
- _objectValue = nullptr;
61
- } else if (_objectValue->isArray(runtime)) {
62
- _type = JsiWrapperValueType::Array;
63
- _arrayValue =
64
- std::make_shared<jsi::Array>(_objectValue->asArray(runtime));
65
- _objectValue = nullptr;
66
- } else if (_objectValue->isHostObject(runtime)) {
67
- _type = JsiWrapperValueType::HostObject;
68
- _hostObjectValue = _objectValue->asHostObject(runtime);
69
- _objectValue = nullptr;
70
- }
71
- } else {
72
- throw std::runtime_error("Could not store jsi::Value of provided type");
73
- }
74
- // Save in value holder as well so that we can return current
75
- if (_valueHolder == nullptr) {
76
- _valueHolder = std::make_shared<jsi::Object>(runtime);
77
- }
78
- _valueHolder->setProperty(runtime, "current", value);
79
- }
80
-
81
- jsi::Value getCurrent(jsi::Runtime &runtime) {
82
- if (_valueHolder == nullptr) {
83
- return jsi::Value::undefined();
84
- }
85
- return _valueHolder->getProperty(runtime, "current");
86
- }
87
-
88
- bool isUndefinedOrNull() { return isUndefined() || isNull(); }
89
-
90
- bool isUndefined() { return _type == JsiWrapperValueType::Undefined; }
91
-
92
- bool isNull() { return _type == JsiWrapperValueType::Null; }
93
-
94
- bool getAsBool() {
95
- assert(_type == JsiWrapperValueType::Bool);
96
- return _boolValue;
97
- }
98
-
99
- double getAsNumber() {
100
- assert(_type == JsiWrapperValueType::Number);
101
- return _numberValue;
102
- }
103
-
104
- const std::string &getAsString() {
105
- assert(_type == JsiWrapperValueType::String);
106
- return _stringValue;
107
- }
108
-
109
- std::shared_ptr<jsi::Function> getAsFunction() {
110
- assert(_type == JsiWrapperValueType::Function);
111
- return _functionValue;
112
- }
113
-
114
- std::shared_ptr<jsi::Array> getAsArray() {
115
- assert(_type == JsiWrapperValueType::Array);
116
- return _arrayValue;
117
- }
118
-
119
- std::shared_ptr<jsi::Object> getAsObject() {
120
- assert(_type == JsiWrapperValueType::Object);
121
- return _objectValue;
122
- }
123
-
124
- std::shared_ptr<jsi::HostObject> getAsHostObject() {
125
- assert(_type == JsiWrapperValueType::HostObject);
126
- return _hostObjectValue;
127
- }
128
-
129
- JsiWrapperValueType getType() { return _type; }
130
-
131
- bool equals(jsi::Runtime &runtime, const jsi::Value &value) {
132
- if (value.isNumber() && _type == JsiWrapperValueType::Number) {
133
- return _numberValue == value.asNumber();
134
- } else if (value.isBool() && _type == JsiWrapperValueType::Bool) {
135
- return _boolValue == value.getBool();
136
- } else if (value.isUndefined()) {
137
- return _type == JsiWrapperValueType::Undefined;
138
- } else if (value.isNull()) {
139
- return _type == JsiWrapperValueType::Null;
140
- } else if (value.isString()) {
141
- auto current = getCurrent(runtime);
142
- if (current.isString()) {
143
- return jsi::String::strictEquals(runtime, value.asString(runtime),
144
- current.asString(runtime));
145
- }
146
- return false;
147
- }
148
- return false;
149
- }
150
-
151
- private:
152
- std::shared_ptr<jsi::Object> _valueHolder;
153
-
154
- bool _boolValue;
155
- double _numberValue;
156
- std::string _stringValue;
157
- std::shared_ptr<jsi::Object> _objectValue;
158
- std::shared_ptr<jsi::Function> _functionValue;
159
- std::shared_ptr<jsi::Array> _arrayValue;
160
- std::shared_ptr<jsi::HostObject> _hostObjectValue;
161
-
162
- JsiWrapperValueType _type;
163
- };
164
- } // namespace RNJsi