react-native-skia 3.0.0 → 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 (261) hide show
  1. package/Package.swift +14 -13
  2. package/android/CMakeLists.txt +13 -42
  3. package/android/build.gradle +55 -47
  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/RNDawnContext.h +6 -0
  31. package/cpp/rnskia/RNDawnInterop.cpp +0 -4
  32. package/cpp/rnskia/RNDawnUtils.h +0 -1
  33. package/cpp/rnskia/RNDawnWindowContext.cpp +10 -0
  34. package/cpp/rnskia/RNDawnWindowContext.h +4 -0
  35. package/cpp/rnskia/RNSkCanvasProvider.h +221 -0
  36. package/cpp/rnskia/RNSkGraphiteProducer.cpp +36 -14
  37. package/cpp/rnskia/RNSkGraphiteProducer.h +23 -8
  38. package/cpp/rnskia/RNSkGraphiteTarget.h +383 -0
  39. package/cpp/rnskia/RNSkJsiViewApi.h +9 -18
  40. package/cpp/rnskia/RNSkPlatformContext.h +9 -15
  41. package/cpp/rnskia/RNSkView.h +340 -245
  42. package/jestSetup.js +1 -3
  43. package/lib/commonjs/Platform/Platform.web.js +1 -1
  44. package/lib/commonjs/Platform/Platform.web.js.map +1 -1
  45. package/lib/commonjs/index.d.ts +1 -3
  46. package/lib/commonjs/index.js +3 -12
  47. package/lib/commonjs/index.js.map +1 -1
  48. package/lib/commonjs/mock/index.js +0 -1
  49. package/lib/commonjs/mock/index.js.map +1 -1
  50. package/lib/commonjs/renderer/Canvas.d.ts +15 -12
  51. package/lib/commonjs/renderer/Canvas.js +24 -20
  52. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  53. package/lib/commonjs/skia/types/Graphite.d.ts +3 -3
  54. package/lib/commonjs/skia/types/Graphite.js.map +1 -1
  55. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +2 -2
  56. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  57. package/lib/commonjs/skia/types/Skia.d.ts +1 -2
  58. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  59. package/lib/commonjs/skia/types/Surface/SurfaceFactory.d.ts +1 -1
  60. package/lib/commonjs/skia/types/Surface/SurfaceFactory.js.map +1 -1
  61. package/lib/commonjs/specs/NativeSkiaModule.web.d.ts +5 -4
  62. package/lib/commonjs/specs/NativeSkiaModule.web.js +5 -9
  63. package/lib/commonjs/specs/NativeSkiaModule.web.js.map +1 -1
  64. package/lib/{typescript/src/specs/SkiaPictureViewNativeComponent.d.ts → commonjs/specs/SkiaViewNativeComponent.d.ts} +0 -3
  65. package/lib/commonjs/specs/{SkiaPictureViewNativeComponent.js → SkiaViewNativeComponent.js} +2 -2
  66. package/lib/commonjs/specs/SkiaViewNativeComponent.js.map +1 -0
  67. package/lib/commonjs/specs/SkiaViewNativeComponent.web.d.ts +10 -0
  68. package/lib/commonjs/specs/SkiaViewNativeComponent.web.js +27 -0
  69. package/lib/commonjs/specs/SkiaViewNativeComponent.web.js.map +1 -0
  70. package/lib/commonjs/views/SkiaGraphiteView.d.ts +3 -4
  71. package/lib/commonjs/views/SkiaGraphiteView.js +23 -12
  72. package/lib/commonjs/views/SkiaGraphiteView.js.map +1 -1
  73. package/lib/commonjs/views/SkiaPictureView.d.ts +4 -1
  74. package/lib/commonjs/views/SkiaPictureView.js +12 -10
  75. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  76. package/lib/commonjs/views/SkiaView.web.d.ts +12 -0
  77. package/lib/commonjs/views/{SkiaGraphiteView.web.js → SkiaView.web.js} +82 -38
  78. package/lib/commonjs/views/SkiaView.web.js.map +1 -0
  79. package/lib/commonjs/views/SkiaWebRenderer.js +1 -1
  80. package/lib/commonjs/views/SkiaWebRenderer.js.map +1 -1
  81. package/lib/commonjs/views/android.d.ts +6 -0
  82. package/lib/commonjs/views/android.js +17 -0
  83. package/lib/commonjs/views/android.js.map +1 -0
  84. package/lib/commonjs/views/types.d.ts +3 -12
  85. package/lib/commonjs/views/types.js.map +1 -1
  86. package/lib/module/Platform/Platform.web.js +1 -1
  87. package/lib/module/Platform/Platform.web.js.map +1 -1
  88. package/lib/module/index.d.ts +1 -3
  89. package/lib/module/index.js +1 -3
  90. package/lib/module/index.js.map +1 -1
  91. package/lib/module/mock/index.js +0 -1
  92. package/lib/module/mock/index.js.map +1 -1
  93. package/lib/module/renderer/Canvas.d.ts +15 -12
  94. package/lib/module/renderer/Canvas.js +24 -19
  95. package/lib/module/renderer/Canvas.js.map +1 -1
  96. package/lib/module/skia/types/Graphite.d.ts +3 -3
  97. package/lib/module/skia/types/Graphite.js.map +1 -1
  98. package/lib/module/skia/types/Image/ImageFactory.d.ts +2 -2
  99. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  100. package/lib/module/skia/types/Skia.d.ts +1 -2
  101. package/lib/module/skia/types/Skia.js.map +1 -1
  102. package/lib/module/skia/types/Surface/SurfaceFactory.d.ts +1 -1
  103. package/lib/module/skia/types/Surface/SurfaceFactory.js.map +1 -1
  104. package/lib/module/specs/NativeSkiaModule.web.d.ts +5 -4
  105. package/lib/module/specs/NativeSkiaModule.web.js +5 -9
  106. package/lib/module/specs/NativeSkiaModule.web.js.map +1 -1
  107. package/lib/{commonjs/specs/SkiaPictureViewNativeComponent.d.ts → module/specs/SkiaViewNativeComponent.d.ts} +0 -3
  108. package/lib/module/specs/{SkiaPictureViewNativeComponent.js → SkiaViewNativeComponent.js} +2 -2
  109. package/lib/module/specs/SkiaViewNativeComponent.js.map +1 -0
  110. package/lib/module/specs/SkiaViewNativeComponent.web.d.ts +10 -0
  111. package/lib/module/specs/SkiaViewNativeComponent.web.js +21 -0
  112. package/lib/module/specs/SkiaViewNativeComponent.web.js.map +1 -0
  113. package/lib/module/views/SkiaGraphiteView.d.ts +3 -4
  114. package/lib/module/views/SkiaGraphiteView.js +23 -12
  115. package/lib/module/views/SkiaGraphiteView.js.map +1 -1
  116. package/lib/module/views/SkiaPictureView.d.ts +4 -1
  117. package/lib/module/views/SkiaPictureView.js +12 -10
  118. package/lib/module/views/SkiaPictureView.js.map +1 -1
  119. package/lib/module/views/SkiaView.web.d.ts +12 -0
  120. package/lib/module/views/{SkiaGraphiteView.web.js → SkiaView.web.js} +80 -36
  121. package/lib/module/views/SkiaView.web.js.map +1 -0
  122. package/lib/module/views/SkiaWebRenderer.js +1 -1
  123. package/lib/module/views/SkiaWebRenderer.js.map +1 -1
  124. package/lib/module/views/android.d.ts +6 -0
  125. package/lib/module/views/android.js +10 -0
  126. package/lib/module/views/android.js.map +1 -0
  127. package/lib/module/views/types.d.ts +3 -12
  128. package/lib/module/views/types.js.map +1 -1
  129. package/lib/typescript/src/index.d.ts +1 -3
  130. package/lib/typescript/src/renderer/Canvas.d.ts +15 -12
  131. package/lib/typescript/src/skia/types/Graphite.d.ts +3 -3
  132. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +2 -2
  133. package/lib/typescript/src/skia/types/Skia.d.ts +1 -2
  134. package/lib/typescript/src/skia/types/Surface/SurfaceFactory.d.ts +1 -1
  135. package/lib/typescript/src/specs/NativeSkiaModule.web.d.ts +5 -4
  136. package/lib/{module/specs/SkiaPictureViewNativeComponent.d.ts → typescript/src/specs/SkiaViewNativeComponent.d.ts} +0 -3
  137. package/lib/typescript/src/specs/SkiaViewNativeComponent.web.d.ts +10 -0
  138. package/lib/typescript/src/views/SkiaGraphiteView.d.ts +3 -4
  139. package/lib/typescript/src/views/SkiaPictureView.d.ts +4 -1
  140. package/lib/typescript/src/views/SkiaView.web.d.ts +12 -0
  141. package/lib/typescript/src/views/android.d.ts +6 -0
  142. package/lib/typescript/src/views/types.d.ts +3 -12
  143. package/package.json +8 -16
  144. package/react-native-skia.podspec +50 -57
  145. package/src/Platform/Platform.web.tsx +1 -1
  146. package/src/index.ts +1 -3
  147. package/src/mock/index.ts +0 -1
  148. package/src/renderer/Canvas.tsx +23 -27
  149. package/src/skia/types/Graphite.ts +3 -3
  150. package/src/skia/types/Image/ImageFactory.ts +2 -2
  151. package/src/skia/types/Skia.ts +1 -2
  152. package/src/skia/types/Surface/SurfaceFactory.ts +1 -1
  153. package/src/specs/NativeSkiaModule.web.ts +9 -21
  154. package/src/specs/{SkiaPictureViewNativeComponent.ts → SkiaViewNativeComponent.ts} +1 -4
  155. package/src/specs/{SkiaPictureViewNativeComponent.web.ts → SkiaViewNativeComponent.web.ts} +8 -10
  156. package/src/views/SkiaGraphiteView.tsx +27 -16
  157. package/src/views/SkiaPictureView.tsx +11 -10
  158. package/src/views/{SkiaGraphiteView.web.tsx → SkiaView.web.tsx} +100 -53
  159. package/src/views/SkiaWebRenderer.ts +1 -1
  160. package/src/views/android.ts +16 -0
  161. package/src/views/types.ts +3 -14
  162. package/android/cpp/jni/include/JniSkiaBaseView.h +0 -92
  163. package/android/cpp/jni/include/JniSkiaGraphiteView.h +0 -176
  164. package/android/cpp/jni/include/JniSkiaPictureView.h +0 -172
  165. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.cpp +0 -245
  166. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.h +0 -33
  167. package/android/cpp/rnskia-android/OpenGLContext.h +0 -248
  168. package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +0 -67
  169. package/android/cpp/rnskia-android/OpenGLWindowContext.h +0 -75
  170. package/android/cpp/rnskia-android/RNSkAndroidView.h +0 -67
  171. package/android/cpp/rnskia-android/gl/Context.h +0 -80
  172. package/android/cpp/rnskia-android/gl/Display.h +0 -123
  173. package/android/cpp/rnskia-android/gl/Error.cpp +0 -45
  174. package/android/cpp/rnskia-android/gl/Error.h +0 -8
  175. package/android/cpp/rnskia-android/gl/Surface.h +0 -43
  176. package/android/src/main/java/com/reactnative/skia/SkiaBaseViewManager.java +0 -65
  177. package/android/src/main/java/com/reactnative/skia/SkiaGraphiteView.java +0 -78
  178. package/android/src/main/java/com/reactnative/skia/SkiaGraphiteViewManager.java +0 -32
  179. package/android/src/main/java/com/reactnative/skia/SkiaPictureView.java +0 -105
  180. package/android/src/main/java/com/reactnative/skia/SkiaPictureViewManager.java +0 -44
  181. package/apple/MetalContext.h +0 -116
  182. package/apple/MetalContext.mm +0 -38
  183. package/apple/MetalWindowContext.h +0 -42
  184. package/apple/MetalWindowContext.mm +0 -95
  185. package/apple/RNSkAppleView.h +0 -49
  186. package/apple/RNSkAppleView.mm +0 -35
  187. package/apple/SkiaCVPixelBufferUtils.h +0 -122
  188. package/apple/SkiaCVPixelBufferUtils.mm +0 -535
  189. package/apple/SkiaGraphiteView.h +0 -26
  190. package/apple/SkiaGraphiteView.mm +0 -180
  191. package/apple/SkiaPictureView.h +0 -7
  192. package/apple/SkiaPictureView.mm +0 -70
  193. package/apple/SkiaUIView.h +0 -31
  194. package/apple/SkiaUIView.mm +0 -164
  195. package/cpp/rnskia/RNSkGraphiteView.h +0 -708
  196. package/cpp/rnskia/RNSkPictureView.cpp +0 -69
  197. package/cpp/rnskia/RNSkPictureView.h +0 -211
  198. package/cpp/skia/src/gpu/ganesh/gl/GrGLDefines.h +0 -1162
  199. package/lib/commonjs/renderer/DefaultCanvas.d.ts +0 -8
  200. package/lib/commonjs/renderer/DefaultCanvas.js +0 -42
  201. package/lib/commonjs/renderer/DefaultCanvas.js.map +0 -1
  202. package/lib/commonjs/renderer/GraphiteCanvas.d.ts +0 -22
  203. package/lib/commonjs/renderer/GraphiteCanvas.js +0 -64
  204. package/lib/commonjs/renderer/GraphiteCanvas.js.map +0 -1
  205. package/lib/commonjs/renderer/GraphiteCanvas.web.d.ts +0 -1
  206. package/lib/commonjs/renderer/GraphiteCanvas.web.js +0 -13
  207. package/lib/commonjs/renderer/GraphiteCanvas.web.js.map +0 -1
  208. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.d.ts +0 -10
  209. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.js +0 -12
  210. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.js.map +0 -1
  211. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.web.d.ts +0 -9
  212. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.web.js +0 -28
  213. package/lib/commonjs/specs/SkiaGraphiteViewNativeComponent.web.js.map +0 -1
  214. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +0 -1
  215. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +0 -10
  216. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js +0 -29
  217. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js.map +0 -1
  218. package/lib/commonjs/views/SkiaGraphiteView.web.d.ts +0 -14
  219. package/lib/commonjs/views/SkiaGraphiteView.web.js.map +0 -1
  220. package/lib/commonjs/views/SkiaPictureView.web.d.ts +0 -11
  221. package/lib/commonjs/views/SkiaPictureView.web.js +0 -165
  222. package/lib/commonjs/views/SkiaPictureView.web.js.map +0 -1
  223. package/lib/module/renderer/DefaultCanvas.d.ts +0 -8
  224. package/lib/module/renderer/DefaultCanvas.js +0 -34
  225. package/lib/module/renderer/DefaultCanvas.js.map +0 -1
  226. package/lib/module/renderer/GraphiteCanvas.d.ts +0 -22
  227. package/lib/module/renderer/GraphiteCanvas.js +0 -57
  228. package/lib/module/renderer/GraphiteCanvas.js.map +0 -1
  229. package/lib/module/renderer/GraphiteCanvas.web.d.ts +0 -1
  230. package/lib/module/renderer/GraphiteCanvas.web.js +0 -4
  231. package/lib/module/renderer/GraphiteCanvas.web.js.map +0 -1
  232. package/lib/module/specs/SkiaGraphiteViewNativeComponent.d.ts +0 -10
  233. package/lib/module/specs/SkiaGraphiteViewNativeComponent.js +0 -8
  234. package/lib/module/specs/SkiaGraphiteViewNativeComponent.js.map +0 -1
  235. package/lib/module/specs/SkiaGraphiteViewNativeComponent.web.d.ts +0 -9
  236. package/lib/module/specs/SkiaGraphiteViewNativeComponent.web.js +0 -22
  237. package/lib/module/specs/SkiaGraphiteViewNativeComponent.web.js.map +0 -1
  238. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +0 -1
  239. package/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +0 -10
  240. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js +0 -23
  241. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js.map +0 -1
  242. package/lib/module/views/SkiaGraphiteView.web.d.ts +0 -14
  243. package/lib/module/views/SkiaGraphiteView.web.js.map +0 -1
  244. package/lib/module/views/SkiaPictureView.web.d.ts +0 -11
  245. package/lib/module/views/SkiaPictureView.web.js +0 -156
  246. package/lib/module/views/SkiaPictureView.web.js.map +0 -1
  247. package/lib/typescript/src/renderer/DefaultCanvas.d.ts +0 -8
  248. package/lib/typescript/src/renderer/GraphiteCanvas.d.ts +0 -22
  249. package/lib/typescript/src/renderer/GraphiteCanvas.web.d.ts +0 -1
  250. package/lib/typescript/src/specs/SkiaGraphiteViewNativeComponent.d.ts +0 -10
  251. package/lib/typescript/src/specs/SkiaGraphiteViewNativeComponent.web.d.ts +0 -9
  252. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.web.d.ts +0 -10
  253. package/lib/typescript/src/views/SkiaGraphiteView.web.d.ts +0 -14
  254. package/lib/typescript/src/views/SkiaPictureView.web.d.ts +0 -11
  255. package/libs/.graphite +0 -1
  256. package/src/renderer/DefaultCanvas.tsx +0 -41
  257. package/src/renderer/GraphiteCanvas.tsx +0 -61
  258. package/src/renderer/GraphiteCanvas.web.tsx +0 -3
  259. package/src/specs/SkiaGraphiteViewNativeComponent.ts +0 -17
  260. package/src/specs/SkiaGraphiteViewNativeComponent.web.ts +0 -31
  261. package/src/views/SkiaPictureView.web.tsx +0 -224
@@ -1,31 +1,29 @@
1
-
2
1
  #pragma once
3
2
 
3
+ #include <atomic>
4
+ #include <functional>
4
5
  #include <memory>
6
+ #include <mutex>
5
7
  #include <string>
6
8
  #include <unordered_map>
9
+ #include <utility>
7
10
  #include <vector>
8
11
 
12
+ #include <jsi/jsi.h>
13
+
14
+ #include "RNSkCanvasProvider.h"
15
+ #include "RNSkGraphiteProducer.h"
16
+ #include "RNSkGraphiteTarget.h"
9
17
  #include "RNSkPlatformContext.h"
10
18
  #include "jsi/ViewProperty.h"
11
-
12
- #include "api/JsiSkImage.h"
13
- #include "api/JsiSkPoint.h"
14
- #include "api/JsiSkRect.h"
19
+ #include "utils/RNSkLog.h"
15
20
 
16
21
  #pragma clang diagnostic push
17
22
  #pragma clang diagnostic ignored "-Wdocumentation"
18
23
 
19
24
  #include "include/core/SkCanvas.h"
20
- #include "include/core/SkSurface.h"
21
-
22
- #if defined(SK_GRAPHITE)
23
- #include "RNDawnContext.h"
24
- #include "RNDawnUtils.h"
25
- #include "include/gpu/graphite/Recording.h"
26
- #include "include/gpu/graphite/TextureInfo.h"
27
- #include "include/gpu/graphite/dawn/DawnGraphiteTypes.h"
28
- #endif
25
+ #include "include/core/SkImage.h"
26
+ #include "include/core/SkRect.h"
29
27
 
30
28
  #pragma clang diagnostic pop
31
29
 
@@ -33,211 +31,108 @@ namespace RNSkia {
33
31
 
34
32
  namespace jsi = facebook::jsi;
35
33
 
36
- #if defined(SK_GRAPHITE)
37
34
  /**
38
- * Describes the texture a Graphite recording is replayed onto: the pixel size
39
- * and the format of a window (or offscreen) surface. A deferred canvas is
40
- * recorded against this description, so it can be created before the surface
41
- * itself exists.
35
+ * The native side of a Skia view. Frames come from one of two producers: JS
36
+ * records them itself through the view's target
37
+ * (SkiaViewApi.makeGraphiteContext), or <Canvas> hands over a recorder (or a
38
+ * picture) that the render thread pool records for the view. Either way the
39
+ * recordings are queued on the target; the view presents them onto its canvas
40
+ * provider on the main thread, aligned with the platform's display link.
41
+ *
42
+ * Threading: the registry side (RNSkJsiViewApi) runs on the JS thread, except
43
+ * applyUpdates() which a Reanimated mapper calls on the UI runtime; snapshots
44
+ * replay on the calling thread. Presenting runs on the main thread.
42
45
  */
43
- struct RNSkGraphiteTargetInfo {
44
- int width = 0;
45
- int height = 0;
46
- SkColorType colorType = kUnknown_SkColorType;
47
- skgpu::graphite::TextureInfo textureInfo;
48
- };
49
- #endif
50
-
51
- class RNSkCanvasProvider {
52
- public:
53
- explicit RNSkCanvasProvider(std::function<void()> requestRedraw)
54
- : _requestRedraw(requestRedraw) {}
55
-
56
- virtual ~RNSkCanvasProvider() = default;
57
-
58
- #if defined(SK_GRAPHITE)
59
- /**
60
- Describes the current target texture. Safe to call from any thread;
61
- returns false while there is no surface to describe.
62
- */
63
- virtual bool getGraphiteTargetInfo(RNSkGraphiteTargetInfo *info) {
64
- return false;
65
- }
66
-
67
- /**
68
- Replays the recordings, in order, onto the target texture and presents
69
- it. Called on the thread that owns the surface.
70
- */
71
- virtual bool presentRecordings(
72
- const std::vector<skgpu::graphite::Recording *> &recordings) {
73
- return false;
74
- }
75
- #endif
76
-
77
- /**
78
- Returns the scaled width of the view
79
- */
80
- virtual int getWidth() = 0;
81
-
82
- /**
83
- Returns the scaled height of the view
84
- */
85
- virtual int getHeight() = 0;
86
-
87
- /**
88
- Render to a canvas
89
- */
90
- virtual bool renderToCanvas(const std::function<void(SkCanvas *)> &) = 0;
91
-
92
- protected:
93
- std::function<void()> _requestRedraw;
94
- };
95
-
96
- class RNSkRenderer {
97
- public:
98
- explicit RNSkRenderer(std::function<void()> requestRedraw)
99
- : _requestRedraw(std::move(requestRedraw)), _showDebugOverlays(false) {}
100
-
101
- virtual void
102
- renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
103
-
104
- void setShowDebugOverlays(bool showDebugOverlays) {
105
- _showDebugOverlays = showDebugOverlays;
106
- }
107
- bool getShowDebugOverlays() const { return _showDebugOverlays; }
108
-
109
- protected:
110
- std::function<void()> _requestRedraw;
111
- bool _showDebugOverlays;
112
- };
113
-
114
- class RNSkOffscreenCanvasProvider : public RNSkCanvasProvider {
46
+ class RNSkView : public std::enable_shared_from_this<RNSkView> {
115
47
  public:
116
- RNSkOffscreenCanvasProvider(
117
- const std::shared_ptr<RNSkPlatformContext> &context,
118
- std::function<void()> requestRedraw, float width, float height)
119
- : RNSkCanvasProvider(std::move(requestRedraw)), _width(width),
120
- _height(height), _context(context) {
121
- _surface = context->makeOffscreenSurface(_width, _height);
122
- _pd = context->getPixelDensity();
48
+ RNSkView(std::shared_ptr<RNSkPlatformContext> context,
49
+ std::shared_ptr<RNSkCanvasProvider> canvasProvider)
50
+ : _platformContext(std::move(context)),
51
+ _canvasProvider(std::move(canvasProvider)),
52
+ _producer(std::make_shared<RNSkGraphiteProducer>()) {
53
+ // A new surface or a new size: present again. The platform view owns
54
+ // the provider together with this view, and the destructor clears the
55
+ // callback, so a raw capture is safe.
56
+ _canvasProvider->setRequestRedraw([this]() { requestRedraw(); });
123
57
  }
124
58
 
125
- virtual ~RNSkOffscreenCanvasProvider() = default;
126
-
127
- /**
128
- Returns a snapshot of the current surface/canvas
129
- */
130
- sk_sp<SkImage> makeSnapshot(SkRect *bounds) {
131
- if (_surface == nullptr) {
132
- return nullptr;
59
+ ~RNSkView() {
60
+ _canvasProvider->setRequestRedraw(nullptr);
61
+ std::shared_ptr<RNSkGraphiteTarget> target;
62
+ {
63
+ std::lock_guard<std::mutex> lock(_mutex);
64
+ target = std::move(_target);
133
65
  }
134
- sk_sp<SkImage> image;
135
- if (bounds != nullptr) {
136
- SkIRect b =
137
- SkIRect::MakeXYWH(bounds->x() * _pd, bounds->y() * _pd,
138
- bounds->width() * _pd, bounds->height() * _pd);
139
- image = _surface->makeImageSnapshot(b);
140
- } else {
141
- image = _surface->makeImageSnapshot();
66
+ if (target) {
67
+ target->detach(_canvasProvider);
142
68
  }
143
- #if defined(SK_GRAPHITE)
144
- // Only Graphite-backed surfaces have a recorder to snap/submit; a raster
145
- // surface's snapshot is already a valid CPU image.
146
- if (auto *recorder = _surface->recorder()) {
147
- DawnContext::getInstance().submitRecording(recorder->snap().get());
148
- }
149
- return DawnContext::getInstance().MakeRasterImage(image);
150
- #else
151
- auto grContext = _context->getDirectContext();
152
- return image->makeRasterImage(grContext);
153
- #endif
154
69
  }
155
70
 
156
- /**
157
- Returns the scaled width of the view
158
- */
159
- int getWidth() override { return _width; };
160
-
161
- /**
162
- Returns the scaled height of the view
163
- */
164
- int getHeight() override { return _height; };
71
+ // Registry side ------------------------------------------------------------
165
72
 
166
73
  /**
167
- Render to a canvas
74
+ Binds the view to its id. The target recording for this id (created by
75
+ whichever side came first, see RNSkGraphiteTargetRegistry) is attached to
76
+ the view's surface, and frames recorded before the view existed are
77
+ presented now.
168
78
  */
169
- bool renderToCanvas(const std::function<void(SkCanvas *)> &cb) override {
170
- if (_surface == nullptr) {
171
- return false;
79
+ void setNativeId(size_t nativeId) {
80
+ _nativeId = nativeId;
81
+ std::shared_ptr<RNSkGraphiteTarget> previous;
82
+ {
83
+ std::lock_guard<std::mutex> lock(_mutex);
84
+ if (_target && _targetId == nativeId) {
85
+ return;
86
+ }
87
+ previous = std::move(_target);
88
+ _targetId = nativeId;
89
+ _target = RNSkGraphiteTargetRegistry::getInstance().getOrCreate(
90
+ nativeId, _platformContext);
172
91
  }
173
- cb(_surface->getCanvas());
174
- return true;
175
- };
176
-
177
- #if defined(SK_GRAPHITE)
178
- bool getGraphiteTargetInfo(RNSkGraphiteTargetInfo *info) override {
179
- if (_surface == nullptr || _surface->recorder() == nullptr) {
180
- return false;
92
+ if (previous) {
93
+ previous->detach(_canvasProvider);
181
94
  }
182
- // Graphite gives its render targets the sampled+renderable usage, a
183
- // superset of what a deferred canvas asks for.
184
- auto colorType = _surface->imageInfo().colorType();
185
- info->width = _width;
186
- info->height = _height;
187
- info->colorType = colorType;
188
- info->textureInfo = skgpu::graphite::TextureInfos::MakeDawn(
189
- skgpu::graphite::DawnTextureInfo(
190
- skgpu::graphite::SampleCount::k1, skgpu::Mipmapped::kNo,
191
- DawnUtils::textureFormatForColorType(colorType),
192
- DawnUtils::DefaultTargetUsage, wgpu::TextureAspect::All));
193
- return true;
194
- }
195
-
196
- bool presentRecordings(
197
- const std::vector<skgpu::graphite::Recording *> &recordings) override {
198
- if (_surface == nullptr || _surface->recorder() == nullptr) {
199
- return false;
95
+ std::weak_ptr<RNSkView> weakThis = weak_from_this();
96
+ auto context = _platformContext;
97
+ auto scheduleOnMainThread = [weakThis, context]() {
98
+ context->runOnMainThread([weakThis]() {
99
+ if (auto view = weakThis.lock()) {
100
+ view->scheduleFrame();
101
+ }
102
+ });
103
+ };
104
+ auto target = getTarget();
105
+ target->attach(_canvasProvider, scheduleOnMainThread);
106
+ _producer->setTarget(target);
107
+ if (target->hasQueued()) {
108
+ scheduleOnMainThread();
200
109
  }
201
- return DawnContext::getInstance().insertRecordings(recordings,
202
- _surface.get());
203
110
  }
204
- #endif
205
-
206
- private:
207
- int _width;
208
- int _height;
209
- float _pd = 1.0f;
210
- sk_sp<SkSurface> _surface;
211
- std::shared_ptr<RNSkPlatformContext> _context;
212
- };
213
-
214
- class RNSkView : public std::enable_shared_from_this<RNSkView> {
215
- public:
216
- /**
217
- * Constructor
218
- */
219
- RNSkView(std::shared_ptr<RNSkPlatformContext> context,
220
- std::shared_ptr<RNSkCanvasProvider> canvasProvider,
221
- std::shared_ptr<RNSkRenderer> renderer)
222
- : _platformContext(context), _canvasProvider(canvasProvider),
223
- _renderer(renderer) {}
224
111
 
225
- /**
226
- Destructor
227
- */
228
- virtual ~RNSkView() {}
112
+ size_t getNativeId() { return _nativeId; }
229
113
 
230
- virtual void setJsiProperties(
231
- std::unordered_map<std::string, RNJsi::ViewProperty> &props) = 0;
114
+ /** Declarative content: the recorder of <Canvas>, or a picture. */
115
+ void setJsiProperties(
116
+ std::unordered_map<std::string, RNJsi::ViewProperty> &props) {
117
+ for (auto &prop : props) {
118
+ if (prop.first == "recorder") {
119
+ _producer->setRecorder(
120
+ prop.second.isRecorder() ? prop.second.getRecorder() : nullptr);
121
+ } else if (prop.first == "picture") {
122
+ _producer->setPicture(prop.second.isPicture() ? prop.second.getPicture()
123
+ : nullptr);
124
+ }
125
+ }
126
+ }
232
127
 
233
128
  /**
234
129
  Reads the shared values on the calling runtime into the recording the
235
130
  view owns and schedules a frame. Returns false when there is nothing to
236
131
  update: no recording, or one other than recorderId (a stale mapper).
237
132
  */
238
- virtual bool applyUpdates(jsi::Runtime &runtime, double recorderId,
239
- const jsi::Array &values) {
240
- return false;
133
+ bool applyUpdates(jsi::Runtime &runtime, double recorderId,
134
+ const jsi::Array &values) {
135
+ return _producer->applyUpdates(runtime, recorderId, values);
241
136
  }
242
137
 
243
138
  /**
@@ -246,91 +141,291 @@ public:
246
141
  until it is finalized, so the resources go away here rather than with the
247
142
  garbage collector.
248
143
  */
249
- virtual void releaseContent() {}
144
+ void releaseContent() { _producer->clear(); }
250
145
 
146
+ /** Schedules redraw() on the main thread, once. */
251
147
  void requestRedraw() {
252
- if (!_redrawRequested) {
253
- _redrawRequested = true;
254
- // Capture a weak pointer to this
255
- auto weakThis = std::weak_ptr<RNSkView>(shared_from_this());
256
-
257
- _platformContext->runOnMainThread([weakThis]() {
258
- // Try to lock the weak pointer
259
- if (auto strongThis = weakThis.lock()) {
260
- // Only proceed if the object still exists
261
- if (strongThis->_renderer && strongThis->_redrawRequested) {
262
- strongThis->_renderer->renderImmediate(strongThis->_canvasProvider);
263
- strongThis->_redrawRequested = false;
264
- }
265
- }
266
- });
148
+ if (_redrawRequested.exchange(true)) {
149
+ return;
267
150
  }
151
+ std::weak_ptr<RNSkView> weakThis = weak_from_this();
152
+ _platformContext->runOnMainThread([weakThis]() {
153
+ if (auto view = weakThis.lock()) {
154
+ if (view->_redrawRequested) {
155
+ view->redraw();
156
+ }
157
+ }
158
+ });
268
159
  }
269
160
 
161
+ /**
162
+ Main thread. Presents everything submitted since the last frame; with
163
+ nothing queued, presents the last frame again (a redraw after a resize or
164
+ on a new surface). Without a surface the queue is left alone: the surface
165
+ presents it when it appears.
166
+ */
270
167
  void redraw() {
271
- _renderer->renderImmediate(_canvasProvider);
272
168
  _redrawRequested = false;
169
+ RNSkGraphiteTargetInfo targetInfo;
170
+ if (!_canvasProvider->getTargetInfo(&targetInfo)) {
171
+ return;
172
+ }
173
+ // Declarative content is recorded again for the surface as it is now.
174
+ const bool frameComing = _producer->requestFrame();
175
+ auto target = getTarget();
176
+ std::shared_ptr<RNSkGraphiteRecording> lastPresented;
177
+ {
178
+ std::lock_guard<std::mutex> lock(_mutex);
179
+ lastPresented = _lastPresented;
180
+ }
181
+ std::vector<std::shared_ptr<RNSkGraphiteRecording>> recordings;
182
+ if (target) {
183
+ recordings = target->takeQueued();
184
+ }
185
+ if (recordings.empty()) {
186
+ // With a frame on its way, the layer keeps showing the last one until
187
+ // it lands: presenting it again would only cost a second present.
188
+ // Otherwise the last frame is presented again, also onto a surface of
189
+ // another size (the view was resized and nothing records for it): the
190
+ // frame then shows at its own size rather than nothing at all.
191
+ if (frameComing || lastPresented == nullptr) {
192
+ return;
193
+ }
194
+ if (present(_canvasProvider, targetInfo, {lastPresented},
195
+ /* remember= */ true)) {
196
+ _producer->onFramePresented();
197
+ }
198
+ return;
199
+ }
200
+ if (present(_canvasProvider, targetInfo, recordings,
201
+ /* remember= */ true)) {
202
+ _producer->onFramePresented();
203
+ } else if (target) {
204
+ target->requeue(recordings);
205
+ }
273
206
  }
274
207
 
275
208
  /**
276
- Sets the native id of the view
209
+ Renders the view into an offscreen surface: declarative content is
210
+ replayed on the calling thread with the latest values, so the snapshot
211
+ does not wait for a frame; otherwise the current frame is replayed.
277
212
  */
278
- virtual void setNativeId(size_t nativeId) { _nativeId = nativeId; }
213
+ sk_sp<SkImage> makeImageSnapshot(SkRect *bounds) {
214
+ auto provider = std::make_shared<RNSkOffscreenCanvasProvider>(
215
+ _platformContext, getScaledWidth(), getScaledHeight());
216
+ if (_producer->hasContent()) {
217
+ if (auto *canvas = provider->getCanvas()) {
218
+ _producer->renderInto(canvas, _platformContext->getPixelDensity());
219
+ }
220
+ } else {
221
+ renderLastFrame(provider);
222
+ }
223
+ return provider->makeSnapshot(bounds);
224
+ }
279
225
 
280
- /**
281
- Returns the native id
282
- */
283
- size_t getNativeId() { return _nativeId; }
226
+ /** Width of the surface, in pixels. */
227
+ int getScaledWidth() { return _canvasProvider->getWidth(); }
228
+
229
+ /** Height of the surface, in pixels. */
230
+ int getScaledHeight() { return _canvasProvider->getHeight(); }
231
+
232
+ // Platform view side -------------------------------------------------------
284
233
 
285
234
  /**
286
- * Set to true to show the debug overlays on render
235
+ Installed by the platform view: arms its display link. Main thread. Without
236
+ one, frames are presented as soon as the main thread gets to them.
287
237
  */
288
- void setShowDebugOverlays(bool show) {
289
- _renderer->setShowDebugOverlays(show);
290
- requestRedraw();
238
+ void setFrameScheduler(std::function<void()> scheduler) {
239
+ _frameScheduler = std::move(scheduler);
240
+ }
241
+
242
+ /** Main thread: a recording is waiting. */
243
+ void scheduleFrame() {
244
+ if (_frameScheduler) {
245
+ _frameScheduler();
246
+ } else {
247
+ redraw();
248
+ }
291
249
  }
292
250
 
293
251
  /**
294
- Renders the view into an SkImage instead of the screen.
252
+ Display link tick: presents the queued recordings. Returns whether more
253
+ are waiting, so that the caller keeps its display link armed: also when the
254
+ present failed (the app is in the background), so that the recordings are
255
+ tried again on the next frame rather than left waiting for a redraw.
256
+ Without a surface the queue is left alone: the surface presents it when
257
+ it appears.
295
258
  */
296
- virtual sk_sp<SkImage> makeImageSnapshot(SkRect *bounds) {
259
+ bool presentFrame() {
260
+ auto target = getTarget();
261
+ if (target == nullptr) {
262
+ return false;
263
+ }
264
+ RNSkGraphiteTargetInfo targetInfo;
265
+ if (!_canvasProvider->getTargetInfo(&targetInfo)) {
266
+ return false;
267
+ }
268
+ auto recordings = target->takeQueued();
269
+ if (recordings.empty()) {
270
+ return false;
271
+ }
272
+ if (!present(_canvasProvider, targetInfo, recordings,
273
+ /* remember= */ true)) {
274
+ target->requeue(recordings);
275
+ return true;
276
+ }
277
+ _producer->onFramePresented();
278
+ return target->hasQueued();
279
+ }
297
280
 
298
- auto provider = std::make_shared<RNSkOffscreenCanvasProvider>(
299
- getPlatformContext(), std::bind(&RNSkView::requestRedraw, this),
300
- _canvasProvider->getWidth(), _canvasProvider->getHeight());
281
+ bool hasQueuedRecordings() {
282
+ auto target = getTarget();
283
+ return target && target->hasQueued();
284
+ }
301
285
 
302
- _renderer->renderImmediate(provider);
303
- return provider->makeSnapshot(bounds);
286
+ std::shared_ptr<RNSkCanvasProvider> getCanvasProvider() {
287
+ return _canvasProvider;
304
288
  }
305
289
 
306
- std::shared_ptr<RNSkRenderer> getRenderer() { return _renderer; }
290
+ std::shared_ptr<RNSkPlatformContext> getPlatformContext() {
291
+ return _platformContext;
292
+ }
293
+
294
+ private:
295
+ std::shared_ptr<RNSkGraphiteTarget> getTarget() {
296
+ std::lock_guard<std::mutex> lock(_mutex);
297
+ return _target;
298
+ }
307
299
 
308
300
  /**
309
- Returns the scaled width of the view
301
+ Replays the frame on screen, or the one about to be, onto another
302
+ provider (a snapshot surface). Any thread.
310
303
  */
311
- int getScaledWidth() { return _canvasProvider->getWidth(); }
304
+ bool renderLastFrame(const std::shared_ptr<RNSkCanvasProvider> &provider) {
305
+ std::shared_ptr<RNSkGraphiteTarget> target;
306
+ std::shared_ptr<RNSkGraphiteRecording> recording;
307
+ {
308
+ std::lock_guard<std::mutex> lock(_mutex);
309
+ target = _target;
310
+ recording = _lastPresented;
311
+ }
312
+ if (target) {
313
+ if (auto latest = target->peekLatest()) {
314
+ recording = latest;
315
+ }
316
+ }
317
+ if (recording == nullptr) {
318
+ return false;
319
+ }
320
+ RNSkGraphiteTargetInfo targetInfo;
321
+ if (!provider->getTargetInfo(&targetInfo)) {
322
+ return false;
323
+ }
324
+ return present(provider, targetInfo, {recording}, /* remember= */ false);
325
+ }
312
326
 
313
327
  /**
314
- Returns the scaled height of the view
328
+ Replays the recordings onto the provider's target, in order, and remembers
329
+ the last one for the next redraw when asked to (not for a snapshot).
330
+ Returns false when the provider could not present (no surface, app in the
331
+ background), in which case the caller keeps the recordings.
315
332
  */
316
- int getScaledHeight() { return _canvasProvider->getHeight(); }
317
-
318
- protected:
319
- std::shared_ptr<RNSkPlatformContext> getPlatformContext() {
320
- return _platformContext;
333
+ bool
334
+ present(const std::shared_ptr<RNSkCanvasProvider> &provider,
335
+ const RNSkGraphiteTargetInfo &targetInfo,
336
+ const std::vector<std::shared_ptr<RNSkGraphiteRecording>> &recordings,
337
+ bool remember) {
338
+ // Recordings of the target's size are replayed straight onto it, as one
339
+ // batch; one of another size goes through a texture of its own size.
340
+ std::vector<skgpu::graphite::Recording *> batch;
341
+ std::shared_ptr<RNSkGraphiteRecording> last;
342
+ bool success = true;
343
+ auto flush = [&]() {
344
+ if (!batch.empty()) {
345
+ success = provider->presentRecordings(batch) && success;
346
+ batch.clear();
347
+ }
348
+ };
349
+ for (const auto &recording : recordings) {
350
+ // A recording made for another format (recorded before the surface
351
+ // existed, with a bit depth the surface did not get) cannot be
352
+ // replayed onto this one.
353
+ if (!recording->hasFormatOf(targetInfo)) {
354
+ RNSkLogger::logToConsole("SkiaView: skipping a recording made for a "
355
+ "different surface format");
356
+ continue;
357
+ }
358
+ if (recording->hasSizeOf(targetInfo)) {
359
+ batch.push_back(recording->get());
360
+ last = recording;
361
+ continue;
362
+ }
363
+ flush();
364
+ if (presentResized(provider, recording)) {
365
+ last = recording;
366
+ } else {
367
+ success = false;
368
+ }
369
+ }
370
+ flush();
371
+ if (last == nullptr) {
372
+ // Nothing presentable: the recordings are consumed, not kept.
373
+ return true;
374
+ }
375
+ if (success && remember) {
376
+ std::lock_guard<std::mutex> lock(_mutex);
377
+ _lastPresented = last;
378
+ }
379
+ return success;
321
380
  }
322
- std::shared_ptr<RNSkCanvasProvider> getCanvasProvider() {
323
- return _canvasProvider;
381
+
382
+ /**
383
+ Presents a recording made for another size than the target's: the size
384
+ JS measured before the surface existed can be a pixel off (the platform
385
+ rounds the layout to pixels on its own terms), and the view may have been
386
+ resized since. The recording is replayed into a texture of its own size
387
+ and that texture is drawn onto the target, so the frame shows rather than
388
+ being dropped; the copy only happens while the sizes differ.
389
+ */
390
+ bool presentResized(const std::shared_ptr<RNSkCanvasProvider> &provider,
391
+ const std::shared_ptr<RNSkGraphiteRecording> &recording) {
392
+ const auto &size = recording->getTarget();
393
+ auto intermediate = std::make_shared<RNSkOffscreenCanvasProvider>(
394
+ _platformContext, size.width, size.height);
395
+ RNSkGraphiteTargetInfo info;
396
+ if (!intermediate->getTargetInfo(&info) ||
397
+ !recording->isCompatibleWith(info)) {
398
+ // No texture of that size and format: the frame is consumed.
399
+ RNSkLogger::logToConsole("SkiaView: skipping a %dx%d recording, the "
400
+ "surface is %dx%d",
401
+ size.width, size.height, provider->getWidth(),
402
+ provider->getHeight());
403
+ return true;
404
+ }
405
+ if (!intermediate->presentRecordings({recording->get()})) {
406
+ return false;
407
+ }
408
+ auto image = intermediate->makeImage();
409
+ if (image == nullptr) {
410
+ return false;
411
+ }
412
+ return provider->presentImage(image);
324
413
  }
325
414
 
326
- private:
327
415
  std::shared_ptr<RNSkPlatformContext> _platformContext;
328
416
  std::shared_ptr<RNSkCanvasProvider> _canvasProvider;
329
- std::shared_ptr<RNSkRenderer> _renderer;
330
-
331
- size_t _nativeId;
332
-
417
+ std::shared_ptr<RNSkGraphiteProducer> _producer;
418
+ size_t _nativeId = 0;
419
+ std::function<void()> _frameScheduler;
333
420
  std::atomic<bool> _redrawRequested = {false};
421
+
422
+ // The target is bound on the JS thread and read on the main thread and by
423
+ // snapshots; the last presented frame is written by the main thread and
424
+ // read by snapshots.
425
+ std::mutex _mutex;
426
+ std::shared_ptr<RNSkGraphiteTarget> _target;
427
+ size_t _targetId = 0;
428
+ std::shared_ptr<RNSkGraphiteRecording> _lastPresented;
334
429
  };
335
430
 
336
431
  } // namespace RNSkia
package/jestSetup.js CHANGED
@@ -21,7 +21,5 @@ jest.mock("react-native-skia", () => {
21
21
  useFonts: () => null,
22
22
  };
23
23
  });
24
- return require("react-native-skia/lib/module/mock").Mock(
25
- global.CanvasKit
26
- );
24
+ return require("react-native-skia/lib/module/mock").Mock(global.CanvasKit);
27
25
  });