react-native-skia 3.0.1 → 3.0.2

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 (260) hide show
  1. package/Package.swift +14 -13
  2. package/android/CMakeLists.txt +13 -42
  3. package/android/build.gradle +54 -63
  4. package/android/cpp/jni/JniLoad.cpp +2 -4
  5. package/android/cpp/jni/include/JniSkiaView.h +123 -0
  6. package/android/cpp/rnskia-android/{RNSkOpenGLCanvasProvider.cpp → RNSkAndroidCanvasProvider.cpp} +45 -100
  7. package/android/cpp/rnskia-android/{RNSkOpenGLCanvasProvider.h → RNSkAndroidCanvasProvider.h} +24 -22
  8. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +0 -83
  9. package/android/cpp/rnskia-android/RNSkAndroidVideo.cpp +0 -8
  10. package/android/src/main/java/com/reactnative/skia/RNSkiaPackage.java +1 -2
  11. package/android/src/main/java/com/reactnative/skia/SkiaSurfaceView.java +1 -3
  12. package/android/src/main/java/com/reactnative/skia/SkiaTextureView.java +1 -17
  13. package/android/src/main/java/com/reactnative/skia/{SkiaBaseView.java → SkiaView.java} +78 -16
  14. package/android/src/main/java/com/reactnative/skia/SkiaViewManager.java +85 -0
  15. package/apple/MetalLayerColorSpace.mm +0 -4
  16. package/apple/RNSkApplePlatformContext.h +0 -14
  17. package/apple/RNSkApplePlatformContext.mm +2 -112
  18. package/apple/RNSkMetalCanvasProvider.h +14 -19
  19. package/apple/RNSkMetalCanvasProvider.mm +35 -68
  20. package/apple/SkiaView.h +20 -0
  21. package/apple/SkiaView.mm +210 -0
  22. package/cpp/api/JsiSkApi.h +2 -13
  23. package/cpp/api/JsiSkCanvas.h +4 -18
  24. package/cpp/api/JsiSkGraphiteContext.h +1 -5
  25. package/cpp/api/JsiSkGraphiteRecording.h +1 -5
  26. package/cpp/api/JsiSkImage.h +7 -41
  27. package/cpp/api/JsiSkImageFactory.h +0 -14
  28. package/cpp/api/JsiSkSurface.h +2 -16
  29. package/cpp/api/JsiSkSurfaceFactory.h +0 -8
  30. package/cpp/rnskia/RNDawnInterop.cpp +0 -4
  31. package/cpp/rnskia/RNDawnUtils.h +0 -1
  32. package/cpp/rnskia/RNDawnWindowContext.cpp +10 -0
  33. package/cpp/rnskia/RNDawnWindowContext.h +4 -0
  34. package/cpp/rnskia/RNSkCanvasProvider.h +221 -0
  35. package/cpp/rnskia/RNSkGraphiteProducer.cpp +36 -14
  36. package/cpp/rnskia/RNSkGraphiteProducer.h +23 -8
  37. package/cpp/rnskia/RNSkGraphiteTarget.h +383 -0
  38. package/cpp/rnskia/RNSkJsiViewApi.h +9 -18
  39. package/cpp/rnskia/RNSkPlatformContext.h +9 -15
  40. package/cpp/rnskia/RNSkView.h +340 -245
  41. package/jestSetup.js +1 -3
  42. package/lib/commonjs/Platform/Platform.web.js +1 -1
  43. package/lib/commonjs/Platform/Platform.web.js.map +1 -1
  44. package/lib/commonjs/index.d.ts +1 -3
  45. package/lib/commonjs/index.js +3 -12
  46. package/lib/commonjs/index.js.map +1 -1
  47. package/lib/commonjs/mock/index.js +0 -1
  48. package/lib/commonjs/mock/index.js.map +1 -1
  49. package/lib/commonjs/renderer/Canvas.d.ts +15 -12
  50. package/lib/commonjs/renderer/Canvas.js +24 -20
  51. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  52. package/lib/commonjs/skia/types/Graphite.d.ts +3 -3
  53. package/lib/commonjs/skia/types/Graphite.js.map +1 -1
  54. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +2 -2
  55. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  56. package/lib/commonjs/skia/types/Skia.d.ts +1 -2
  57. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  58. package/lib/commonjs/skia/types/Surface/SurfaceFactory.d.ts +1 -1
  59. package/lib/commonjs/skia/types/Surface/SurfaceFactory.js.map +1 -1
  60. package/lib/commonjs/specs/NativeSkiaModule.web.d.ts +5 -4
  61. package/lib/commonjs/specs/NativeSkiaModule.web.js +5 -9
  62. package/lib/commonjs/specs/NativeSkiaModule.web.js.map +1 -1
  63. package/lib/{typescript/src/specs/SkiaPictureViewNativeComponent.d.ts → commonjs/specs/SkiaViewNativeComponent.d.ts} +0 -3
  64. package/lib/commonjs/specs/{SkiaPictureViewNativeComponent.js → SkiaViewNativeComponent.js} +2 -2
  65. package/lib/commonjs/specs/SkiaViewNativeComponent.js.map +1 -0
  66. package/lib/commonjs/specs/SkiaViewNativeComponent.web.d.ts +10 -0
  67. package/lib/commonjs/specs/SkiaViewNativeComponent.web.js +27 -0
  68. package/lib/commonjs/specs/SkiaViewNativeComponent.web.js.map +1 -0
  69. package/lib/commonjs/views/SkiaGraphiteView.d.ts +3 -4
  70. package/lib/commonjs/views/SkiaGraphiteView.js +23 -12
  71. package/lib/commonjs/views/SkiaGraphiteView.js.map +1 -1
  72. package/lib/commonjs/views/SkiaPictureView.d.ts +4 -1
  73. package/lib/commonjs/views/SkiaPictureView.js +12 -10
  74. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  75. package/lib/commonjs/views/SkiaView.web.d.ts +12 -0
  76. package/lib/commonjs/views/{SkiaGraphiteView.web.js → SkiaView.web.js} +82 -38
  77. package/lib/commonjs/views/SkiaView.web.js.map +1 -0
  78. package/lib/commonjs/views/SkiaWebRenderer.js +1 -1
  79. package/lib/commonjs/views/SkiaWebRenderer.js.map +1 -1
  80. package/lib/commonjs/views/android.d.ts +6 -0
  81. package/lib/commonjs/views/android.js +17 -0
  82. package/lib/commonjs/views/android.js.map +1 -0
  83. package/lib/commonjs/views/types.d.ts +3 -12
  84. package/lib/commonjs/views/types.js.map +1 -1
  85. package/lib/module/Platform/Platform.web.js +1 -1
  86. package/lib/module/Platform/Platform.web.js.map +1 -1
  87. package/lib/module/index.d.ts +1 -3
  88. package/lib/module/index.js +1 -3
  89. package/lib/module/index.js.map +1 -1
  90. package/lib/module/mock/index.js +0 -1
  91. package/lib/module/mock/index.js.map +1 -1
  92. package/lib/module/renderer/Canvas.d.ts +15 -12
  93. package/lib/module/renderer/Canvas.js +24 -19
  94. package/lib/module/renderer/Canvas.js.map +1 -1
  95. package/lib/module/skia/types/Graphite.d.ts +3 -3
  96. package/lib/module/skia/types/Graphite.js.map +1 -1
  97. package/lib/module/skia/types/Image/ImageFactory.d.ts +2 -2
  98. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  99. package/lib/module/skia/types/Skia.d.ts +1 -2
  100. package/lib/module/skia/types/Skia.js.map +1 -1
  101. package/lib/module/skia/types/Surface/SurfaceFactory.d.ts +1 -1
  102. package/lib/module/skia/types/Surface/SurfaceFactory.js.map +1 -1
  103. package/lib/module/specs/NativeSkiaModule.web.d.ts +5 -4
  104. package/lib/module/specs/NativeSkiaModule.web.js +5 -9
  105. package/lib/module/specs/NativeSkiaModule.web.js.map +1 -1
  106. package/lib/{commonjs/specs/SkiaPictureViewNativeComponent.d.ts → module/specs/SkiaViewNativeComponent.d.ts} +0 -3
  107. package/lib/module/specs/{SkiaPictureViewNativeComponent.js → SkiaViewNativeComponent.js} +2 -2
  108. package/lib/module/specs/SkiaViewNativeComponent.js.map +1 -0
  109. package/lib/module/specs/SkiaViewNativeComponent.web.d.ts +10 -0
  110. package/lib/module/specs/SkiaViewNativeComponent.web.js +21 -0
  111. package/lib/module/specs/SkiaViewNativeComponent.web.js.map +1 -0
  112. package/lib/module/views/SkiaGraphiteView.d.ts +3 -4
  113. package/lib/module/views/SkiaGraphiteView.js +23 -12
  114. package/lib/module/views/SkiaGraphiteView.js.map +1 -1
  115. package/lib/module/views/SkiaPictureView.d.ts +4 -1
  116. package/lib/module/views/SkiaPictureView.js +12 -10
  117. package/lib/module/views/SkiaPictureView.js.map +1 -1
  118. package/lib/module/views/SkiaView.web.d.ts +12 -0
  119. package/lib/module/views/{SkiaGraphiteView.web.js → SkiaView.web.js} +80 -36
  120. package/lib/module/views/SkiaView.web.js.map +1 -0
  121. package/lib/module/views/SkiaWebRenderer.js +1 -1
  122. package/lib/module/views/SkiaWebRenderer.js.map +1 -1
  123. package/lib/module/views/android.d.ts +6 -0
  124. package/lib/module/views/android.js +10 -0
  125. package/lib/module/views/android.js.map +1 -0
  126. package/lib/module/views/types.d.ts +3 -12
  127. package/lib/module/views/types.js.map +1 -1
  128. package/lib/typescript/src/index.d.ts +1 -3
  129. package/lib/typescript/src/renderer/Canvas.d.ts +15 -12
  130. package/lib/typescript/src/skia/types/Graphite.d.ts +3 -3
  131. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +2 -2
  132. package/lib/typescript/src/skia/types/Skia.d.ts +1 -2
  133. package/lib/typescript/src/skia/types/Surface/SurfaceFactory.d.ts +1 -1
  134. package/lib/typescript/src/specs/NativeSkiaModule.web.d.ts +5 -4
  135. package/lib/{module/specs/SkiaPictureViewNativeComponent.d.ts → typescript/src/specs/SkiaViewNativeComponent.d.ts} +0 -3
  136. package/lib/typescript/src/specs/SkiaViewNativeComponent.web.d.ts +10 -0
  137. package/lib/typescript/src/views/SkiaGraphiteView.d.ts +3 -4
  138. package/lib/typescript/src/views/SkiaPictureView.d.ts +4 -1
  139. package/lib/typescript/src/views/SkiaView.web.d.ts +12 -0
  140. package/lib/typescript/src/views/android.d.ts +6 -0
  141. package/lib/typescript/src/views/types.d.ts +3 -12
  142. package/package.json +5 -13
  143. package/react-native-skia.podspec +34 -58
  144. package/src/Platform/Platform.web.tsx +1 -1
  145. package/src/index.ts +1 -3
  146. package/src/mock/index.ts +0 -1
  147. package/src/renderer/Canvas.tsx +23 -27
  148. package/src/skia/types/Graphite.ts +3 -3
  149. package/src/skia/types/Image/ImageFactory.ts +2 -2
  150. package/src/skia/types/Skia.ts +1 -2
  151. package/src/skia/types/Surface/SurfaceFactory.ts +1 -1
  152. package/src/specs/NativeSkiaModule.web.ts +9 -21
  153. package/src/specs/{SkiaPictureViewNativeComponent.ts → SkiaViewNativeComponent.ts} +1 -4
  154. package/src/specs/{SkiaPictureViewNativeComponent.web.ts → SkiaViewNativeComponent.web.ts} +8 -10
  155. package/src/views/SkiaGraphiteView.tsx +27 -16
  156. package/src/views/SkiaPictureView.tsx +11 -10
  157. package/src/views/{SkiaGraphiteView.web.tsx → SkiaView.web.tsx} +100 -53
  158. package/src/views/SkiaWebRenderer.ts +1 -1
  159. package/src/views/android.ts +16 -0
  160. package/src/views/types.ts +3 -14
  161. package/android/cpp/jni/include/JniSkiaBaseView.h +0 -92
  162. package/android/cpp/jni/include/JniSkiaGraphiteView.h +0 -176
  163. package/android/cpp/jni/include/JniSkiaPictureView.h +0 -172
  164. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.cpp +0 -245
  165. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.h +0 -33
  166. package/android/cpp/rnskia-android/OpenGLContext.h +0 -248
  167. package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +0 -67
  168. package/android/cpp/rnskia-android/OpenGLWindowContext.h +0 -75
  169. package/android/cpp/rnskia-android/RNSkAndroidView.h +0 -67
  170. package/android/cpp/rnskia-android/gl/Context.h +0 -80
  171. package/android/cpp/rnskia-android/gl/Display.h +0 -123
  172. package/android/cpp/rnskia-android/gl/Error.cpp +0 -45
  173. package/android/cpp/rnskia-android/gl/Error.h +0 -8
  174. package/android/cpp/rnskia-android/gl/Surface.h +0 -43
  175. package/android/src/main/java/com/reactnative/skia/SkiaBaseViewManager.java +0 -65
  176. package/android/src/main/java/com/reactnative/skia/SkiaGraphiteView.java +0 -78
  177. package/android/src/main/java/com/reactnative/skia/SkiaGraphiteViewManager.java +0 -32
  178. package/android/src/main/java/com/reactnative/skia/SkiaPictureView.java +0 -105
  179. package/android/src/main/java/com/reactnative/skia/SkiaPictureViewManager.java +0 -44
  180. package/apple/MetalContext.h +0 -116
  181. package/apple/MetalContext.mm +0 -38
  182. package/apple/MetalWindowContext.h +0 -42
  183. package/apple/MetalWindowContext.mm +0 -95
  184. package/apple/RNSkAppleView.h +0 -49
  185. package/apple/RNSkAppleView.mm +0 -35
  186. package/apple/SkiaCVPixelBufferUtils.h +0 -122
  187. package/apple/SkiaCVPixelBufferUtils.mm +0 -535
  188. package/apple/SkiaGraphiteView.h +0 -26
  189. package/apple/SkiaGraphiteView.mm +0 -180
  190. package/apple/SkiaPictureView.h +0 -7
  191. package/apple/SkiaPictureView.mm +0 -70
  192. package/apple/SkiaUIView.h +0 -31
  193. package/apple/SkiaUIView.mm +0 -164
  194. package/cpp/rnskia/RNSkGraphiteView.h +0 -708
  195. package/cpp/rnskia/RNSkPictureView.cpp +0 -69
  196. package/cpp/rnskia/RNSkPictureView.h +0 -211
  197. package/cpp/skia/src/gpu/ganesh/gl/GrGLDefines.h +0 -1162
  198. package/lib/commonjs/renderer/DefaultCanvas.d.ts +0 -8
  199. package/lib/commonjs/renderer/DefaultCanvas.js +0 -42
  200. package/lib/commonjs/renderer/DefaultCanvas.js.map +0 -1
  201. package/lib/commonjs/renderer/GraphiteCanvas.d.ts +0 -22
  202. package/lib/commonjs/renderer/GraphiteCanvas.js +0 -64
  203. package/lib/commonjs/renderer/GraphiteCanvas.js.map +0 -1
  204. package/lib/commonjs/renderer/GraphiteCanvas.web.d.ts +0 -1
  205. package/lib/commonjs/renderer/GraphiteCanvas.web.js +0 -13
  206. package/lib/commonjs/renderer/GraphiteCanvas.web.js.map +0 -1
  207. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.d.ts +0 -10
  208. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.js +0 -12
  209. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.js.map +0 -1
  210. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.web.d.ts +0 -9
  211. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.web.js +0 -28
  212. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.web.js.map +0 -1
  213. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +0 -1
  214. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +0 -10
  215. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js +0 -29
  216. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js.map +0 -1
  217. package/lib/commonjs/views/SkiaGraphiteView.web.d.ts +0 -14
  218. package/lib/commonjs/views/SkiaGraphiteView.web.js.map +0 -1
  219. package/lib/commonjs/views/SkiaPictureView.web.d.ts +0 -11
  220. package/lib/commonjs/views/SkiaPictureView.web.js +0 -165
  221. package/lib/commonjs/views/SkiaPictureView.web.js.map +0 -1
  222. package/lib/module/renderer/DefaultCanvas.d.ts +0 -8
  223. package/lib/module/renderer/DefaultCanvas.js +0 -34
  224. package/lib/module/renderer/DefaultCanvas.js.map +0 -1
  225. package/lib/module/renderer/GraphiteCanvas.d.ts +0 -22
  226. package/lib/module/renderer/GraphiteCanvas.js +0 -57
  227. package/lib/module/renderer/GraphiteCanvas.js.map +0 -1
  228. package/lib/module/renderer/GraphiteCanvas.web.d.ts +0 -1
  229. package/lib/module/renderer/GraphiteCanvas.web.js +0 -4
  230. package/lib/module/renderer/GraphiteCanvas.web.js.map +0 -1
  231. package/lib/module/specs/SkiaGraphiteViewNativeComponent.d.ts +0 -10
  232. package/lib/module/specs/SkiaGraphiteViewNativeComponent.js +0 -8
  233. package/lib/module/specs/SkiaGraphiteViewNativeComponent.js.map +0 -1
  234. package/lib/module/specs/SkiaGraphiteViewNativeComponent.web.d.ts +0 -9
  235. package/lib/module/specs/SkiaGraphiteViewNativeComponent.web.js +0 -22
  236. package/lib/module/specs/SkiaGraphiteViewNativeComponent.web.js.map +0 -1
  237. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +0 -1
  238. package/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +0 -10
  239. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js +0 -23
  240. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js.map +0 -1
  241. package/lib/module/views/SkiaGraphiteView.web.d.ts +0 -14
  242. package/lib/module/views/SkiaGraphiteView.web.js.map +0 -1
  243. package/lib/module/views/SkiaPictureView.web.d.ts +0 -11
  244. package/lib/module/views/SkiaPictureView.web.js +0 -156
  245. package/lib/module/views/SkiaPictureView.web.js.map +0 -1
  246. package/lib/typescript/src/renderer/DefaultCanvas.d.ts +0 -8
  247. package/lib/typescript/src/renderer/GraphiteCanvas.d.ts +0 -22
  248. package/lib/typescript/src/renderer/GraphiteCanvas.web.d.ts +0 -1
  249. package/lib/typescript/src/specs/SkiaGraphiteViewNativeComponent.d.ts +0 -10
  250. package/lib/typescript/src/specs/SkiaGraphiteViewNativeComponent.web.d.ts +0 -9
  251. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.web.d.ts +0 -10
  252. package/lib/typescript/src/views/SkiaGraphiteView.web.d.ts +0 -14
  253. package/lib/typescript/src/views/SkiaPictureView.web.d.ts +0 -11
  254. package/libs/.graphite +0 -1
  255. package/src/renderer/DefaultCanvas.tsx +0 -41
  256. package/src/renderer/GraphiteCanvas.tsx +0 -61
  257. package/src/renderer/GraphiteCanvas.web.tsx +0 -3
  258. package/src/specs/SkiaGraphiteViewNativeComponent.ts +0 -17
  259. package/src/specs/SkiaGraphiteViewNativeComponent.web.ts +0 -31
  260. package/src/views/SkiaPictureView.web.tsx +0 -224
@@ -3,18 +3,33 @@ package com.reactnative.skia;
3
3
  import android.content.Context;
4
4
  import android.graphics.SurfaceTexture;
5
5
  import android.util.Log;
6
+ import android.view.Choreographer;
6
7
  import android.view.MotionEvent;
7
8
  import android.view.Surface;
8
9
  import android.view.View;
9
10
 
11
+ import com.facebook.jni.HybridData;
12
+ import com.facebook.jni.annotations.DoNotStrip;
13
+ import com.facebook.react.bridge.ReactContext;
10
14
  import com.facebook.react.uimanager.PointerEvents;
11
15
  import com.facebook.react.views.view.ReactViewGroup;
12
16
 
13
- public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI {
17
+ /**
18
+ * The Android view behind <Canvas>, <SkiaPictureView> and <SkiaGraphiteView>.
19
+ * It is backed by a SurfaceView or a TextureView (see updateView()) that the
20
+ * native side draws into with Graphite, presenting the queued recordings on
21
+ * the Choreographer.
22
+ */
23
+ public class SkiaView extends ReactViewGroup implements SkiaViewAPI, Choreographer.FrameCallback {
14
24
  // Backing view kinds, see updateView().
15
25
  private static final int KIND_SURFACE_VIEW = 0;
16
26
  private static final int KIND_TEXTURE_VIEW = 1;
17
27
 
28
+ private static final String TAG = "SkiaView";
29
+
30
+ @DoNotStrip
31
+ private HybridData mHybridData;
32
+
18
33
  private View mView;
19
34
 
20
35
  // Props, applied together in updateView().
@@ -28,11 +43,12 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
28
43
  private boolean mAppliedZOrderOnTop;
29
44
  private boolean mAppliedHighBitDepth;
30
45
 
31
- private final boolean debug = false;
32
- private final String tag = "SkiaView";
46
+ private boolean mFramePending = false;
33
47
 
34
- public SkiaBaseView(Context context) {
48
+ public SkiaView(Context context) {
35
49
  super(context);
50
+ RNSkiaModule skiaModule = ((ReactContext) context).getNativeModule(RNSkiaModule.class);
51
+ mHybridData = initHybrid(skiaModule.getSkiaManager());
36
52
  }
37
53
 
38
54
  @Override
@@ -82,7 +98,7 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
82
98
  return false;
83
99
  }
84
100
  if (kind != KIND_SURFACE_VIEW || !mOpaque) {
85
- Log.w(tag, "highBitDepth requires an opaque SurfaceView on Android, falling back to the 8-bit format");
101
+ Log.w(TAG, "highBitDepth requires an opaque SurfaceView on Android, falling back to the 8-bit format");
86
102
  return false;
87
103
  }
88
104
  return true;
@@ -107,8 +123,8 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
107
123
  mAppliedZOrderOnTop = zOrderOnTop;
108
124
  mAppliedHighBitDepth = highBitDepth;
109
125
  mView = kind == KIND_SURFACE_VIEW
110
- ? new SkiaSurfaceView(getContext(), this, debug, zOrderOnTop, mOpaque)
111
- : new SkiaTextureView(getContext(), this, debug, mOpaque);
126
+ ? new SkiaSurfaceView(getContext(), this, zOrderOnTop, mOpaque)
127
+ : new SkiaTextureView(getContext(), this, mOpaque);
112
128
  addView(mView);
113
129
  // React Native sizes native children explicitly through onLayout, so
114
130
  // the requestLayout triggered by addView is ignored; size the new
@@ -130,11 +146,52 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
130
146
  @Override
131
147
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
132
148
  super.onLayout(changed, left, top, right, bottom);
149
+ // The backing view gets this exact size, and so does its surface: tell
150
+ // the native side now, so that a frame recorded before the surface
151
+ // exists (the first one usually is) already has the right size.
152
+ setLayoutSize(right - left, bottom - top);
133
153
  if (mView != null) {
134
154
  mView.layout(0, 0, right - left, bottom - top);
135
155
  }
136
156
  }
137
157
 
158
+ // Frames --------------------------------------------------------------
159
+
160
+ /**
161
+ * Presents the queued recordings on the next vsync. Main thread; called
162
+ * from native when a recording is submitted.
163
+ */
164
+ @DoNotStrip
165
+ public void scheduleFrame() {
166
+ if (!mFramePending) {
167
+ mFramePending = true;
168
+ Choreographer.getInstance().postFrameCallback(this);
169
+ }
170
+ }
171
+
172
+ @Override
173
+ public void doFrame(long frameTimeNanos) {
174
+ mFramePending = false;
175
+ if (presentFrame()) {
176
+ scheduleFrame();
177
+ }
178
+ }
179
+
180
+ @Override
181
+ protected void onDetachedFromWindow() {
182
+ super.onDetachedFromWindow();
183
+ Choreographer.getInstance().removeFrameCallback(this);
184
+ mFramePending = false;
185
+ }
186
+
187
+ @Override
188
+ protected void finalize() throws Throwable {
189
+ super.finalize();
190
+ mHybridData.resetNative();
191
+ }
192
+
193
+ // Surfaces ------------------------------------------------------------
194
+
138
195
  // SurfaceView callbacks: the native side receives an android.view.Surface.
139
196
 
140
197
  @Override
@@ -144,7 +201,7 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
144
201
 
145
202
  @Override
146
203
  public void onSurfaceChanged(Surface surface, int width, int height) {
147
- Log.i(tag, "onSurfaceChanged " + width + "/" + height);
204
+ Log.i(TAG, "onSurfaceChanged " + width + "/" + height);
148
205
  surfaceSizeChanged(surface, width, height, true, mAppliedHighBitDepth);
149
206
  }
150
207
 
@@ -158,7 +215,7 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
158
215
 
159
216
  @Override
160
217
  public void onSurfaceTextureChanged(SurfaceTexture surface, int width, int height) {
161
- Log.i(tag, "onSurfaceTextureSizeChanged " + width + "/" + height);
218
+ Log.i(TAG, "onSurfaceTextureSizeChanged " + width + "/" + height);
162
219
  surfaceSizeChanged(surface, width, height, false, false);
163
220
  }
164
221
 
@@ -167,19 +224,24 @@ public abstract class SkiaBaseView extends ReactViewGroup implements SkiaViewAPI
167
224
  surfaceDestroyed();
168
225
  }
169
226
 
227
+ // Native ---------------------------------------------------------------
228
+
229
+ private native HybridData initHybrid(SkiaManager skiaManager);
230
+
170
231
  // isSurface tells the native side whether `surface` is an
171
232
  // android.view.Surface (SurfaceView) or a SurfaceTexture (TextureView).
172
- protected abstract void surfaceAvailable(Object surface, int width, int height, boolean isSurface, boolean highBitDepth);
233
+ private native void surfaceAvailable(Object surface, int width, int height, boolean isSurface, boolean highBitDepth);
173
234
 
174
- protected abstract void surfaceSizeChanged(Object surface, int width, int height, boolean isSurface, boolean highBitDepth);
235
+ private native void surfaceSizeChanged(Object surface, int width, int height, boolean isSurface, boolean highBitDepth);
175
236
 
176
- protected abstract void surfaceDestroyed();
237
+ private native void surfaceDestroyed();
177
238
 
178
- protected abstract void setDebugMode(boolean show);
239
+ private native void setLayoutSize(int width, int height);
179
240
 
180
- protected abstract void registerView(int nativeId);
241
+ native void registerView(int nativeId);
181
242
 
182
- protected abstract void unregisterView();
243
+ private native void unregisterView();
183
244
 
184
- protected abstract int[] getBitmap(int width, int height);
245
+ /** Choreographer tick: returns whether more recordings are waiting. */
246
+ private native boolean presentFrame();
185
247
  }
@@ -0,0 +1,85 @@
1
+ package com.reactnative.skia;
2
+
3
+ import com.facebook.react.uimanager.PointerEvents;
4
+ import com.facebook.react.uimanager.ThemedReactContext;
5
+ import com.facebook.react.uimanager.ViewProps;
6
+ import com.facebook.react.uimanager.annotations.ReactProp;
7
+ import com.facebook.react.viewmanagers.SkiaViewManagerDelegate;
8
+ import com.facebook.react.viewmanagers.SkiaViewManagerInterface;
9
+ import com.facebook.react.views.view.ReactViewGroup;
10
+ import com.facebook.react.views.view.ReactViewManager;
11
+
12
+ import androidx.annotation.NonNull;
13
+ import androidx.annotation.Nullable;
14
+
15
+ public class SkiaViewManager extends ReactViewManager implements SkiaViewManagerInterface<SkiaView> {
16
+
17
+ protected SkiaViewManagerDelegate mDelegate;
18
+
19
+ SkiaViewManager() {
20
+ mDelegate = new SkiaViewManagerDelegate(this);
21
+ }
22
+
23
+ protected SkiaViewManagerDelegate getDelegate() {
24
+ return mDelegate;
25
+ }
26
+
27
+ @NonNull
28
+ @Override
29
+ public String getName() {
30
+ return "SkiaView";
31
+ }
32
+
33
+ @NonNull
34
+ @Override
35
+ public SkiaView createViewInstance(@NonNull ThemedReactContext reactContext) {
36
+ return new SkiaView(reactContext);
37
+ }
38
+
39
+ @Override
40
+ public void setNativeId(@NonNull ReactViewGroup view, @Nullable String nativeId) {
41
+ super.setNativeId(view, nativeId);
42
+ int nativeIdResolved = Integer.parseInt(nativeId);
43
+ ((SkiaView) view).registerView(nativeIdResolved);
44
+ }
45
+
46
+ @ReactProp(name = "opaque")
47
+ public void setOpaque(SkiaView view, boolean value) {
48
+ view.setOpaque(value);
49
+ }
50
+
51
+ @ReactProp(name = "highBitDepth")
52
+ public void setHighBitDepth(SkiaView view, boolean value) {
53
+ view.setHighBitDepth(value);
54
+ }
55
+
56
+ @ReactProp(name = "androidSurfaceType")
57
+ public void setAndroidSurfaceType(SkiaView view, @Nullable String value) {
58
+ view.setSurfaceType(value);
59
+ }
60
+
61
+ @ReactProp(name = "androidZOrderOnTop")
62
+ public void setAndroidZOrderOnTop(SkiaView view, boolean value) {
63
+ view.setZOrderOnTop(value);
64
+ }
65
+
66
+ @ReactProp(name = ViewProps.POINTER_EVENTS)
67
+ public void setPointerEvents(SkiaView view, @Nullable String pointerEventsStr) {
68
+ view.setPointerEvents(PointerEvents.parsePointerEvents(pointerEventsStr));
69
+ }
70
+
71
+ // The backing view depends on several props (opaque, androidSurfaceType,
72
+ // androidZOrderOnTop, highBitDepth), so it is resolved once per transaction
73
+ // rather than in each setter.
74
+ @Override
75
+ protected void onAfterUpdateTransaction(@NonNull ReactViewGroup view) {
76
+ super.onAfterUpdateTransaction(view);
77
+ ((SkiaView) view).updateView();
78
+ }
79
+
80
+ @Override
81
+ public void onDropViewInstance(@NonNull ReactViewGroup view) {
82
+ super.onDropViewInstance(view);
83
+ ((SkiaView) view).dropInstance();
84
+ }
85
+ }
@@ -1,5 +1,3 @@
1
- #ifdef SK_GRAPHITE
2
-
3
1
  #import <CoreGraphics/CoreGraphics.h>
4
2
  #import <Foundation/Foundation.h>
5
3
  #import <QuartzCore/CAMetalLayer.h>
@@ -35,5 +33,3 @@ void applyCAMetalLayerColorSpace(void *nativeSurface,
35
33
  }
36
34
 
37
35
  } // namespace RNSkia
38
-
39
- #endif // SK_GRAPHITE
@@ -42,18 +42,6 @@ public:
42
42
 
43
43
  sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) override;
44
44
 
45
- #if !defined(SK_GRAPHITE)
46
- GrDirectContext *getDirectContext() override;
47
-
48
- sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &textureInfo,
49
- int width, int height,
50
- bool mipMapped) override;
51
-
52
- const TextureInfo getTexture(sk_sp<SkSurface> image) override;
53
-
54
- const TextureInfo getTexture(sk_sp<SkImage> image) override;
55
- #endif
56
-
57
45
  uint64_t makeNativeBuffer(sk_sp<SkImage> image) override;
58
46
 
59
47
  uint64_t makeTestNativeBuffer(int width, int height) override;
@@ -78,8 +66,6 @@ public:
78
66
 
79
67
  private:
80
68
  ViewScreenshotService *_screenshotService;
81
-
82
- SkColorType mtlPixelFormatToSkColorType(MTLPixelFormat pixelFormat);
83
69
  };
84
70
 
85
71
  } // namespace RNSkia
@@ -1,18 +1,13 @@
1
1
  #import "RNSkApplePlatformContext.h"
2
2
 
3
3
  #import <CoreMedia/CMSampleBuffer.h>
4
- #include <Metal/Metal.h>
5
4
  #import <React/RCTUtils.h>
6
5
  #include <algorithm>
7
6
  #include <set>
8
7
  #include <thread>
9
8
  #include <utility>
10
9
 
11
- #if defined(SK_GRAPHITE)
12
10
  #include "RNDawnContext.h"
13
- #else
14
- #include "MetalContext.h"
15
- #endif
16
11
  #include "RNSkAppleVideo.h"
17
12
 
18
13
  #pragma clang diagnostic push
@@ -83,15 +78,12 @@ void RNSkApplePlatformContext::releaseNativeBuffer(uint64_t pointer) {
83
78
  }
84
79
 
85
80
  uint64_t RNSkApplePlatformContext::makeNativeBuffer(sk_sp<SkImage> image) {
86
- #if defined(SK_GRAPHITE)
87
81
  // A Graphite GPU texture can't be read with readPixels(nullptr) (and can't be
88
- // drawn onto a raster surface) — both yield uninitialized/black pixels. Read
89
- // it back to a raster image first. (JsiNativeBuffer calls the Ganesh-only
90
- // SkImage::makeNonTextureImage(), which is a no-op on Graphite.)
82
+ // drawn onto a raster surface): both yield uninitialized/black pixels. Read
83
+ // it back to a raster image first.
91
84
  if (image && image->isTextureBacked()) {
92
85
  image = DawnContext::getInstance().MakeRasterImage(image);
93
86
  }
94
- #endif
95
87
  // 0. If Image is not in BGRA, convert to BGRA as only BGRA is supported.
96
88
  if (image->colorType() != kBGRA_8888_SkColorType) {
97
89
  const SkImageInfo bgraInfo =
@@ -241,67 +233,6 @@ uint64_t RNSkApplePlatformContext::makeTestNativeBuffer(int width, int height) {
241
233
  return reinterpret_cast<uint64_t>(pixelBuffer);
242
234
  }
243
235
 
244
- #if !defined(SK_GRAPHITE)
245
- GrDirectContext *RNSkApplePlatformContext::getDirectContext() {
246
- return MetalContext::getInstance().getDirectContext();
247
- }
248
-
249
- const TextureInfo RNSkApplePlatformContext::getTexture(sk_sp<SkImage> image) {
250
- TextureInfo result;
251
- GrBackendTexture texture;
252
- if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
253
- throw std::runtime_error("Couldn't get backend texture");
254
- }
255
- if (!texture.isValid()) {
256
- throw std::runtime_error("Invalid backend texture");
257
- }
258
- GrMtlTextureInfo textureInfo;
259
- if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
260
- throw std::runtime_error("Couldn't get Metal texture info");
261
- }
262
- result.mtlTexture = textureInfo.fTexture.get();
263
- return result;
264
- }
265
-
266
- const TextureInfo
267
- RNSkApplePlatformContext::getTexture(sk_sp<SkSurface> surface) {
268
- TextureInfo result;
269
- GrBackendTexture texture = SkSurfaces::GetBackendTexture(
270
- surface.get(), SkSurfaces::BackendHandleAccess::kFlushRead);
271
- if (!texture.isValid()) {
272
- throw std::runtime_error("Invalid backend texture");
273
- }
274
- GrMtlTextureInfo textureInfo;
275
- if (!GrBackendTextures::GetMtlTextureInfo(texture, &textureInfo)) {
276
- throw std::runtime_error("Couldn't get Metal texture info");
277
- }
278
- result.mtlTexture = textureInfo.fTexture.get();
279
- return result;
280
- }
281
-
282
- sk_sp<SkImage> RNSkApplePlatformContext::makeImageFromNativeTexture(
283
- const TextureInfo &texInfo, int width, int height, bool mipMapped) {
284
- id<MTLTexture> mtlTexture = (__bridge id<MTLTexture>)(texInfo.mtlTexture);
285
-
286
- SkColorType colorType = mtlPixelFormatToSkColorType(mtlTexture.pixelFormat);
287
- if (colorType == SkColorType::kUnknown_SkColorType) {
288
- throw std::runtime_error("Unsupported pixelFormat");
289
- }
290
-
291
- GrMtlTextureInfo textureInfo;
292
- textureInfo.fTexture.retain((__bridge const void *)mtlTexture);
293
-
294
- GrBackendTexture texture = GrBackendTextures::MakeMtl(
295
- width, height, mipMapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
296
- textureInfo);
297
-
298
- return SkImages::BorrowTextureFrom(getDirectContext(), texture,
299
- kTopLeft_GrSurfaceOrigin, colorType,
300
- kPremul_SkAlphaType, nullptr);
301
- return nullptr;
302
- }
303
- #endif
304
-
305
236
  std::shared_ptr<RNSkVideo>
306
237
  RNSkApplePlatformContext::createVideo(const std::string &url) {
307
238
  return std::make_shared<RNSkAppleVideo>(url, this);
@@ -314,54 +245,13 @@ void RNSkApplePlatformContext::raiseError(const std::exception &err) {
314
245
  sk_sp<SkSurface>
315
246
  RNSkApplePlatformContext::makeOffscreenSurface(int width, int height,
316
247
  bool useP3ColorSpace) {
317
- #if defined(SK_GRAPHITE)
318
248
  return DawnContext::getInstance().MakeOffscreen(width, height,
319
249
  useP3ColorSpace);
320
- #else
321
- return MetalContext::getInstance().MakeOffscreen(width, height,
322
- useP3ColorSpace);
323
- #endif
324
250
  }
325
251
 
326
252
  sk_sp<SkImage>
327
253
  RNSkApplePlatformContext::makeImageFromNativeBuffer(void *buffer) {
328
- #if defined(SK_GRAPHITE)
329
254
  return DawnContext::getInstance().MakeImageFromBuffer(buffer);
330
- #else
331
- return MetalContext::getInstance().MakeImageFromBuffer(buffer);
332
- #endif
333
- }
334
-
335
- SkColorType RNSkApplePlatformContext::mtlPixelFormatToSkColorType(
336
- MTLPixelFormat pixelFormat) {
337
- switch (pixelFormat) {
338
- case MTLPixelFormatRGBA8Unorm:
339
- return kRGBA_8888_SkColorType;
340
- case MTLPixelFormatBGRA8Unorm:
341
- return kBGRA_8888_SkColorType;
342
- case MTLPixelFormatRGB10A2Unorm:
343
- return kRGBA_1010102_SkColorType;
344
- case MTLPixelFormatR8Unorm:
345
- return kGray_8_SkColorType;
346
- case MTLPixelFormatRGBA16Float:
347
- return kRGBA_F16_SkColorType;
348
- case MTLPixelFormatRG8Unorm:
349
- return kR8G8_unorm_SkColorType;
350
- case MTLPixelFormatR16Float:
351
- return kA16_float_SkColorType;
352
- case MTLPixelFormatRG16Float:
353
- return kR16G16_float_SkColorType;
354
- case MTLPixelFormatR16Unorm:
355
- return kA16_unorm_SkColorType;
356
- case MTLPixelFormatRG16Unorm:
357
- return kR16G16_unorm_SkColorType;
358
- case MTLPixelFormatRGBA16Unorm:
359
- return kR16G16B16A16_unorm_SkColorType;
360
- case MTLPixelFormatRGBA8Unorm_sRGB:
361
- return kSRGBA_8888_SkColorType;
362
- default:
363
- return kUnknown_SkColorType;
364
- }
365
255
  }
366
256
 
367
257
  sk_sp<SkFontMgr> RNSkApplePlatformContext::createFontMgr() {
@@ -1,43 +1,39 @@
1
1
  #pragma once
2
2
 
3
+ #import "RNSkCanvasProvider.h"
3
4
  #import "RNSkPlatformContext.h"
4
- #import "RNSkView.h"
5
5
 
6
6
  #import <MetalKit/MetalKit.h>
7
7
  #import <QuartzCore/CAMetalLayer.h>
8
8
 
9
+ #include <memory>
9
10
  #include <mutex>
10
11
  #include <vector>
11
12
 
12
- #pragma clang diagnostic push
13
- #pragma clang diagnostic ignored "-Wdocumentation"
14
-
15
- #import <include/gpu/ganesh/GrDirectContext.h>
16
-
17
- #pragma clang diagnostic pop
18
-
13
+ /**
14
+ * The CAMetalLayer a SkiaView presents into. The layer is configured as a
15
+ * Dawn surface (see DawnWindowContext) when the view gets a size.
16
+ */
19
17
  class RNSkMetalCanvasProvider : public RNSkia::RNSkCanvasProvider {
20
18
  public:
21
- RNSkMetalCanvasProvider(std::function<void()> requestRedraw,
22
- std::shared_ptr<RNSkia::RNSkPlatformContext> context,
23
- bool useP3ColorSpace = true);
19
+ explicit RNSkMetalCanvasProvider(
20
+ std::shared_ptr<RNSkia::RNSkPlatformContext> context);
24
21
 
25
22
  ~RNSkMetalCanvasProvider();
26
23
 
27
24
  int getWidth() override;
28
25
  int getHeight() override;
29
26
 
30
- bool renderToCanvas(const std::function<void(SkCanvas *)> &cb) override;
27
+ bool getTargetInfo(RNSkia::RNSkGraphiteTargetInfo *info) override;
31
28
 
32
- #if defined(SK_GRAPHITE)
33
- bool getGraphiteTargetInfo(RNSkia::RNSkGraphiteTargetInfo *info) override;
29
+ bool getLayoutSize(int *width, int *height) override;
34
30
 
35
31
  bool presentRecordings(
36
32
  const std::vector<skgpu::graphite::Recording *> &recordings) override;
37
- #endif
33
+
34
+ bool presentImage(const sk_sp<SkImage> &image) override;
38
35
 
39
36
  void setSize(int width, int height);
40
- void setUseP3ColorSpace(bool useP3ColorSpace);
41
37
  void setHighBitDepth(bool highBitDepth);
42
38
  CALayer *getLayer();
43
39
 
@@ -48,13 +44,12 @@ private:
48
44
  #pragma clang diagnostic ignored "-Wunguarded-availability-new"
49
45
  CAMetalLayer *_layer;
50
46
  #pragma clang diagnostic pop
51
- bool _useP3ColorSpace = true;
52
47
  bool _highBitDepth = false;
53
- #if defined(SK_GRAPHITE)
54
48
  // A copy of the window's target description, readable from any thread
55
49
  // while the window itself belongs to the main thread.
56
50
  std::mutex _targetInfoMutex;
57
51
  RNSkia::RNSkGraphiteTargetInfo _targetInfo;
58
52
  bool _hasTargetInfo = false;
59
- #endif
53
+ int _layoutWidth = 0;
54
+ int _layoutHeight = 0;
60
55
  };
@@ -1,31 +1,13 @@
1
1
  #import "RNSkMetalCanvasProvider.h"
2
2
 
3
- #import "RNSkLog.h"
4
-
5
- #if defined(SK_GRAPHITE)
6
3
  #import "RNDawnContext.h"
7
- #else
8
- #import "MetalContext.h"
9
- #endif
10
-
11
- #pragma clang diagnostic push
12
- #pragma clang diagnostic ignored "-Wdocumentation"
13
-
14
- #import "include/core/SkCanvas.h"
15
- #import "include/core/SkColorSpace.h"
16
- #import "include/core/SkSurface.h"
17
-
18
- #import <include/gpu/ganesh/GrBackendSurface.h>
19
- #import <include/gpu/ganesh/GrDirectContext.h>
20
- #import <include/gpu/ganesh/SkSurfaceGanesh.h>
4
+ #import "RNSkLog.h"
21
5
 
22
- #pragma clang diagnostic pop
6
+ #include <cmath>
23
7
 
24
8
  RNSkMetalCanvasProvider::RNSkMetalCanvasProvider(
25
- std::function<void()> requestRedraw,
26
- std::shared_ptr<RNSkia::RNSkPlatformContext> context, bool useP3ColorSpace)
27
- : RNSkCanvasProvider(requestRedraw), _context(context),
28
- _useP3ColorSpace(useP3ColorSpace) {
9
+ std::shared_ptr<RNSkia::RNSkPlatformContext> context)
10
+ : _context(std::move(context)) {
29
11
  #pragma clang diagnostic push
30
12
  #pragma clang diagnostic ignored "-Wunguarded-availability-new"
31
13
  _layer = [CAMetalLayer layer];
@@ -61,38 +43,7 @@ static bool appIsBackgrounded() {
61
43
  #endif // !TARGET_OS_OSX
62
44
  }
63
45
 
64
- /**
65
- Render to a canvas
66
- */
67
- bool RNSkMetalCanvasProvider::renderToCanvas(
68
- const std::function<void(SkCanvas *)> &cb) {
69
- if (!_ctx) {
70
- return false;
71
- }
72
-
73
- if ([[NSThread currentThread] isMainThread]) {
74
- if (appIsBackgrounded()) {
75
- // Request a redraw in the next run loop callback
76
- _requestRedraw();
77
- // and don't draw now since it might cause errors in the metal renderer if
78
- // we try to render while in the background. (see above issue)
79
- return false;
80
- }
81
-
82
- auto surface = _ctx->getSurface();
83
- if (!surface) {
84
- return false;
85
- }
86
- auto canvas = surface->getCanvas();
87
- cb(canvas);
88
- _ctx->present();
89
- return true;
90
- }
91
- return false;
92
- };
93
-
94
- #if defined(SK_GRAPHITE)
95
- bool RNSkMetalCanvasProvider::getGraphiteTargetInfo(
46
+ bool RNSkMetalCanvasProvider::getTargetInfo(
96
47
  RNSkia::RNSkGraphiteTargetInfo *info) {
97
48
  std::lock_guard<std::mutex> lock(_targetInfoMutex);
98
49
  if (!_hasTargetInfo) {
@@ -102,49 +53,65 @@ bool RNSkMetalCanvasProvider::getGraphiteTargetInfo(
102
53
  return true;
103
54
  }
104
55
 
56
+ bool RNSkMetalCanvasProvider::getLayoutSize(int *width, int *height) {
57
+ std::lock_guard<std::mutex> lock(_targetInfoMutex);
58
+ if (_layoutWidth <= 0 || _layoutHeight <= 0) {
59
+ return false;
60
+ }
61
+ *width = _layoutWidth;
62
+ *height = _layoutHeight;
63
+ return true;
64
+ }
65
+
105
66
  bool RNSkMetalCanvasProvider::presentRecordings(
106
67
  const std::vector<skgpu::graphite::Recording *> &recordings) {
107
68
  if (!_ctx || ![[NSThread currentThread] isMainThread]) {
108
69
  return false;
109
70
  }
110
71
  if (appIsBackgrounded()) {
111
- _requestRedraw();
72
+ requestRedraw();
112
73
  return false;
113
74
  }
114
75
  return static_cast<RNSkia::DawnWindowContext *>(_ctx.get())
115
76
  ->presentRecordings(recordings);
116
77
  }
117
- #endif
78
+
79
+ bool RNSkMetalCanvasProvider::presentImage(const sk_sp<SkImage> &image) {
80
+ if (!_ctx || ![[NSThread currentThread] isMainThread]) {
81
+ return false;
82
+ }
83
+ if (appIsBackgrounded()) {
84
+ requestRedraw();
85
+ return false;
86
+ }
87
+ return static_cast<RNSkia::DawnWindowContext *>(_ctx.get())
88
+ ->presentImage(image);
89
+ }
118
90
 
119
91
  void RNSkMetalCanvasProvider::setSize(int width, int height) {
120
92
  _layer.frame = CGRectMake(0, 0, width, height);
121
- auto w = width * _context->getPixelDensity();
122
- auto h = height * _context->getPixelDensity();
123
- #if defined(SK_GRAPHITE)
93
+ // The layout is on the pixel grid: round rather than truncate, so that a
94
+ // product like 1169.9999 gives the pixel size the layout means.
95
+ int w = static_cast<int>(std::lround(width * _context->getPixelDensity()));
96
+ int h = static_cast<int>(std::lround(height * _context->getPixelDensity()));
124
97
  _ctx = RNSkia::DawnContext::getInstance().MakeWindow((__bridge void *)_layer,
125
98
  w, h, _highBitDepth);
126
99
  {
127
100
  auto *window = static_cast<RNSkia::DawnWindowContext *>(_ctx.get());
128
101
  std::lock_guard<std::mutex> lock(_targetInfoMutex);
102
+ _layoutWidth = w;
103
+ _layoutHeight = h;
129
104
  _targetInfo.width = window->getWidth();
130
105
  _targetInfo.height = window->getHeight();
131
106
  _targetInfo.colorType = window->getColorType();
132
107
  _targetInfo.textureInfo = window->getTextureInfo();
133
108
  _hasTargetInfo = true;
134
109
  }
135
- #else
136
- _ctx = MetalContext::getInstance().MakeWindow(_layer, w, h, _useP3ColorSpace,
137
- _highBitDepth);
138
- #endif
139
- _requestRedraw();
110
+ requestRedraw();
140
111
  }
141
112
 
142
113
  CALayer *RNSkMetalCanvasProvider::getLayer() { return _layer; }
143
114
 
144
- void RNSkMetalCanvasProvider::setUseP3ColorSpace(bool useP3ColorSpace) {
145
- _useP3ColorSpace = useP3ColorSpace;
146
- }
147
-
148
115
  void RNSkMetalCanvasProvider::setHighBitDepth(bool highBitDepth) {
149
116
  if (_highBitDepth == highBitDepth) {
150
117
  return;