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,69 +0,0 @@
1
- // Everything that needs a complete Recorder lives here rather than in
2
- // RNSkPictureView.h (which platform views include on its own).
3
- #include "RNSkPictureView.h"
4
-
5
- #include <memory>
6
- #include <utility>
7
-
8
- #include "api/recorder/DrawingCtx.h"
9
- #include "api/recorder/RNRecorder.h"
10
-
11
- namespace RNSkia {
12
-
13
- void drawContent(SkCanvas *canvas, Recorder *recorder,
14
- const sk_sp<SkPicture> &picture, float pixelDensity) {
15
- canvas->clear(SK_ColorTRANSPARENT);
16
- canvas->save();
17
- canvas->scale(pixelDensity, pixelDensity);
18
- if (recorder != nullptr) {
19
- DrawingCtx ctx(canvas);
20
- recorder->play(&ctx);
21
- } else if (picture != nullptr) {
22
- canvas->drawPicture(picture);
23
- }
24
- canvas->restore();
25
- }
26
-
27
- void RNSkPictureRenderer::setRecorder(std::shared_ptr<Recorder> recorder) {
28
- sk_sp<SkPicture> picture;
29
- if (recorder != nullptr && recorder->variables.empty()) {
30
- picture = recorder->makePicture();
31
- recorder = nullptr;
32
- }
33
- std::shared_ptr<Recorder> retired;
34
- {
35
- std::lock_guard<std::mutex> lock(_mutex);
36
- retired = std::move(_recorder);
37
- _recorder = std::move(recorder);
38
- _picture = std::move(picture);
39
- }
40
- retired = nullptr;
41
- _requestRedraw();
42
- }
43
-
44
- bool RNSkPictureRenderer::applyUpdatesTo(
45
- const std::shared_ptr<Recorder> &recorder, jsi::Runtime &runtime,
46
- double recorderId, const jsi::Array &values) {
47
- if (recorder == nullptr || recorder->id != recorderId) {
48
- return false;
49
- }
50
- // The values are written into the commands by the next replay, which the
51
- // caller schedules: the mapper never waits for a draw.
52
- recorder->readUpdates(runtime, values);
53
- return true;
54
- }
55
-
56
- bool RNSkPictureRenderer::applyUpdates(jsi::Runtime &runtime, double recorderId,
57
- const jsi::Array &values) {
58
- std::shared_ptr<Recorder> recorder;
59
- {
60
- std::lock_guard<std::mutex> lock(_mutex);
61
- recorder = _recorder;
62
- }
63
- // Outside the renderer lock: a commit replacing the recorder must not wait
64
- // for the update. Should it land while this runs, the update goes into the
65
- // retired recorder and the redraw that follows draws the new one.
66
- return applyUpdatesTo(recorder, runtime, recorderId, values);
67
- }
68
-
69
- } // namespace RNSkia
@@ -1,211 +0,0 @@
1
- #pragma once
2
-
3
- #include <functional>
4
- #include <memory>
5
- #include <mutex>
6
- #include <string>
7
- #include <unordered_map>
8
- #include <variant>
9
- #include <vector>
10
-
11
- #include <jsi/jsi.h>
12
-
13
- #include "RNSkView.h"
14
- #include "jsi/ViewProperty.h"
15
-
16
- #include "RNSkPlatformContext.h"
17
- #include "api/JsiSkPicture.h"
18
- #include "utils/RNSkLog.h"
19
- #include "utils/RNSkTimingInfo.h"
20
-
21
- #pragma clang diagnostic push
22
- #pragma clang diagnostic ignored "-Wdocumentation"
23
-
24
- #include "include/core/SkBBHFactory.h"
25
- #include "include/core/SkCanvas.h"
26
- #include "include/core/SkPictureRecorder.h"
27
-
28
- #pragma clang diagnostic pop
29
-
30
- class SkPicture;
31
- struct SkRect;
32
- class SkImage;
33
-
34
- namespace RNSkia {
35
-
36
- namespace jsi = facebook::jsi;
37
-
38
- class Recorder;
39
-
40
- /**
41
- * Replays declarative content, a recorder if there is one, else a picture,
42
- * into a canvas that draws in pixels: clears it and applies the density.
43
- * Shared by the picture renderer and the Graphite producer.
44
- */
45
- void drawContent(SkCanvas *canvas, Recorder *recorder,
46
- const sk_sp<SkPicture> &picture, float pixelDensity);
47
-
48
- /**
49
- * Draws either an SkPicture (imperative API, or a recording without shared
50
- * values, played once into a picture) or a Recorder (declarative <Canvas>
51
- * driven by Reanimated shared values). The recorder is owned by the
52
- * renderer, not by a JS wrapper: its commands hold every native resource the
53
- * canvas draws (images, pictures, paths), and tying their lifetime to the
54
- * garbage collector of a runtime that rarely allocates (the UI runtime) kept
55
- * them resident long after unmount. Here they are released as soon as the
56
- * recorder is replaced, or when the host view is torn down (see clear()).
57
- *
58
- * Threading: setPicture/setRecorder run on the JS thread (React commit),
59
- * applyUpdates on the UI thread (Reanimated mapper), drawInto on the main
60
- * thread (onscreen draws) or the JS thread (snapshots). _mutex only guards
61
- * the two pointers, so a commit never waits for a draw; the recorder
62
- * serializes its replay against its updates on its own.
63
- */
64
- class RNSkPictureRenderer
65
- : public RNSkRenderer,
66
- public std::enable_shared_from_this<RNSkPictureRenderer> {
67
- public:
68
- RNSkPictureRenderer(std::function<void()> requestRedraw,
69
- std::shared_ptr<RNSkPlatformContext> context)
70
- : RNSkRenderer(std::move(requestRedraw)),
71
- _platformContext(std::move(context)) {}
72
-
73
- virtual ~RNSkPictureRenderer() = default;
74
-
75
- void
76
- renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
77
- performDraw(canvasProvider);
78
- }
79
-
80
- void setPicture(sk_sp<SkPicture> picture) {
81
- {
82
- std::lock_guard<std::mutex> lock(_mutex);
83
- _picture = picture;
84
- _recorder = nullptr;
85
- }
86
- _requestRedraw();
87
- }
88
-
89
- /**
90
- * Takes ownership of a recorder. A recording without shared values is
91
- * played once into a picture here (and its commands released): nothing
92
- * will ever update it, and drawing a picture on every redraw (resize,
93
- * foreground, ref.redraw(), snapshots) is cheaper than replaying commands.
94
- * The previous recorder (and the resources its commands reference) is
95
- * released here; Recorder's destructor moves the commands to the main
96
- * thread before destroying them so GPU-backed resources are freed on the
97
- * thread that used them.
98
- */
99
- void setRecorder(std::shared_ptr<Recorder> recorder);
100
-
101
- /**
102
- * Drops the recorder and the picture without scheduling a redraw. Called
103
- * when the host view is torn down, so that the resources go away with the
104
- * view rather than with the garbage collection of its host object (on
105
- * Android the native view is only destroyed when the Java view is
106
- * finalized).
107
- */
108
- void clear() {
109
- std::shared_ptr<Recorder> retired;
110
- sk_sp<SkPicture> picture;
111
- {
112
- std::lock_guard<std::mutex> lock(_mutex);
113
- retired = std::move(_recorder);
114
- picture = std::move(_picture);
115
- }
116
- // Both are destroyed here, outside the lock.
117
- }
118
-
119
- /**
120
- * Reads the shared values on the calling runtime into the recorder's
121
- * commands. Returns false when there is no recorder to update, or when the
122
- * view holds a different recording than recorderId (a stale mapper whose
123
- * values would otherwise land in the new recording's slots).
124
- */
125
- bool applyUpdates(jsi::Runtime &runtime, double recorderId,
126
- const jsi::Array &values);
127
-
128
- /**
129
- * Same as applyUpdates(), for a recording that no view owns yet (one
130
- * queued in the view registry until its view registers).
131
- */
132
- static bool applyUpdatesTo(const std::shared_ptr<Recorder> &recorder,
133
- jsi::Runtime &runtime, double recorderId,
134
- const jsi::Array &values);
135
-
136
- /**
137
- * Draws the current content (recorder or picture) into the canvas, scaled
138
- * by the pixel density. Used for onscreen draws, snapshots and the Android
139
- * bitmap export.
140
- */
141
- void drawInto(SkCanvas *canvas, float pixelDensity) {
142
- // Copy the content under the lock and draw from the copies: a React
143
- // commit swapping the content must only ever wait for a pointer swap,
144
- // never for a draw. If the recorder is replaced mid-draw, this frame
145
- // finishes with the old one and the swap schedules the next frame.
146
- std::shared_ptr<Recorder> recorder;
147
- sk_sp<SkPicture> picture;
148
- {
149
- std::lock_guard<std::mutex> lock(_mutex);
150
- recorder = _recorder;
151
- picture = _picture;
152
- }
153
- drawContent(canvas, recorder.get(), picture, pixelDensity);
154
- }
155
-
156
- private:
157
- bool performDraw(std::shared_ptr<RNSkCanvasProvider> canvasProvider) {
158
- auto pd = _platformContext->getPixelDensity();
159
- return canvasProvider->renderToCanvas(
160
- [this, pd](SkCanvas *canvas) { drawInto(canvas, pd); });
161
- }
162
-
163
- std::shared_ptr<RNSkPlatformContext> _platformContext;
164
- mutable std::mutex _mutex;
165
- sk_sp<SkPicture> _picture;
166
- std::shared_ptr<Recorder> _recorder;
167
- };
168
-
169
- class RNSkPictureView : public RNSkView {
170
- public:
171
- /**
172
- * Constructor
173
- */
174
- RNSkPictureView(std::shared_ptr<RNSkPlatformContext> context,
175
- std::shared_ptr<RNSkCanvasProvider> canvasProvider)
176
- : RNSkView(
177
- context, canvasProvider,
178
- std::make_shared<RNSkPictureRenderer>(
179
- std::bind(&RNSkPictureView::requestRedraw, this), context)) {}
180
-
181
- void setJsiProperties(
182
- std::unordered_map<std::string, RNJsi::ViewProperty> &props) override {
183
- auto renderer =
184
- std::static_pointer_cast<RNSkPictureRenderer>(getRenderer());
185
- for (auto &prop : props) {
186
- if (prop.first == "picture") {
187
- renderer->setPicture(prop.second.isPicture() ? prop.second.getPicture()
188
- : nullptr);
189
- } else if (prop.first == "recorder") {
190
- renderer->setRecorder(
191
- prop.second.isRecorder() ? prop.second.getRecorder() : nullptr);
192
- }
193
- }
194
- }
195
-
196
- bool applyUpdates(jsi::Runtime &runtime, double recorderId,
197
- const jsi::Array &values) override {
198
- auto renderer =
199
- std::static_pointer_cast<RNSkPictureRenderer>(getRenderer());
200
- if (!renderer->applyUpdates(runtime, recorderId, values)) {
201
- return false;
202
- }
203
- requestRedraw();
204
- return true;
205
- }
206
-
207
- void releaseContent() override {
208
- std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())->clear();
209
- }
210
- };
211
- } // namespace RNSkia