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
@@ -1,708 +0,0 @@
1
- #pragma once
2
-
3
- #if defined(SK_GRAPHITE)
4
-
5
- #include <deque>
6
- #include <functional>
7
- #include <memory>
8
- #include <mutex>
9
- #include <stdexcept>
10
- #include <string>
11
- #include <unordered_map>
12
- #include <utility>
13
- #include <vector>
14
-
15
- #include "RNDawnContext.h"
16
- #include "RNDawnUtils.h"
17
- #include "RNSkGraphiteProducer.h"
18
- #include "RNSkPlatformContext.h"
19
- #include "RNSkView.h"
20
- #include "jsi/ViewProperty.h"
21
- #include "utils/RNSkLog.h"
22
-
23
- #pragma clang diagnostic push
24
- #pragma clang diagnostic ignored "-Wdocumentation"
25
-
26
- #include "include/core/SkCanvas.h"
27
- #include "include/core/SkColorSpace.h"
28
- #include "include/core/SkImageInfo.h"
29
- #include "include/gpu/graphite/Recorder.h"
30
- #include "include/gpu/graphite/Recording.h"
31
- #include "include/gpu/graphite/TextureInfo.h"
32
- #include "include/gpu/graphite/dawn/DawnGraphiteTypes.h"
33
-
34
- #pragma clang diagnostic pop
35
-
36
- namespace RNSkia {
37
-
38
- /**
39
- * The Graphite recorder of one SkiaGraphiteView. Shared by the view's target
40
- * and by every recording snapped from it, so that the recorder outlives its
41
- * recordings whichever of them is released last.
42
- */
43
- struct RNSkGraphiteRecorder {
44
- std::unique_ptr<skgpu::graphite::Recorder> recorder;
45
- };
46
-
47
- /**
48
- * A frame snapped from a view's recorder, with the description of the target
49
- * it was recorded for. Immutable once snapped: it can be replayed any number
50
- * of times, from any thread.
51
- */
52
- class RNSkGraphiteRecording {
53
- public:
54
- RNSkGraphiteRecording(std::shared_ptr<RNSkGraphiteRecorder> recorder,
55
- std::unique_ptr<skgpu::graphite::Recording> recording,
56
- const RNSkGraphiteTargetInfo &target)
57
- : _recorder(std::move(recorder)), _recording(std::move(recording)),
58
- _target(target) {}
59
-
60
- skgpu::graphite::Recording *get() const { return _recording.get(); }
61
-
62
- /**
63
- Whether the recording was made for a texture of the given size. A deferred
64
- canvas records against fixed dimensions: replaying it onto a texture of
65
- another size makes Graphite copy and draw outside of it, which Dawn
66
- rejects (the whole frame is then dropped). A view that was resized has
67
- stale recordings in flight; they are skipped and the content is recorded
68
- again for the new size.
69
- */
70
- bool hasSizeOf(const RNSkGraphiteTargetInfo &target) const {
71
- return _target.width == target.width && _target.height == target.height;
72
- }
73
-
74
- /**
75
- Whether the recording has the format of the given target, and the target
76
- supports every usage the recording relies on.
77
- */
78
- bool hasFormatOf(const RNSkGraphiteTargetInfo &target) const {
79
- return _target.colorType == target.colorType &&
80
- _target.textureInfo.canBeFulfilledBy(target.textureInfo);
81
- }
82
-
83
- /** Whether the recording can be replayed onto the given target. */
84
- bool isCompatibleWith(const RNSkGraphiteTargetInfo &target) const {
85
- return hasSizeOf(target) && hasFormatOf(target);
86
- }
87
-
88
- private:
89
- // Declared before the recording so that the recording is released first.
90
- std::shared_ptr<RNSkGraphiteRecorder> _recorder;
91
- std::unique_ptr<skgpu::graphite::Recording> _recording;
92
- RNSkGraphiteTargetInfo _target;
93
- };
94
-
95
- /**
96
- * What a SkiaGraphiteView and the JS objects recording for it share: the
97
- * view's recorder, the queue of recordings waiting to be presented and the
98
- * description of the target texture. Created by whichever side comes first
99
- * (see RNSkGraphiteTargetRegistry) and kept alive by both.
100
- *
101
- * Recording happens on the producer's thread (the JS thread or a worklet
102
- * runtime), presenting on the main thread; the queue is the hand-off. A
103
- * recorder serves one thread at a time, which is enforced by allowing a
104
- * single open recording.
105
- */
106
- class RNSkGraphiteTarget {
107
- public:
108
- explicit RNSkGraphiteTarget(std::shared_ptr<RNSkPlatformContext> context)
109
- : _context(std::move(context)) {}
110
-
111
- // Producer side ------------------------------------------------------------
112
-
113
- /**
114
- The layout size (points) and the props the format follows from, as known
115
- to JS. Used until the view has a surface of its own.
116
- */
117
- void setLayout(float width, float height, bool opaque, bool highBitDepth) {
118
- std::lock_guard<std::mutex> lock(_stateMutex);
119
- _layoutWidth = width;
120
- _layoutHeight = height;
121
- _opaque = opaque;
122
- _highBitDepth = highBitDepth;
123
- }
124
-
125
- /** Current width of the target, in points. */
126
- float getWidth() {
127
- return resolveTargetInfo().width / _context->getPixelDensity();
128
- }
129
-
130
- /** Current height of the target, in points. */
131
- float getHeight() {
132
- return resolveTargetInfo().height / _context->getPixelDensity();
133
- }
134
-
135
- /**
136
- Returns a canvas that records the next frame, in points. The canvas is
137
- deleted by finishRecording(). Throws while a recording is already open.
138
- */
139
- SkCanvas *beginRecording() {
140
- auto target = resolveTargetInfo();
141
- std::lock_guard<std::mutex> lock(_stateMutex);
142
- if (_recording) {
143
- throw std::runtime_error("SkiaGraphiteView: a recording is already open, "
144
- "call finishRecording() first.");
145
- }
146
- if (target.width <= 0 || target.height <= 0) {
147
- throw std::runtime_error("SkiaGraphiteView: the view has no size yet.");
148
- }
149
- if (_recorder == nullptr) {
150
- auto recorder = std::make_shared<RNSkGraphiteRecorder>();
151
- recorder->recorder = DawnContext::getInstance().makeRecorder();
152
- if (recorder->recorder == nullptr) {
153
- throw std::runtime_error(
154
- "SkiaGraphiteView: could not create a Graphite recorder.");
155
- }
156
- _recorder = std::move(recorder);
157
- }
158
- auto imageInfo =
159
- SkImageInfo::Make(target.width, target.height, target.colorType,
160
- kPremul_SkAlphaType, SkColorSpace::MakeSRGB());
161
- auto *canvas =
162
- _recorder->recorder->makeDeferredCanvas(imageInfo, target.textureInfo);
163
- if (canvas == nullptr) {
164
- throw std::runtime_error(
165
- "SkiaGraphiteView: could not create a deferred canvas.");
166
- }
167
- // The canvas loads whatever the target holds: the producer clears.
168
- auto pd = _context->getPixelDensity();
169
- canvas->scale(pd, pd);
170
- _recording = true;
171
- _recordingTarget = target;
172
- return canvas;
173
- }
174
-
175
- /** Snaps the open recording. Throws when no recording is open. */
176
- std::shared_ptr<RNSkGraphiteRecording> finishRecording() {
177
- std::lock_guard<std::mutex> lock(_stateMutex);
178
- if (!_recording) {
179
- throw std::runtime_error("SkiaGraphiteView: no recording is open, call "
180
- "beginRecording() first.");
181
- }
182
- _recording = false;
183
- auto recording = _recorder->recorder->snap();
184
- if (recording == nullptr) {
185
- throw std::runtime_error(
186
- "SkiaGraphiteView: snapping the recording failed.");
187
- }
188
- return std::make_shared<RNSkGraphiteRecording>(
189
- _recorder, std::move(recording), _recordingTarget);
190
- }
191
-
192
- /**
193
- Queues a recording for the next frame. Recordings are never dropped: a
194
- later frame may depend on resources an earlier one uploaded.
195
- */
196
- void submit(std::shared_ptr<RNSkGraphiteRecording> recording) {
197
- std::function<void()> requestFrame;
198
- {
199
- std::lock_guard<std::mutex> lock(_queueMutex);
200
- _queue.push_back(std::move(recording));
201
- requestFrame = _requestFrame;
202
- }
203
- if (requestFrame) {
204
- requestFrame();
205
- }
206
- }
207
-
208
- // View side ----------------------------------------------------------------
209
-
210
- /**
211
- Binds the target to the view's surface: the provider describes the target
212
- texture, requestFrame asks the view to present the queue.
213
- */
214
- void attach(std::weak_ptr<RNSkCanvasProvider> provider,
215
- std::function<void()> requestFrame) {
216
- {
217
- std::lock_guard<std::mutex> lock(_stateMutex);
218
- _provider = std::move(provider);
219
- }
220
- std::lock_guard<std::mutex> lock(_queueMutex);
221
- _requestFrame = std::move(requestFrame);
222
- }
223
-
224
- void detach() {
225
- {
226
- std::lock_guard<std::mutex> lock(_stateMutex);
227
- _provider.reset();
228
- }
229
- std::lock_guard<std::mutex> lock(_queueMutex);
230
- _requestFrame = nullptr;
231
- }
232
-
233
- /** Takes every recording submitted since the last call, in order. */
234
- std::vector<std::shared_ptr<RNSkGraphiteRecording>> takeQueued() {
235
- std::lock_guard<std::mutex> lock(_queueMutex);
236
- std::vector<std::shared_ptr<RNSkGraphiteRecording>> recordings(
237
- _queue.begin(), _queue.end());
238
- _queue.clear();
239
- return recordings;
240
- }
241
-
242
- /**
243
- Puts recordings that could not be presented back at the front of the
244
- queue, ahead of anything submitted meanwhile, so that the order holds.
245
- */
246
- void requeue(
247
- const std::vector<std::shared_ptr<RNSkGraphiteRecording>> &recordings) {
248
- std::lock_guard<std::mutex> lock(_queueMutex);
249
- _queue.insert(_queue.begin(), recordings.begin(), recordings.end());
250
- }
251
-
252
- /** The most recently submitted recording still waiting, if any. */
253
- std::shared_ptr<RNSkGraphiteRecording> peekLatest() {
254
- std::lock_guard<std::mutex> lock(_queueMutex);
255
- return _queue.empty() ? nullptr : _queue.back();
256
- }
257
-
258
- bool hasQueued() {
259
- std::lock_guard<std::mutex> lock(_queueMutex);
260
- return !_queue.empty();
261
- }
262
-
263
- private:
264
- /**
265
- The target to record against: the view's surface when it has one, else a
266
- description derived from the layout size and the props, following the
267
- rules the surface will be created with (see DawnWindowContext and
268
- SkiaBaseView.java).
269
- */
270
- RNSkGraphiteTargetInfo resolveTargetInfo() {
271
- std::shared_ptr<RNSkCanvasProvider> provider;
272
- float width;
273
- float height;
274
- bool opaque;
275
- bool highBitDepth;
276
- {
277
- std::lock_guard<std::mutex> lock(_stateMutex);
278
- provider = _provider.lock();
279
- width = _layoutWidth;
280
- height = _layoutHeight;
281
- opaque = _opaque;
282
- highBitDepth = _highBitDepth;
283
- }
284
- RNSkGraphiteTargetInfo info;
285
- if (provider && provider->getGraphiteTargetInfo(&info)) {
286
- return info;
287
- }
288
- #if defined(__ANDROID__)
289
- // The 10-bit buffer format only has 2 bits of alpha: translucent views
290
- // stay 8-bit.
291
- if (!opaque) {
292
- highBitDepth = false;
293
- }
294
- #else
295
- (void)opaque;
296
- #endif
297
- auto pd = _context->getPixelDensity();
298
- info.width = static_cast<int>(width * pd);
299
- info.height = static_cast<int>(height * pd);
300
- info.colorType = highBitDepth ? DawnUtils::HighBitDepthColorType
301
- : DawnUtils::PreferedColorType;
302
- info.textureInfo = skgpu::graphite::TextureInfos::MakeDawn(
303
- skgpu::graphite::DawnTextureInfo(
304
- skgpu::graphite::SampleCount::k1, skgpu::Mipmapped::kNo,
305
- highBitDepth ? DawnUtils::HighBitDepthTextureFormat
306
- : DawnUtils::PreferredTextureFormat,
307
- DawnUtils::DefaultTargetUsage, wgpu::TextureAspect::All));
308
- return info;
309
- }
310
-
311
- std::shared_ptr<RNSkPlatformContext> _context;
312
-
313
- // Recorder, open recording and target description.
314
- std::mutex _stateMutex;
315
- std::shared_ptr<RNSkGraphiteRecorder> _recorder;
316
- bool _recording = false;
317
- RNSkGraphiteTargetInfo _recordingTarget;
318
- std::weak_ptr<RNSkCanvasProvider> _provider;
319
- float _layoutWidth = 0;
320
- float _layoutHeight = 0;
321
- bool _opaque = false;
322
- bool _highBitDepth = false;
323
-
324
- // Hand-off to the main thread.
325
- std::mutex _queueMutex;
326
- std::deque<std::shared_ptr<RNSkGraphiteRecording>> _queue;
327
- std::function<void()> _requestFrame;
328
- };
329
-
330
- /**
331
- * Targets by native view id. Entries are weak: a target lives as long as the
332
- * view or a JS context object holds it.
333
- */
334
- class RNSkGraphiteTargetRegistry {
335
- public:
336
- static RNSkGraphiteTargetRegistry &getInstance() {
337
- static RNSkGraphiteTargetRegistry instance;
338
- return instance;
339
- }
340
-
341
- RNSkGraphiteTargetRegistry(const RNSkGraphiteTargetRegistry &) = delete;
342
- RNSkGraphiteTargetRegistry &
343
- operator=(const RNSkGraphiteTargetRegistry &) = delete;
344
-
345
- std::shared_ptr<RNSkGraphiteTarget>
346
- getOrCreate(size_t nativeId,
347
- const std::shared_ptr<RNSkPlatformContext> &context) {
348
- std::lock_guard<std::mutex> lock(_mutex);
349
- auto it = _targets.find(nativeId);
350
- if (it != _targets.end()) {
351
- if (auto target = it->second.lock()) {
352
- return target;
353
- }
354
- }
355
- for (auto entry = _targets.begin(); entry != _targets.end();) {
356
- entry = entry->second.expired() ? _targets.erase(entry) : ++entry;
357
- }
358
- auto target = std::make_shared<RNSkGraphiteTarget>(context);
359
- _targets[nativeId] = target;
360
- return target;
361
- }
362
-
363
- private:
364
- RNSkGraphiteTargetRegistry() = default;
365
- std::mutex _mutex;
366
- std::unordered_map<size_t, std::weak_ptr<RNSkGraphiteTarget>> _targets;
367
- };
368
-
369
- /**
370
- * Presents the recordings queued on a target. Runs on the main thread, except
371
- * renderLastFrame() which serves snapshots from any thread.
372
- */
373
- class RNSkGraphiteRenderer : public RNSkRenderer {
374
- public:
375
- explicit RNSkGraphiteRenderer(std::function<void()> requestRedraw)
376
- : RNSkRenderer(std::move(requestRedraw)) {}
377
-
378
- virtual ~RNSkGraphiteRenderer() = default;
379
-
380
- void setTarget(std::shared_ptr<RNSkGraphiteTarget> target) {
381
- std::lock_guard<std::mutex> lock(_mutex);
382
- _target = std::move(target);
383
- }
384
-
385
- /**
386
- Called on the main thread after each presented frame (the producer paces
387
- itself on it) and whenever the view asks for a redraw: a new surface, a
388
- resize, or SkiaViewApi.requestRedraw. The latter returns whether a frame
389
- is coming for it. Set once, at construction.
390
- */
391
- void setOnPresented(std::function<void()> callback) {
392
- _onPresented = std::move(callback);
393
- }
394
-
395
- void setOnRedraw(std::function<bool()> callback) {
396
- _onRedraw = std::move(callback);
397
- }
398
-
399
- /**
400
- Presents everything submitted since the last frame. With nothing queued,
401
- presents the last frame again: a redraw after a resize or on a new
402
- surface. Without a surface the queue is left alone: the surface presents
403
- it when it appears.
404
- */
405
- void
406
- renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
407
- RNSkGraphiteTargetInfo targetInfo;
408
- if (!canvasProvider->getGraphiteTargetInfo(&targetInfo)) {
409
- return;
410
- }
411
- // Declarative content is recorded again for the surface as it is now.
412
- const bool frameComing = _onRedraw && _onRedraw();
413
- std::shared_ptr<RNSkGraphiteTarget> target;
414
- std::shared_ptr<RNSkGraphiteRecording> lastPresented;
415
- {
416
- std::lock_guard<std::mutex> lock(_mutex);
417
- target = _target;
418
- lastPresented = _lastPresented;
419
- }
420
- std::vector<std::shared_ptr<RNSkGraphiteRecording>> recordings;
421
- if (target) {
422
- recordings = target->takeQueued();
423
- }
424
- if (recordings.empty()) {
425
- // With a frame on its way, the layer keeps showing the last one until
426
- // it lands: presenting it again would only cost a second present. The
427
- // same holds after a resize, when the last frame has the old size.
428
- if (frameComing || lastPresented == nullptr ||
429
- !lastPresented->hasSizeOf(targetInfo)) {
430
- return;
431
- }
432
- if (present(canvasProvider, targetInfo, {lastPresented},
433
- /* remember= */ true)) {
434
- notifyPresented();
435
- }
436
- return;
437
- }
438
- if (present(canvasProvider, targetInfo, recordings,
439
- /* remember= */ true)) {
440
- notifyPresented();
441
- } else if (target) {
442
- target->requeue(recordings);
443
- }
444
- }
445
-
446
- /**
447
- Presents the queued recordings, if any. Returns whether more are waiting,
448
- so that the caller keeps its frame callback armed: also when the present
449
- failed (the app is in the background), so that the recordings are tried
450
- again on the next frame rather than left waiting for a redraw. Without a
451
- surface the queue is left alone: the surface presents it when it appears.
452
- */
453
- bool presentQueued(const std::shared_ptr<RNSkCanvasProvider> &provider) {
454
- std::shared_ptr<RNSkGraphiteTarget> target;
455
- {
456
- std::lock_guard<std::mutex> lock(_mutex);
457
- target = _target;
458
- }
459
- if (target == nullptr) {
460
- return false;
461
- }
462
- RNSkGraphiteTargetInfo targetInfo;
463
- if (!provider->getGraphiteTargetInfo(&targetInfo)) {
464
- return false;
465
- }
466
- auto recordings = target->takeQueued();
467
- if (recordings.empty()) {
468
- return false;
469
- }
470
- if (!present(provider, targetInfo, recordings, /* remember= */ true)) {
471
- target->requeue(recordings);
472
- return true;
473
- }
474
- notifyPresented();
475
- return target->hasQueued();
476
- }
477
-
478
- /**
479
- Replays the frame on screen, or the one about to be, onto another
480
- provider (a snapshot surface).
481
- */
482
- bool renderLastFrame(const std::shared_ptr<RNSkCanvasProvider> &provider) {
483
- std::shared_ptr<RNSkGraphiteTarget> target;
484
- std::shared_ptr<RNSkGraphiteRecording> recording;
485
- {
486
- std::lock_guard<std::mutex> lock(_mutex);
487
- target = _target;
488
- recording = _lastPresented;
489
- }
490
- if (target) {
491
- if (auto latest = target->peekLatest()) {
492
- recording = latest;
493
- }
494
- }
495
- if (recording == nullptr) {
496
- return false;
497
- }
498
- RNSkGraphiteTargetInfo targetInfo;
499
- if (!provider->getGraphiteTargetInfo(&targetInfo)) {
500
- return false;
501
- }
502
- return present(provider, targetInfo, {recording}, /* remember= */ false);
503
- }
504
-
505
- private:
506
- /**
507
- Replays the recordings onto the provider's target, and remembers the last
508
- one for the next redraw when asked to (not for a snapshot). Returns false
509
- when the provider could not present (no surface, app in the background),
510
- in which case nothing was consumed and the caller keeps the recordings.
511
- The on-screen callers tell the producer about a consumed frame.
512
- */
513
- bool
514
- present(const std::shared_ptr<RNSkCanvasProvider> &provider,
515
- const RNSkGraphiteTargetInfo &targetInfo,
516
- const std::vector<std::shared_ptr<RNSkGraphiteRecording>> &recordings,
517
- bool remember) {
518
- std::vector<skgpu::graphite::Recording *> raw;
519
- std::shared_ptr<RNSkGraphiteRecording> last;
520
- for (const auto &recording : recordings) {
521
- // A recording made for the size the view had before a resize is
522
- // dropped quietly: the next one is recorded for the new size.
523
- if (!recording->hasSizeOf(targetInfo)) {
524
- continue;
525
- }
526
- // A recording made for another format (recorded before the surface
527
- // existed, with a bit depth the surface did not get) cannot be
528
- // replayed onto this one.
529
- if (!recording->hasFormatOf(targetInfo)) {
530
- RNSkLogger::logToConsole("SkiaGraphiteView: skipping a recording made "
531
- "for a different surface format");
532
- continue;
533
- }
534
- raw.push_back(recording->get());
535
- last = recording;
536
- }
537
- if (raw.empty()) {
538
- // Nothing presentable: the recordings are consumed, not kept.
539
- return true;
540
- }
541
- bool success = provider->presentRecordings(raw);
542
- if (success && remember) {
543
- std::lock_guard<std::mutex> lock(_mutex);
544
- _lastPresented = last;
545
- }
546
- return success;
547
- }
548
-
549
- void notifyPresented() {
550
- if (_onPresented) {
551
- _onPresented();
552
- }
553
- }
554
-
555
- std::mutex _mutex;
556
- std::shared_ptr<RNSkGraphiteTarget> _target;
557
- std::shared_ptr<RNSkGraphiteRecording> _lastPresented;
558
- std::function<void()> _onPresented;
559
- std::function<bool()> _onRedraw;
560
- };
561
-
562
- /**
563
- * A view that presents Graphite recordings. Frames come from one of two
564
- * producers: JS records them itself through the target
565
- * (SkiaViewApi.makeGraphiteContext), or <GraphiteCanvas> hands over a recorder
566
- * (or a picture) that the render thread pool records for the view. The
567
- * platform view presents them on its display link. Only available with the
568
- * Graphite backend.
569
- */
570
- class RNSkGraphiteView : public RNSkView {
571
- public:
572
- RNSkGraphiteView(std::shared_ptr<RNSkPlatformContext> context,
573
- std::shared_ptr<RNSkCanvasProvider> canvasProvider)
574
- : RNSkView(context, canvasProvider,
575
- std::make_shared<RNSkGraphiteRenderer>(
576
- std::bind(&RNSkGraphiteView::requestRedraw, this))),
577
- _producer(std::make_shared<RNSkGraphiteProducer>()) {
578
- std::weak_ptr<RNSkGraphiteProducer> weakProducer = _producer;
579
- getGraphiteRenderer()->setOnPresented([weakProducer]() {
580
- if (auto producer = weakProducer.lock()) {
581
- producer->onFramePresented();
582
- }
583
- });
584
- getGraphiteRenderer()->setOnRedraw([weakProducer]() {
585
- if (auto producer = weakProducer.lock()) {
586
- return producer->requestFrame();
587
- }
588
- return false;
589
- });
590
- }
591
-
592
- ~RNSkGraphiteView() override {
593
- if (_target) {
594
- _target->detach();
595
- }
596
- }
597
-
598
- /** Declarative content: the recorder of <GraphiteCanvas>, or a picture. */
599
- void setJsiProperties(
600
- std::unordered_map<std::string, RNJsi::ViewProperty> &props) override {
601
- for (auto &prop : props) {
602
- if (prop.first == "recorder") {
603
- _producer->setRecorder(
604
- prop.second.isRecorder() ? prop.second.getRecorder() : nullptr);
605
- } else if (prop.first == "picture") {
606
- _producer->setPicture(prop.second.isPicture() ? prop.second.getPicture()
607
- : nullptr);
608
- }
609
- }
610
- }
611
-
612
- bool applyUpdates(jsi::Runtime &runtime, double recorderId,
613
- const jsi::Array &values) override {
614
- return _producer->applyUpdates(runtime, recorderId, values);
615
- }
616
-
617
- void releaseContent() override { _producer->clear(); }
618
-
619
- void setNativeId(size_t nativeId) override {
620
- RNSkView::setNativeId(nativeId);
621
- if (_target && _targetId == nativeId) {
622
- return;
623
- }
624
- if (_target) {
625
- _target->detach();
626
- }
627
- _targetId = nativeId;
628
- _target = RNSkGraphiteTargetRegistry::getInstance().getOrCreate(
629
- nativeId, getPlatformContext());
630
- getGraphiteRenderer()->setTarget(_target);
631
- std::weak_ptr<RNSkGraphiteView> weakThis =
632
- std::static_pointer_cast<RNSkGraphiteView>(shared_from_this());
633
- auto context = getPlatformContext();
634
- auto scheduleOnMainThread = [weakThis, context]() {
635
- context->runOnMainThread([weakThis]() {
636
- if (auto view = weakThis.lock()) {
637
- view->scheduleFrame();
638
- }
639
- });
640
- };
641
- _target->attach(getCanvasProvider(), scheduleOnMainThread);
642
- _producer->setTarget(_target);
643
- // Frames recorded before the view existed are presented now.
644
- if (_target->hasQueued()) {
645
- scheduleOnMainThread();
646
- }
647
- }
648
-
649
- /**
650
- Installed by the platform view: arms its display link. Main thread. Without
651
- one, frames are presented as soon as the main thread gets to them.
652
- */
653
- void setFrameScheduler(std::function<void()> scheduler) {
654
- _frameScheduler = std::move(scheduler);
655
- }
656
-
657
- /** Main thread: a recording is waiting. */
658
- void scheduleFrame() {
659
- if (_frameScheduler) {
660
- _frameScheduler();
661
- } else {
662
- redraw();
663
- }
664
- }
665
-
666
- /**
667
- Display link tick: presents the queued recordings. Returns whether more
668
- are already waiting.
669
- */
670
- bool presentFrame() {
671
- return getGraphiteRenderer()->presentQueued(getCanvasProvider());
672
- }
673
-
674
- bool hasQueuedRecordings() { return _target && _target->hasQueued(); }
675
-
676
- /**
677
- Renders the view into an offscreen surface: declarative content is
678
- replayed on the calling thread with the latest values, so the snapshot
679
- does not wait for a frame; otherwise the current frame is replayed.
680
- */
681
- sk_sp<SkImage> makeImageSnapshot(SkRect *bounds) override {
682
- auto provider = std::make_shared<RNSkOffscreenCanvasProvider>(
683
- getPlatformContext(), std::bind(&RNSkView::requestRedraw, this),
684
- getScaledWidth(), getScaledHeight());
685
- if (_producer->hasContent()) {
686
- auto pd = getPlatformContext()->getPixelDensity();
687
- provider->renderToCanvas(
688
- [this, pd](SkCanvas *canvas) { _producer->renderInto(canvas, pd); });
689
- } else {
690
- getGraphiteRenderer()->renderLastFrame(provider);
691
- }
692
- return provider->makeSnapshot(bounds);
693
- }
694
-
695
- private:
696
- std::shared_ptr<RNSkGraphiteRenderer> getGraphiteRenderer() {
697
- return std::static_pointer_cast<RNSkGraphiteRenderer>(getRenderer());
698
- }
699
-
700
- std::shared_ptr<RNSkGraphiteProducer> _producer;
701
- std::shared_ptr<RNSkGraphiteTarget> _target;
702
- size_t _targetId = 0;
703
- std::function<void()> _frameScheduler;
704
- };
705
-
706
- } // namespace RNSkia
707
-
708
- #endif // SK_GRAPHITE