@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
@@ -3,97 +3,88 @@ package com.shopify.reactnative.skia;
3
3
  import android.content.Context;
4
4
  import android.graphics.SurfaceTexture;
5
5
  import android.util.Log;
6
- import android.view.TextureView;
6
+ import android.view.Surface;
7
+ import android.view.View;
7
8
 
8
9
  import com.facebook.react.views.view.ReactViewGroup;
9
10
 
10
- public abstract class SkiaBaseView extends ReactViewGroup implements TextureView.SurfaceTextureListener {
11
- private TextureView mTexture;
11
+ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI {
12
+ private View mView;
12
13
 
13
- private String tag = "SkiaView";
14
-
15
- private boolean isDropped = false;
14
+ private final boolean debug = false;
15
+ private final String tag = "SkiaView";
16
16
 
17
17
  public SkiaBaseView(Context context) {
18
18
  super(context);
19
- mTexture = new TextureView(context);
20
- mTexture.setSurfaceTextureListener(this);
21
- mTexture.setOpaque(false);
22
- addView(mTexture);
19
+ mView = new SkiaTextureView(context, this, debug);
20
+ addView(mView);
23
21
  }
24
22
 
25
- private void createSurfaceTexture() {
26
- // This API Level is >= 26, we created our own SurfaceTexture to have a faster time to first frame
27
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
28
- Log.i(tag, "Create SurfaceTexture");
29
- SurfaceTexture surface = new SurfaceTexture(false);
30
- mTexture.setSurfaceTexture(surface);
31
- this.onSurfaceTextureAvailable(surface, this.getMeasuredWidth(), this.getMeasuredHeight());
23
+ public void setOpaque(boolean value) {
24
+ if (value && mView instanceof SkiaTextureView) {
25
+ removeView(mView);
26
+ mView = new SkiaSurfaceView(getContext(), this, debug);
27
+ addView(mView);
28
+ } else if (!value && mView instanceof SkiaSurfaceView) {
29
+ removeView(mView);
30
+ mView = new SkiaTextureView(getContext(), this, debug);
31
+ addView(mView);
32
32
  }
33
33
  }
34
34
 
35
- void dropInstance() {
36
- isDropped = true;
37
- unregisterView();
38
- }
39
-
40
35
  @Override
41
36
  protected void onAttachedToWindow() {
42
37
  super.onAttachedToWindow();
43
- if (this.getMeasuredWidth() == 0) {
44
- createSurfaceTexture();
38
+ if (getMeasuredWidth() == 0) {
39
+ if (mView instanceof SkiaTextureView) {
40
+ ((SkiaTextureView) mView).createSurfaceTexture();
41
+ }
42
+ }
43
+ }
44
+
45
+ void dropInstance() {
46
+ if (mView instanceof SkiaTextureView) {
47
+ ((SkiaTextureView)mView).isDropped = true;
45
48
  }
49
+ unregisterView();
46
50
  }
47
51
 
48
52
  @Override
49
53
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
50
- Log.i(tag, "onLayout " + this.getMeasuredWidth() + "/" + this.getMeasuredHeight());
51
54
  super.onLayout(changed, left, top, right, bottom);
52
- mTexture.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
55
+ mView.layout(0, 0, right - left, bottom - top);
53
56
  }
54
57
 
55
58
  @Override
56
- public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
57
- Log.i(tag, "onSurfaceTextureAvailable " + width + "/" + height);
58
- surfaceAvailable(surface, width, height);
59
+ public void onSurfaceCreated(Surface surface, int width, int height) {
60
+ surfaceAvailable(surface, width, height, true);
59
61
  }
60
62
 
61
63
  @Override
62
- public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
63
- if (isDropped) {
64
- return;
65
- }
64
+ public void onSurfaceChanged(Surface surface, int width, int height) {
66
65
  Log.i(tag, "onSurfaceTextureSizeChanged " + width + "/" + height);
67
- surfaceSizeChanged(surface, width, height);
66
+ surfaceSizeChanged(surface, width, height, true);
68
67
  }
69
68
 
70
69
  @Override
71
- public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
72
- Log.i(tag, "onSurfaceTextureDestroyed");
73
- // https://developer.android.com/reference/android/view/TextureView.SurfaceTextureListener#onSurfaceTextureDestroyed(android.graphics.SurfaceTexture)
74
- surfaceDestroyed();
75
- // Because of React Native Screens (which dettach the view), we always keep the surface alive.
76
- // If not, Texture view will recreate the texture surface by itself and
77
- // we will lose the fast first time to frame.
78
- // We only delete the surface when the view is dropped (destroySurface invoked by SkiaBaseViewManager);
79
- if (!isDropped) {
80
- createSurfaceTexture();
81
- }
82
- return false;
70
+ public void onSurfaceTextureCreated(SurfaceTexture surface, int width, int height) {
71
+ surfaceAvailable(surface, width, height, false);
83
72
  }
84
73
 
85
- private long _prevTimestamp = 0;
86
74
  @Override
87
- public void onSurfaceTextureUpdated(SurfaceTexture surface) {
88
- long timestamp = surface.getTimestamp();
89
- long frameDuration = (timestamp - _prevTimestamp)/1000000;
90
- Log.i(tag, "onSurfaceTextureUpdated "+frameDuration+"ms");
91
- _prevTimestamp = timestamp;
75
+ public void onSurfaceTextureChanged(SurfaceTexture surface, int width, int height) {
76
+ Log.i(tag, "onSurfaceTextureSizeChanged " + width + "/" + height);
77
+ surfaceSizeChanged(surface, width, height, false);
78
+ }
79
+
80
+ @Override
81
+ public void onSurfaceDestroyed() {
82
+ surfaceDestroyed();
92
83
  }
93
84
 
94
- protected abstract void surfaceAvailable(Object surface, int width, int height);
85
+ protected abstract void surfaceAvailable(Object surface, int width, int height, boolean opaque);
95
86
 
96
- protected abstract void surfaceSizeChanged(Object surface, int width, int height);
87
+ protected abstract void surfaceSizeChanged(Object surface, int width, int height, boolean opaque);
97
88
 
98
89
  protected abstract void surfaceDestroyed();
99
90
 
@@ -26,6 +26,11 @@ public abstract class SkiaBaseViewManager<T extends SkiaBaseView> extends ReactV
26
26
  ((SkiaBaseView)view).setDebugMode(show);
27
27
  }
28
28
 
29
+ @ReactProp(name = "opaque")
30
+ public void setOpaque(T view, boolean value) {
31
+ ((SkiaBaseView)view).setOpaque(value);
32
+ }
33
+
29
34
  @Override
30
35
  public void onDropViewInstance(@NonNull ReactViewGroup view) {
31
36
  super.onDropViewInstance(view);
@@ -25,9 +25,9 @@ public class SkiaDomView extends SkiaBaseView {
25
25
 
26
26
  private native HybridData initHybrid(SkiaManager skiaManager);
27
27
 
28
- protected native void surfaceAvailable(Object surface, int width, int height);
28
+ protected native void surfaceAvailable(Object surface, int width, int height, boolean opaque);
29
29
 
30
- protected native void surfaceSizeChanged(Object surface, int width, int height);
30
+ protected native void surfaceSizeChanged(Object surface, int width, int height, boolean opaque);
31
31
 
32
32
  protected native void surfaceDestroyed();
33
33
 
@@ -24,9 +24,9 @@ public class SkiaPictureView extends SkiaBaseView {
24
24
 
25
25
  private native HybridData initHybrid(SkiaManager skiaManager);
26
26
 
27
- protected native void surfaceAvailable(Object surface, int width, int height);
27
+ protected native void surfaceAvailable(Object surface, int width, int height, boolean opaque);
28
28
 
29
- protected native void surfaceSizeChanged(Object surface, int width, int height);
29
+ protected native void surfaceSizeChanged(Object surface, int width, int height, boolean opaque);
30
30
 
31
31
  protected native void surfaceDestroyed();
32
32
 
@@ -0,0 +1,42 @@
1
+ package com.shopify.reactnative.skia;
2
+
3
+ import android.annotation.SuppressLint;
4
+ import android.content.Context;
5
+ import android.view.SurfaceHolder;
6
+ import android.view.SurfaceView;
7
+ import androidx.annotation.NonNull;
8
+
9
+ @SuppressLint("ViewConstructor")
10
+ public class SkiaSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
11
+
12
+ SkiaViewAPI mApi;
13
+ boolean mDebug;
14
+
15
+ public SkiaSurfaceView(Context context, SkiaViewAPI api, boolean debug) {
16
+ super(context);
17
+ mApi = api;
18
+ mDebug = debug;
19
+ getHolder().addCallback(this);
20
+ }
21
+
22
+ @Override
23
+ protected void onDetachedFromWindow() {
24
+ super.onDetachedFromWindow();
25
+ mApi.onSurfaceDestroyed();
26
+ }
27
+
28
+ @Override
29
+ public void surfaceCreated(@NonNull SurfaceHolder holder) {
30
+ mApi.onSurfaceCreated(holder.getSurface(), getWidth(), getHeight());
31
+ }
32
+
33
+ @Override
34
+ public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
35
+ mApi.onSurfaceChanged(holder.getSurface(), getWidth(), getHeight());
36
+ }
37
+
38
+ @Override
39
+ public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
40
+ mApi.onSurfaceDestroyed();
41
+ }
42
+ }
@@ -0,0 +1,90 @@
1
+ package com.shopify.reactnative.skia;
2
+
3
+ import android.annotation.SuppressLint;
4
+ import android.content.Context;
5
+ import android.graphics.SurfaceTexture;
6
+ import android.util.Log;
7
+ import android.view.Surface;
8
+ import android.view.TextureView;
9
+ import androidx.annotation.NonNull;
10
+
11
+ @SuppressLint("ViewConstructor")
12
+ public class SkiaTextureView extends TextureView implements TextureView.SurfaceTextureListener {
13
+
14
+ private String tag = "SkiaTextureView";
15
+
16
+ SkiaViewAPI mApi;
17
+ boolean mDebug;
18
+ boolean pristine = true;
19
+
20
+ public boolean isDropped = false;
21
+
22
+ public SkiaTextureView(Context context, SkiaViewAPI api, boolean debug) {
23
+ super(context);
24
+ mApi = api;
25
+ mDebug = debug;
26
+ setOpaque(false);
27
+ setSurfaceTextureListener(this);
28
+ }
29
+
30
+ public void createSurfaceTexture() {
31
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
32
+ Log.i(tag, "Create SurfaceTexture");
33
+ SurfaceTexture surfaceTexture = new SurfaceTexture(false);
34
+ setSurfaceTexture(surfaceTexture);
35
+ onSurfaceTextureAvailable(surfaceTexture, getWidth(), getHeight());
36
+ }
37
+ }
38
+
39
+ private void reCreateSurfaceTexture() {
40
+ boolean surfaceIsAlreadyAvailable = getSurfaceTexture() != null;
41
+ if (surfaceIsAlreadyAvailable) {
42
+ createSurfaceTexture();
43
+ }
44
+ }
45
+
46
+ @Override
47
+ protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
48
+ super.onLayout(changed, left, top, right, bottom);
49
+ }
50
+
51
+ @Override
52
+ public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surfaceTexture, int width, int height) {
53
+ Log.i(tag, "onSurfaceTextureAvailable: " + width + "x" + height);
54
+ mApi.onSurfaceTextureCreated(surfaceTexture, width, height);
55
+ }
56
+
57
+ @Override
58
+ public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surfaceTexture, int width, int height) {
59
+ Log.i(tag, "onSurfaceTextureSizeChanged: " + width + "x" + height);
60
+ if (isDropped) {
61
+ return;
62
+ }
63
+ mApi.onSurfaceTextureCreated(surfaceTexture, width, height);
64
+ }
65
+
66
+ @Override
67
+ public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surfaceTexture) {
68
+ mApi.onSurfaceDestroyed();
69
+ // Because of React Native Screens (which dettach the view), we always keep the surface alive.
70
+ // If not, Texture view will recreate the texture surface by itself and
71
+ // we will lose the fast first time to frame.
72
+ // We only delete the surface when the view is dropped (destroySurface invoked by SkiaBaseViewManager);
73
+ if (!isDropped) {
74
+ reCreateSurfaceTexture();
75
+ }
76
+ return false;
77
+ }
78
+
79
+ private long _prevTimestamp = 0;
80
+ @Override
81
+ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
82
+ if (!mDebug) {
83
+ return;
84
+ }
85
+ long timestamp = surface.getTimestamp();
86
+ long frameDuration = (timestamp - _prevTimestamp)/1000000;
87
+ Log.i("SkiaTextureView", "onSurfaceTextureUpdated "+frameDuration+"ms");
88
+ _prevTimestamp = timestamp;
89
+ }
90
+ }
@@ -0,0 +1,16 @@
1
+ package com.shopify.reactnative.skia;
2
+
3
+ import android.graphics.SurfaceTexture;
4
+ import android.view.Surface;
5
+
6
+ public interface SkiaViewAPI {
7
+ void onSurfaceCreated(Surface surface, int width, int height);
8
+
9
+ void onSurfaceChanged(Surface surface, int width, int height);
10
+
11
+ void onSurfaceTextureCreated(SurfaceTexture surface, int width, int height);
12
+
13
+ void onSurfaceTextureChanged(SurfaceTexture surface, int width, int height);
14
+
15
+ void onSurfaceDestroyed();
16
+ }
@@ -21,6 +21,9 @@ public class SkiaDomViewManagerDelegate<T extends View, U extends BaseViewManage
21
21
  @Override
22
22
  public void setProperty(T view, String propName, @Nullable Object value) {
23
23
  switch (propName) {
24
+ case "opaque":
25
+ mViewManager.setOpaque(view, value != null && (boolean) value);
26
+ break;
24
27
  case "debug":
25
28
  mViewManager.setDebug(view, value != null && (boolean) value);
26
29
  break;
@@ -14,4 +14,5 @@ import androidx.annotation.Nullable;
14
14
 
15
15
  public interface SkiaDomViewManagerInterface<T extends View> {
16
16
  void setDebug(T view, boolean value);
17
+ void setOpaque(T view, boolean value);
17
18
  }
@@ -24,8 +24,10 @@ public class SkiaPictureViewManagerDelegate<T extends View, U extends BaseViewMa
24
24
  case "mode":
25
25
  mViewManager.setMode(view, value == null ? null : (String) value);
26
26
  break;
27
+ case "opaque":
28
+ mViewManager.setOpaque(view, value != null && (boolean) value);
27
29
  case "debug":
28
- mViewManager.setDebug(view, value == null ? false : (boolean) value);
30
+ mViewManager.setDebug(view, value != null && (boolean) value);
29
31
  break;
30
32
  default:
31
33
  super.setProperty(view, propName, value);
@@ -15,4 +15,5 @@ import androidx.annotation.Nullable;
15
15
  public interface SkiaPictureViewManagerInterface<T extends View> {
16
16
  void setMode(T view, @Nullable String value);
17
17
  void setDebug(T view, boolean value);
18
+ void setOpaque(T view, boolean value);
18
19
  }
@@ -26,11 +26,7 @@ public:
26
26
  JsiSkContourMeasure(std::shared_ptr<RNSkPlatformContext> context,
27
27
  const sk_sp<SkContourMeasure> contourMeasure)
28
28
  : JsiSkWrappingSkPtrHostObject(std::move(context),
29
- std::move(contourMeasure)) {
30
- if (contourMeasure == nullptr) {
31
- throw jsi::JSError(*context->getJsRuntime(), "Contour measure is null");
32
- }
33
- }
29
+ std::move(contourMeasure)) {}
34
30
 
35
31
  JSI_HOST_FUNCTION(getPosTan) {
36
32
  auto dist = arguments[0].asNumber();
@@ -10,6 +10,7 @@
10
10
  #include "JsiSkShader.h"
11
11
  #include "third_party/base64.h"
12
12
 
13
+ #include "JsiTextureInfo.h"
13
14
  #include "RNSkTypedArray.h"
14
15
 
15
16
  #if defined(SK_GRAPHITE)
@@ -215,6 +216,15 @@ public:
215
216
  runtime, std::make_shared<JsiSkImage>(getContext(), rasterImage));
216
217
  }
217
218
 
219
+ JSI_HOST_FUNCTION(getNativeTextureUnstable) {
220
+ auto image = getObject();
221
+ if (!image->isTextureBacked()) {
222
+ return jsi::Value::null();
223
+ }
224
+ auto texInfo = getContext()->getTexture(image);
225
+ return JsiTextureInfo::toValue(runtime, texInfo);
226
+ }
227
+
218
228
  EXPORT_JSI_API_TYPENAME(JsiSkImage, Image)
219
229
 
220
230
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkImage, width),
@@ -226,6 +236,7 @@ public:
226
236
  JSI_EXPORT_FUNC(JsiSkImage, encodeToBase64),
227
237
  JSI_EXPORT_FUNC(JsiSkImage, readPixels),
228
238
  JSI_EXPORT_FUNC(JsiSkImage, makeNonTextureImage),
239
+ JSI_EXPORT_FUNC(JsiSkImage, getNativeTextureUnstable),
229
240
  JSI_EXPORT_FUNC(JsiSkImage, dispose))
230
241
 
231
242
  JsiSkImage(std::shared_ptr<RNSkPlatformContext> context,
@@ -78,10 +78,24 @@ public:
78
78
  });
79
79
  }
80
80
 
81
+ JSI_HOST_FUNCTION(MakeImageFromNativeTextureUnstable) {
82
+ auto texInfo = JsiTextureInfo::fromValue(runtime, arguments[0]);
83
+ auto image = getContext()->makeImageFromNativeTexture(
84
+ texInfo, arguments[1].asNumber(), arguments[2].asNumber(),
85
+ count > 3 && arguments[3].asBool());
86
+ if (image == nullptr) {
87
+ throw std::runtime_error("Failed to convert native texture to SkImage!");
88
+ }
89
+ return jsi::Object::createFromHostObject(
90
+ runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
91
+ }
92
+
81
93
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImageFromEncoded),
82
94
  JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImageFromViewTag),
83
95
  JSI_EXPORT_FUNC(JsiSkImageFactory,
84
96
  MakeImageFromNativeBuffer),
97
+ JSI_EXPORT_FUNC(JsiSkImageFactory,
98
+ MakeImageFromNativeTextureUnstable),
85
99
  JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImage))
86
100
 
87
101
  explicit JsiSkImageFactory(std::shared_ptr<RNSkPlatformContext> context)
@@ -4,6 +4,8 @@
4
4
 
5
5
  #include "JsiSkData.h"
6
6
  #include "JsiSkHostObjects.h"
7
+ #include "JsiSkMatrix.h"
8
+ #include "JsiSkRect.h"
7
9
  #include "JsiSkShader.h"
8
10
 
9
11
  #pragma clang diagnostic push
@@ -6,6 +6,7 @@
6
6
  #include <jsi/jsi.h>
7
7
 
8
8
  #include "JsiSkHostObjects.h"
9
+ #include "JsiTextureInfo.h"
9
10
 
10
11
  #include "JsiSkCanvas.h"
11
12
  #include "JsiSkImage.h"
@@ -78,11 +79,17 @@ public:
78
79
  runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
79
80
  }
80
81
 
82
+ JSI_HOST_FUNCTION(getNativeTextureUnstable) {
83
+ auto texInfo = getContext()->getTexture(getObject());
84
+ return JsiTextureInfo::toValue(runtime, texInfo);
85
+ }
86
+
81
87
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkSurface, width),
82
88
  JSI_EXPORT_FUNC(JsiSkSurface, height),
83
89
  JSI_EXPORT_FUNC(JsiSkSurface, getCanvas),
84
90
  JSI_EXPORT_FUNC(JsiSkSurface, makeImageSnapshot),
85
91
  JSI_EXPORT_FUNC(JsiSkSurface, flush),
92
+ JSI_EXPORT_FUNC(JsiSkSurface, getNativeTextureUnstable),
86
93
  JSI_EXPORT_FUNC(JsiSkSurface, dispose))
87
94
  };
88
95
 
@@ -0,0 +1,53 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ #include "RNSkPlatformContext.h"
6
+
7
+ namespace jsi = facebook::jsi;
8
+ namespace react = facebook::react;
9
+
10
+ namespace RNSkia {
11
+
12
+ namespace JsiTextureInfo {
13
+
14
+ inline jsi::Value toValue(jsi::Runtime &runtime, const TextureInfo &texInfo) {
15
+ jsi::Object textureInfo(runtime);
16
+ textureInfo.setProperty(
17
+ runtime, "mtlTexture",
18
+ jsi::BigInt::fromUint64(runtime,
19
+ reinterpret_cast<uint64_t>(texInfo.mtlTexture)));
20
+ textureInfo.setProperty(runtime, "glTarget",
21
+ static_cast<int>(texInfo.glTarget));
22
+ textureInfo.setProperty(runtime, "glID", static_cast<int>(texInfo.glID));
23
+ textureInfo.setProperty(runtime, "glFormat",
24
+ static_cast<int>(texInfo.glFormat));
25
+ textureInfo.setProperty(runtime, "glProtected",
26
+ static_cast<int>(texInfo.glProtected));
27
+ return textureInfo;
28
+ }
29
+
30
+ inline TextureInfo fromValue(jsi::Runtime &runtime, const jsi::Value &value) {
31
+ auto object = value.getObject(runtime);
32
+ TextureInfo texInfo;
33
+ if (object.hasProperty(runtime, "mtlTexture")) {
34
+ texInfo.mtlTexture =
35
+ reinterpret_cast<const void *>(object.getProperty(runtime, "mtlTexture")
36
+ .asBigInt(runtime)
37
+ .asUint64(runtime));
38
+ }
39
+ if (object.hasProperty(runtime, "glID")) {
40
+ texInfo.glTarget = static_cast<unsigned int>(
41
+ object.getProperty(runtime, "glTarget").asNumber());
42
+ texInfo.glID = static_cast<unsigned int>(
43
+ object.getProperty(runtime, "glID").asNumber());
44
+ texInfo.glFormat = static_cast<unsigned int>(
45
+ object.getProperty(runtime, "glFormat").asNumber());
46
+ texInfo.glProtected =
47
+ object.getProperty(runtime, "glProtected").asNumber() != 0;
48
+ }
49
+ return texInfo;
50
+ }
51
+
52
+ } // namespace JsiTextureInfo
53
+ } // namespace RNSkia
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "JsiDomRenderNode.h"
8
+ #include "JsiSkPicture.h"
9
+
10
+ namespace RNJsi {
11
+ namespace jsi = facebook::jsi;
12
+
13
+ class ViewProperty {
14
+ public:
15
+ ViewProperty(jsi::Runtime &runtime, const jsi::Value &value) {
16
+ if (value.isObject()) {
17
+ auto object = value.asObject(runtime);
18
+ if (object.isHostObject(runtime)) {
19
+ auto hostObject = object.asHostObject(runtime);
20
+ auto dom =
21
+ std::dynamic_pointer_cast<RNSkia::JsiDomRenderNode>(hostObject);
22
+ if (dom) {
23
+ _value = dom;
24
+ } else {
25
+ auto jsiPicture =
26
+ std::dynamic_pointer_cast<RNSkia::JsiSkPicture>(hostObject);
27
+ if (jsiPicture) {
28
+ _value = jsiPicture->getObject();
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ bool isNull() { return std::holds_alternative<nullptr_t>(_value); }
36
+
37
+ std::shared_ptr<RNSkia::JsiDomRenderNode> getDomRenderNode() {
38
+ return std::get<std::shared_ptr<RNSkia::JsiDomRenderNode>>(_value);
39
+ }
40
+
41
+ sk_sp<SkPicture> getPicture() { return std::get<sk_sp<SkPicture>>(_value); }
42
+
43
+ private:
44
+ std::variant<nullptr_t, sk_sp<SkPicture>,
45
+ std::shared_ptr<RNSkia::JsiDomRenderNode>>
46
+ _value = nullptr;
47
+ };
48
+ } // namespace RNJsi
@@ -26,26 +26,6 @@ RNSkDomRenderer::~RNSkDomRenderer() {
26
26
  }
27
27
  }
28
28
 
29
- bool RNSkDomRenderer::tryRender(
30
- std::shared_ptr<RNSkCanvasProvider> canvasProvider) {
31
-
32
- // We render on the main thread
33
- if (_renderLock->try_lock()) {
34
- bool result = false;
35
- // If we have a Dom Node we can render directly on the main thread
36
- if (_root != nullptr) {
37
- result = canvasProvider->renderToCanvas(std::bind(
38
- &RNSkDomRenderer::renderCanvas, this, std::placeholders::_1,
39
- canvasProvider->getScaledWidth(), canvasProvider->getScaledHeight()));
40
- }
41
-
42
- _renderLock->unlock();
43
- return result;
44
- } else {
45
- return false;
46
- }
47
- }
48
-
49
29
  void RNSkDomRenderer::renderImmediate(
50
30
  std::shared_ptr<RNSkCanvasProvider> canvasProvider) {
51
31
  auto prevDebugOverlay = getShowDebugOverlays();
@@ -9,8 +9,8 @@
9
9
 
10
10
  #include <jsi/jsi.h>
11
11
 
12
- #include "JsiValueWrapper.h"
13
12
  #include "RNSkView.h"
13
+ #include "ViewProperty.h"
14
14
 
15
15
  #include "JsiDomRenderNode.h"
16
16
  #include "RNSkLog.h"
@@ -42,8 +42,6 @@ public:
42
42
 
43
43
  ~RNSkDomRenderer();
44
44
 
45
- bool tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override;
46
-
47
45
  void
48
46
  renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override;
49
47
 
@@ -77,20 +75,17 @@ public:
77
75
  std::bind(&RNSkView::requestRedraw, this), context)) {}
78
76
 
79
77
  void setJsiProperties(
80
- std::unordered_map<std::string, JsiValueWrapper> &props) override {
81
-
82
- RNSkView::setJsiProperties(props);
78
+ std::unordered_map<std::string, ViewProperty> &props) override {
83
79
 
84
80
  for (auto &prop : props) {
85
81
  if (prop.first == "root") {
86
82
  // Save root
87
- if (prop.second.isUndefined() || prop.second.isNull()) {
83
+ if (prop.second.isNull()) {
88
84
  std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
89
85
  ->setRoot(nullptr);
90
86
  } else {
91
87
  std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
92
- ->setRoot(std::dynamic_pointer_cast<JsiDomRenderNode>(
93
- prop.second.getAsHostObject()));
88
+ ->setRoot(prop.second.getDomRenderNode());
94
89
  }
95
90
 
96
91
  // Request redraw