@shopify/react-native-skia 2.12.0-next.1 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/LICENSE.md +2 -1
  2. package/Package.swift +117 -0
  3. package/android/CMakeLists.txt +23 -179
  4. package/android/build.gradle +40 -209
  5. package/android/cpp/jni/JniSkiaManager.cpp +3 -45
  6. package/android/cpp/jni/include/JniPlatformContext.h +1 -1
  7. package/android/cpp/jni/include/JniSkiaBaseView.h +18 -11
  8. package/android/cpp/jni/include/JniSkiaManager.h +3 -4
  9. package/android/cpp/jni/include/JniSkiaPictureView.h +5 -5
  10. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +0 -10
  11. package/android/cpp/rnskia-android/RNSkAndroidView.h +6 -6
  12. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +4 -4
  13. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +2 -2
  14. package/android/src/main/AndroidManifest.xml +1 -1
  15. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/PlatformContext.java +1 -1
  16. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/RNSkVideo.java +1 -1
  17. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/RNSkiaModule.java +1 -8
  18. package/android/src/main/java/com/reactnative/skia/RNSkiaPackage.java +57 -0
  19. package/android/src/main/java/com/reactnative/skia/SkiaBaseView.java +185 -0
  20. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaBaseViewManager.java +20 -1
  21. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaManager.java +11 -6
  22. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaPictureView.java +3 -3
  23. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaPictureViewManager.java +1 -1
  24. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaSurfaceView.java +13 -3
  25. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaTextureView.java +5 -3
  26. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/SkiaViewAPI.java +1 -1
  27. package/android/src/main/java/com/{shopify/reactnative → reactnative}/skia/ViewScreenshotService.java +10 -13
  28. package/apple/MetalContext.mm +1 -1
  29. package/apple/RNSkApplePlatformContext.h +0 -3
  30. package/apple/RNSkApplePlatformContext.mm +0 -11
  31. package/apple/RNSkiaModule.h +1 -10
  32. package/apple/RNSkiaModule.mm +2 -10
  33. package/apple/SkiaManager.h +0 -2
  34. package/apple/SkiaManager.mm +4 -6
  35. package/apple/SkiaPictureView.h +2 -2
  36. package/apple/SkiaPictureView.mm +0 -4
  37. package/apple/SkiaUIView.h +3 -18
  38. package/apple/SkiaUIView.mm +0 -30
  39. package/cpp/api/CustomBlendModes.h +17 -0
  40. package/cpp/api/JsiSkApi.h +0 -6
  41. package/cpp/api/JsiSkCanvas.h +19 -10
  42. package/cpp/api/JsiSkFont.h +5 -10
  43. package/cpp/api/JsiSkMatrix.h +4 -2
  44. package/cpp/api/JsiSkSVGFactory.h +4 -0
  45. package/cpp/api/recorder/Convertor.h +5 -2
  46. package/cpp/api/recorder/Drawings.h +13 -3
  47. package/cpp/api/recorder/ImageFilters.h +15 -1
  48. package/cpp/api/recorder/Paint.h +8 -1
  49. package/cpp/jsi/JSICache.cpp +99 -0
  50. package/cpp/jsi/JSICache.h +90 -0
  51. package/cpp/jsi/NativeObject.h +38 -87
  52. package/cpp/jsi/ViewProperty.h +0 -28
  53. package/cpp/rnskia/RNDawnContext.h +2 -1
  54. package/cpp/rnskia/RNDawnWindowContext.h +22 -0
  55. package/cpp/rnskia/RNSkManager.cpp +0 -6
  56. package/cpp/rnskia/RNSkPlatformContext.h +0 -3
  57. package/cpp/skia/include/core/RasterContext.h +25 -0
  58. package/cpp/skia/include/core/SkCanvas.h +0 -1
  59. package/cpp/skia/include/core/SkContext.h +62 -0
  60. package/cpp/skia/include/core/SkContextOptions.h +15 -0
  61. package/cpp/skia/include/core/SkMilestone.h +1 -1
  62. package/cpp/skia/include/core/SkPicture.h +36 -18
  63. package/cpp/skia/include/core/SkRecorder.h +8 -0
  64. package/cpp/skia/include/core/SkSerialProcs.h +2 -6
  65. package/cpp/skia/include/effects/SkImageFilters.h +15 -2
  66. package/cpp/skia/include/gpu/ganesh/GrDirectContext.h +73 -47
  67. package/cpp/skia/include/gpu/ganesh/SkSurfaceGanesh.h +7 -4
  68. package/cpp/skia/include/gpu/ganesh/d3d/GrD3DDirectContext.h +12 -0
  69. package/cpp/skia/include/gpu/ganesh/gl/GrGLDirectContext.h +11 -0
  70. package/cpp/skia/include/gpu/ganesh/mtl/GrMtlDirectContext.h +11 -0
  71. package/cpp/skia/include/gpu/ganesh/vk/GrVkDirectContext.h +13 -0
  72. package/cpp/skia/include/gpu/graphite/ContextOptions.h +6 -0
  73. package/cpp/skia/include/gpu/graphite/GraphiteTypes.h +5 -4
  74. package/cpp/skia/include/gpu/graphite/PrecompileContext.h +9 -1
  75. package/cpp/skia/include/gpu/graphite/Recorder.h +0 -2
  76. package/cpp/skia/include/gpu/graphite/Recording.h +3 -0
  77. package/cpp/skia/include/gpu/graphite/dawn/DawnBackendContext.h +8 -0
  78. package/cpp/skia/include/gpu/graphite/mtl/MtlBackendContext.h +11 -0
  79. package/cpp/skia/include/gpu/graphite/vk/VulkanGraphiteContext.h +11 -0
  80. package/cpp/skia/include/private/SkLog.h +1 -19
  81. package/cpp/skia/include/private/chromium/GrVkSecondaryCBDrawContext.h +2 -1
  82. package/cpp/skia/include/utils/SkLogHandler.h +53 -0
  83. package/cpp/skia/include/utils/SkNWayCanvas.h +1 -0
  84. package/cpp/skia/modules/skcms/src/Transform_inl.h +9 -10
  85. package/cpp/skia/modules/skcms/src/skcms_internals.h +8 -3
  86. package/cpp/skia/modules/skottie/include/SkottieProperty.h +2 -0
  87. package/cpp/skia/modules/skottie/include/TextShaper.h +1 -0
  88. package/cpp/skia/modules/skshaper/include/SkShaper.h +23 -1
  89. package/include/ReactNativeSkia.h +6 -0
  90. package/lib/commonjs/dom/nodes/datatypes/Rect.js +5 -1
  91. package/lib/commonjs/dom/nodes/datatypes/Rect.js.map +1 -1
  92. package/lib/commonjs/external/reanimated/useVideo.js +8 -2
  93. package/lib/commonjs/external/reanimated/useVideo.js.map +1 -1
  94. package/lib/commonjs/mock/index.js +0 -1
  95. package/lib/commonjs/mock/index.js.map +1 -1
  96. package/lib/commonjs/renderer/Canvas.d.ts +11 -3
  97. package/lib/commonjs/renderer/Canvas.js +8 -6
  98. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  99. package/lib/commonjs/renderer/components/Mask.js +3 -9
  100. package/lib/commonjs/renderer/components/Mask.js.map +1 -1
  101. package/lib/commonjs/skia/types/JsiInstance.d.ts +1 -0
  102. package/lib/commonjs/skia/types/JsiInstance.js.map +1 -1
  103. package/lib/commonjs/skia/types/Matrix4.js +7 -2
  104. package/lib/commonjs/skia/types/Matrix4.js.map +1 -1
  105. package/lib/commonjs/skia/types/Skia.d.ts +1 -6
  106. package/lib/commonjs/skia/types/Skia.js.map +1 -1
  107. package/lib/commonjs/skia/web/JsiSkCanvas.js +12 -7
  108. package/lib/commonjs/skia/web/JsiSkCanvas.js.map +1 -1
  109. package/lib/commonjs/skia/web/JsiSkImage.js +6 -3
  110. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  111. package/lib/commonjs/skia/web/JsiSkPathBuilder.d.ts +1 -1
  112. package/lib/commonjs/skia/web/JsiSkPathBuilder.js +2 -2
  113. package/lib/commonjs/skia/web/JsiSkPathBuilder.js.map +1 -1
  114. package/lib/commonjs/skia/web/JsiSkTextStyle.js +5 -5
  115. package/lib/commonjs/skia/web/JsiSkTextStyle.js.map +1 -1
  116. package/lib/commonjs/skia/web/JsiSkia.js +0 -3
  117. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  118. package/lib/commonjs/skia/web/JsiSkottieAnimation.js +8 -8
  119. package/lib/commonjs/skia/web/JsiSkottieAnimation.js.map +1 -1
  120. package/lib/commonjs/sksg/Recorder/DrawingContext.d.ts +1 -0
  121. package/lib/commonjs/sksg/Recorder/DrawingContext.js +15 -0
  122. package/lib/commonjs/sksg/Recorder/DrawingContext.js.map +1 -1
  123. package/lib/commonjs/sksg/Recorder/Player.js +1 -1
  124. package/lib/commonjs/sksg/Recorder/Player.js.map +1 -1
  125. package/lib/commonjs/sksg/Recorder/Visitor.js +6 -1
  126. package/lib/commonjs/sksg/Recorder/Visitor.js.map +1 -1
  127. package/lib/commonjs/sksg/Recorder/commands/Drawing.js +12 -9
  128. package/lib/commonjs/sksg/Recorder/commands/Drawing.js.map +1 -1
  129. package/lib/commonjs/sksg/Recorder/commands/ImageFilters.js +15 -1
  130. package/lib/commonjs/sksg/Recorder/commands/ImageFilters.js.map +1 -1
  131. package/lib/commonjs/sksg/Recorder/commands/Shaders.js +1 -1
  132. package/lib/commonjs/sksg/Recorder/commands/Shaders.js.map +1 -1
  133. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  134. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  135. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +3 -1
  136. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js +3 -0
  137. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.js.map +1 -1
  138. package/lib/commonjs/views/SkiaPictureView.js +2 -12
  139. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  140. package/lib/commonjs/views/SkiaPictureView.web.js +247 -57
  141. package/lib/commonjs/views/SkiaPictureView.web.js.map +1 -1
  142. package/lib/commonjs/views/types.d.ts +19 -8
  143. package/lib/commonjs/views/types.js.map +1 -1
  144. package/lib/module/dom/nodes/datatypes/Rect.js +5 -1
  145. package/lib/module/dom/nodes/datatypes/Rect.js.map +1 -1
  146. package/lib/module/external/reanimated/useVideo.js +8 -2
  147. package/lib/module/external/reanimated/useVideo.js.map +1 -1
  148. package/lib/module/mock/index.js +0 -1
  149. package/lib/module/mock/index.js.map +1 -1
  150. package/lib/module/renderer/Canvas.d.ts +11 -3
  151. package/lib/module/renderer/Canvas.js +7 -5
  152. package/lib/module/renderer/Canvas.js.map +1 -1
  153. package/lib/module/renderer/components/Mask.js +3 -9
  154. package/lib/module/renderer/components/Mask.js.map +1 -1
  155. package/lib/module/skia/types/JsiInstance.d.ts +1 -0
  156. package/lib/module/skia/types/JsiInstance.js.map +1 -1
  157. package/lib/module/skia/types/Matrix4.js +7 -2
  158. package/lib/module/skia/types/Matrix4.js.map +1 -1
  159. package/lib/module/skia/types/Skia.d.ts +1 -6
  160. package/lib/module/skia/types/Skia.js.map +1 -1
  161. package/lib/module/skia/web/JsiSkCanvas.js +12 -7
  162. package/lib/module/skia/web/JsiSkCanvas.js.map +1 -1
  163. package/lib/module/skia/web/JsiSkImage.js +6 -3
  164. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  165. package/lib/module/skia/web/JsiSkPathBuilder.d.ts +1 -1
  166. package/lib/module/skia/web/JsiSkPathBuilder.js +2 -2
  167. package/lib/module/skia/web/JsiSkPathBuilder.js.map +1 -1
  168. package/lib/module/skia/web/JsiSkTextStyle.js +5 -5
  169. package/lib/module/skia/web/JsiSkTextStyle.js.map +1 -1
  170. package/lib/module/skia/web/JsiSkia.js +0 -3
  171. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  172. package/lib/module/skia/web/JsiSkottieAnimation.js +8 -8
  173. package/lib/module/skia/web/JsiSkottieAnimation.js.map +1 -1
  174. package/lib/module/sksg/Recorder/DrawingContext.d.ts +1 -0
  175. package/lib/module/sksg/Recorder/DrawingContext.js +15 -0
  176. package/lib/module/sksg/Recorder/DrawingContext.js.map +1 -1
  177. package/lib/module/sksg/Recorder/Player.js +1 -1
  178. package/lib/module/sksg/Recorder/Player.js.map +1 -1
  179. package/lib/module/sksg/Recorder/Visitor.js +6 -1
  180. package/lib/module/sksg/Recorder/Visitor.js.map +1 -1
  181. package/lib/module/sksg/Recorder/commands/Drawing.js +12 -9
  182. package/lib/module/sksg/Recorder/commands/Drawing.js.map +1 -1
  183. package/lib/module/sksg/Recorder/commands/ImageFilters.js +15 -1
  184. package/lib/module/sksg/Recorder/commands/ImageFilters.js.map +1 -1
  185. package/lib/module/sksg/Recorder/commands/Shaders.js +1 -1
  186. package/lib/module/sksg/Recorder/commands/Shaders.js.map +1 -1
  187. package/lib/module/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  188. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  189. package/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +3 -1
  190. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js +3 -0
  191. package/lib/module/specs/SkiaPictureViewNativeComponent.web.js.map +1 -1
  192. package/lib/module/views/SkiaPictureView.js +2 -12
  193. package/lib/module/views/SkiaPictureView.js.map +1 -1
  194. package/lib/module/views/SkiaPictureView.web.js +247 -57
  195. package/lib/module/views/SkiaPictureView.web.js.map +1 -1
  196. package/lib/module/views/types.d.ts +19 -8
  197. package/lib/module/views/types.js.map +1 -1
  198. package/lib/typescript/lib/commonjs/mock/index.d.ts +0 -1
  199. package/lib/typescript/lib/commonjs/renderer/Canvas.d.ts +2 -2
  200. package/lib/typescript/lib/commonjs/skia/web/JsiSkPathBuilder.d.ts +1 -1
  201. package/lib/typescript/lib/commonjs/skia/web/JsiSkia.d.ts +0 -1
  202. package/lib/typescript/lib/commonjs/sksg/Recorder/DrawingContext.d.ts +1 -0
  203. package/lib/typescript/lib/commonjs/specs/SkiaPictureViewNativeComponent.web.d.ts +3 -1
  204. package/lib/typescript/lib/module/mock/index.d.ts +0 -1
  205. package/lib/typescript/lib/module/renderer/Canvas.d.ts +2 -2
  206. package/lib/typescript/lib/module/skia/Skia.web.d.ts +0 -1
  207. package/lib/typescript/lib/module/skia/web/JsiSkPathBuilder.d.ts +1 -1
  208. package/lib/typescript/lib/module/skia/web/JsiSkia.d.ts +0 -1
  209. package/lib/typescript/lib/module/sksg/Recorder/DrawingContext.d.ts +1 -0
  210. package/lib/typescript/lib/module/specs/SkiaPictureViewNativeComponent.web.d.ts +3 -1
  211. package/lib/typescript/react-native.config.d.ts +3 -0
  212. package/lib/typescript/src/renderer/Canvas.d.ts +11 -3
  213. package/lib/typescript/src/skia/types/JsiInstance.d.ts +1 -0
  214. package/lib/typescript/src/skia/types/Skia.d.ts +1 -6
  215. package/lib/typescript/src/skia/web/JsiSkPathBuilder.d.ts +1 -1
  216. package/lib/typescript/src/sksg/Recorder/DrawingContext.d.ts +1 -0
  217. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.d.ts +2 -0
  218. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.web.d.ts +3 -1
  219. package/lib/typescript/src/views/types.d.ts +19 -8
  220. package/package.json +15 -12
  221. package/react-native-skia.podspec +4 -10
  222. package/react-native.config.js +5 -0
  223. package/src/dom/nodes/datatypes/Rect.ts +6 -1
  224. package/src/external/reanimated/useVideo.ts +7 -2
  225. package/src/mock/index.ts +0 -1
  226. package/src/renderer/Canvas.tsx +23 -5
  227. package/src/renderer/components/Mask.tsx +10 -3
  228. package/src/skia/types/JsiInstance.ts +2 -0
  229. package/src/skia/types/Matrix4.ts +6 -2
  230. package/src/skia/types/Skia.ts +1 -7
  231. package/src/skia/web/JsiSkCanvas.ts +22 -12
  232. package/src/skia/web/JsiSkImage.ts +6 -3
  233. package/src/skia/web/JsiSkPathBuilder.ts +2 -2
  234. package/src/skia/web/JsiSkTextStyle.ts +20 -15
  235. package/src/skia/web/JsiSkia.ts +0 -4
  236. package/src/skia/web/JsiSkottieAnimation.ts +8 -8
  237. package/src/sksg/Recorder/DrawingContext.ts +15 -0
  238. package/src/sksg/Recorder/Player.ts +1 -1
  239. package/src/sksg/Recorder/Visitor.ts +9 -6
  240. package/src/sksg/Recorder/commands/Drawing.ts +12 -9
  241. package/src/sksg/Recorder/commands/ImageFilters.ts +20 -1
  242. package/src/sksg/Recorder/commands/Shaders.ts +1 -1
  243. package/src/specs/SkiaPictureViewNativeComponent.ts +5 -0
  244. package/src/specs/SkiaPictureViewNativeComponent.web.ts +5 -0
  245. package/src/views/SkiaPictureView.tsx +2 -10
  246. package/src/views/SkiaPictureView.web.tsx +245 -57
  247. package/src/views/types.ts +23 -10
  248. package/android/src/latest/java/com/shopify/reactnative/skia/ReactNativeCompatible.java +0 -11
  249. package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaPackage.java +0 -79
  250. package/android/src/main/java/com/shopify/reactnative/skia/SkiaAHBView.java +0 -113
  251. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +0 -136
  252. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaPictureViewManagerDelegate.java +0 -41
  253. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaPictureViewManagerInterface.java +0 -20
  254. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaWebGPUViewManagerDelegate.java +0 -35
  255. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaWebGPUViewManagerInterface.java +0 -17
  256. package/android/src/paper/java/com/shopify/reactnative/skia/NativeSkiaModuleSpec.java +0 -38
  257. package/android/src/reactnative74/java/com/shopify/reactnative/skia/ReactNativeCompatible.java +0 -14
  258. package/apple/SkiaPictureViewManager.h +0 -8
  259. package/apple/SkiaPictureViewManager.mm +0 -60
  260. package/apple/SkiaWebGPUView.h +0 -20
  261. package/apple/WebGPUMetalView.h +0 -12
  262. package/cpp/api/JsiSkiaContext.h +0 -94
  263. package/cpp/dawn/include/dawn/dawn_proc.h +0 -53
  264. package/cpp/dawn/include/dawn/dawn_proc_table.h +0 -330
  265. package/cpp/dawn/include/dawn/dawn_thread_dispatch_proc.h +0 -47
  266. package/cpp/dawn/include/dawn/dawn_version.h +0 -41
  267. package/cpp/dawn/include/dawn/native/D3D11Backend.h +0 -65
  268. package/cpp/dawn/include/dawn/native/D3D12Backend.h +0 -103
  269. package/cpp/dawn/include/dawn/native/D3DBackend.h +0 -56
  270. package/cpp/dawn/include/dawn/native/DawnNative.h +0 -368
  271. package/cpp/dawn/include/dawn/native/MetalBackend.h +0 -56
  272. package/cpp/dawn/include/dawn/native/NullBackend.h +0 -39
  273. package/cpp/dawn/include/dawn/native/OpenGLBackend.h +0 -89
  274. package/cpp/dawn/include/dawn/native/VulkanBackend.h +0 -183
  275. package/cpp/dawn/include/dawn/native/WebGPUBackend.h +0 -49
  276. package/cpp/dawn/include/dawn/native/dawn_native_export.h +0 -49
  277. package/cpp/dawn/include/dawn/platform/DawnPlatform.h +0 -207
  278. package/cpp/dawn/include/dawn/platform/dawn_platform_export.h +0 -49
  279. package/cpp/dawn/include/dawn/replay/Replay.h +0 -99
  280. package/cpp/dawn/include/dawn/replay/dawn_replay_export.h +0 -49
  281. package/cpp/dawn/include/dawn/webgpu_cpp_print.h +0 -2856
  282. package/cpp/dawn/include/tint/tint.h +0 -91
  283. package/cpp/dawn/include/webgpu/webgpu.h +0 -5029
  284. package/cpp/dawn/include/webgpu/webgpu_cpp.h +0 -10353
  285. package/cpp/dawn/include/webgpu/webgpu_cpp_chained_struct.h +0 -57
  286. package/cpp/dawn/include/webgpu/webgpu_enum_class_bitmasks.h +0 -162
  287. package/cpp/dawn/include/webgpu/webgpu_glfw.h +0 -88
  288. package/cpp/jsi/RuntimeAwareCache.cpp +0 -7
  289. package/cpp/jsi/RuntimeAwareCache.h +0 -117
  290. package/cpp/jsi/RuntimeLifecycleMonitor.cpp +0 -72
  291. package/cpp/jsi/RuntimeLifecycleMonitor.h +0 -32
  292. package/cpp/skia/src/gpu/graphite/ContextOptionsPriv.h +0 -45
  293. package/cpp/skia/src/gpu/graphite/ResourceTypes.h +0 -360
  294. package/cpp/skia/src/gpu/graphite/TextureProxyView.h +0 -105
  295. package/libs/.graphite +0 -1
@@ -36,32 +36,191 @@ interface Renderer {
36
36
  dispose(): void;
37
37
  }
38
38
 
39
+ // A <canvas> element owns at most one WebGL context for its whole lifetime.
40
+ // A renderer, on the other hand, lives as long as the layout effect that
41
+ // creates it, which is shorter: React re-runs layout effects on a *preserved*
42
+ // host node under StrictMode's DEV double-invoke and when an Activity/offscreen
43
+ // subtree is hidden and revealed (#3976). What belongs to the element rather
44
+ // than to a renderer is tracked here, per element:
45
+ // - the context, so that a new renderer can tell whether the element already
46
+ // has one (and whether it is lost) without calling getContext() itself,
47
+ // which on a fresh element would create a context with the wrong attributes
48
+ // (CanvasKit.GetWebGLContext requests its own);
49
+ // - the WEBGL_lose_context extension, captured while the context is healthy:
50
+ // getExtension() returns null on a lost context, and this object is the only
51
+ // way to lose a context on purpose or to ask the browser to restore one;
52
+ // - whether the browser has announced the loss of the context yet: a lost
53
+ // context can only be restored once its webglcontextlost event has been
54
+ // dispatched, and that event is asynchronous;
55
+ // - the renderer currently attached to the element, if any.
56
+ interface CanvasWebGL {
57
+ gl: WebGLRenderingContext | WebGL2RenderingContext;
58
+ // The WEBGL_lose_context extension.
59
+ loseContext: { loseContext(): void; restoreContext(): void } | null;
60
+ lossAnnounced: boolean;
61
+ owner: WebGLRenderer | null;
62
+ }
63
+ const canvasWebGL = new WeakMap<HTMLCanvasElement, CanvasWebGL>();
64
+
65
+ // Unregisters a context from CanvasKit's registry, which otherwise retains the
66
+ // canvas element (and its detached DOM tree) forever (#3924).
67
+ const deleteContextHandle = (handle: WebGLContextHandle) => {
68
+ CanvasKit.deleteContext(handle);
69
+ // Making the now-deleted handle current clears CanvasKit's current-context
70
+ // globals (GLctx/Module.ctx), which would otherwise keep referencing the
71
+ // context (and the canvas) until another surface becomes current. With a
72
+ // deleted handle this is a no-op that returns null without creating
73
+ // anything.
74
+ CanvasKit.MakeWebGLContext(handle);
75
+ };
76
+
39
77
  class WebGLRenderer implements Renderer {
40
78
  private surface: JsiSkSurface | null = null;
41
79
  private grContext: GrDirectContext | null = null;
42
80
  private contextHandle: WebGLContextHandle = 0;
43
81
  private pd = 1;
82
+ // Set while the renderer is waiting for a lost context to be restored.
83
+ private restoreWanted = false;
84
+ private restoreRequest: ReturnType<typeof setTimeout> | null = null;
85
+
86
+ constructor(
87
+ private canvas: HTMLCanvasElement,
88
+ // Called when the renderer becomes able to paint again and the current
89
+ // picture should be drawn.
90
+ private requestRedraw: () => void
91
+ ) {
92
+ const entry = canvasWebGL.get(canvas);
93
+ if (entry) {
94
+ entry.owner = this;
95
+ }
96
+ canvas.addEventListener("webglcontextlost", this.onContextLost);
97
+ canvas.addEventListener("webglcontextrestored", this.onContextRestored);
98
+ this.acquire();
99
+ }
44
100
 
45
- constructor(private canvas: HTMLCanvasElement) {
46
- this.contextHandle = CanvasKit.GetWebGLContext(canvas);
47
- if (!this.contextHandle) {
101
+ // Builds the GrDirectContext and the surface on the element's WebGL
102
+ // context, creating the context first if the element has none.
103
+ private acquire() {
104
+ const { canvas } = this;
105
+ const known = canvasWebGL.get(canvas);
106
+ if (known?.gl.isContextLost()) {
107
+ // Nothing can be built on a lost context: CanvasKit.GetWebGLContext
108
+ // happily registers it, and MakeWebGLContext then faults inside wasm
109
+ // (#3976). Ask the browser for it back and finish in onContextRestored;
110
+ // if the loss hasn't been announced yet, onContextLost asks.
111
+ this.restoreWanted = true;
112
+ if (known.lossAnnounced) {
113
+ known.loseContext?.restoreContext();
114
+ }
115
+ return;
116
+ }
117
+ const handle = CanvasKit.GetWebGLContext(canvas);
118
+ if (!handle) {
48
119
  throw new Error("Could not create a WebGL context");
49
120
  }
50
- this.grContext = CanvasKit.MakeWebGLContext(this.contextHandle);
51
- if (!this.grContext) {
52
- CanvasKit.deleteContext(this.contextHandle);
53
- this.contextHandle = 0;
121
+ const entry = known ?? this.trackContext();
122
+ let grContext: GrDirectContext | null = null;
123
+ try {
124
+ grContext = CanvasKit.MakeWebGLContext(handle);
125
+ } finally {
126
+ if (!grContext) {
127
+ // Whether it returned null or threw, don't leave the registry
128
+ // holding on to the canvas.
129
+ deleteContextHandle(handle);
130
+ }
131
+ }
132
+ if (!grContext) {
54
133
  throw new Error("Could not create a graphics context");
55
134
  }
56
- const ctx = canvas.getContext("webgl2");
57
- if (ctx) {
58
- ctx.drawingBufferColorSpace = "display-p3";
135
+ this.restoreWanted = false;
136
+ this.contextHandle = handle;
137
+ this.grContext = grContext;
138
+ if (entry) {
139
+ entry.gl.drawingBufferColorSpace = "display-p3";
59
140
  }
60
141
  this.onResize();
61
142
  }
62
143
 
144
+ // CanvasKit just created the element's context; asking for it again hands
145
+ // back the same object (the attributes are only honored the first time).
146
+ private trackContext(): CanvasWebGL | null {
147
+ const { canvas } = this;
148
+ const gl = canvas.getContext("webgl2") ?? canvas.getContext("webgl");
149
+ if (!gl) {
150
+ return null;
151
+ }
152
+ const entry: CanvasWebGL = {
153
+ gl,
154
+ loseContext: gl.getExtension("WEBGL_lose_context"),
155
+ lossAnnounced: false,
156
+ owner: this,
157
+ };
158
+ canvasWebGL.set(canvas, entry);
159
+ // Browsers restore a lost context (evicted as the oldest one when the
160
+ // page exceeds the active-context limit, or lost to a GPU reset) only if
161
+ // the page called preventDefault() on the webglcontextlost event. A
162
+ // renderer isn't necessarily attached when that happens (the canvas may
163
+ // be in a hidden Activity), so these listeners live as long as the
164
+ // element does. They are registered ahead of any renderer's, and the
165
+ // loss can't be announced before they exist since the event is
166
+ // asynchronous.
167
+ canvas.addEventListener("webglcontextlost", (event) => {
168
+ event.preventDefault();
169
+ entry.lossAnnounced = true;
170
+ });
171
+ canvas.addEventListener("webglcontextrestored", () => {
172
+ entry.lossAnnounced = false;
173
+ });
174
+ return entry;
175
+ }
176
+
177
+ // Frees everything built on the context. The context itself belongs to the
178
+ // element, see dispose().
179
+ private release() {
180
+ this.surface?.ref.delete();
181
+ this.surface = null;
182
+ if (this.grContext) {
183
+ this.grContext.releaseResourcesAndAbandonContext();
184
+ this.grContext.delete();
185
+ this.grContext = null;
186
+ }
187
+ if (this.contextHandle) {
188
+ deleteContextHandle(this.contextHandle);
189
+ this.contextHandle = 0;
190
+ }
191
+ }
192
+
193
+ private isContextLost() {
194
+ return canvasWebGL.get(this.canvas)?.gl.isContextLost() ?? false;
195
+ }
196
+
197
+ private onContextLost = () => {
198
+ // The browser already dropped every GL object; drop our references to
199
+ // them. Restoration is left to the browser (it restores an evicted
200
+ // context as soon as a slot frees up), except when this renderer was
201
+ // built on an already-lost context and is waiting to paint for the first
202
+ // time: chasing an eviction from here would evict the next-oldest context
203
+ // in turn, and with more live canvases than the browser allows the
204
+ // canvases would endlessly evict each other.
205
+ this.release();
206
+ if (this.restoreWanted) {
207
+ // The permission granted by preventDefault() is only recorded by the
208
+ // browser once the event dispatch is over: a restoreContext() call
209
+ // from inside the handler is refused, so ask from a later task.
210
+ this.restoreRequest = setTimeout(() => {
211
+ this.restoreRequest = null;
212
+ canvasWebGL.get(this.canvas)?.loseContext?.restoreContext();
213
+ }, 0);
214
+ }
215
+ };
216
+
217
+ private onContextRestored = () => {
218
+ this.acquire();
219
+ this.requestRedraw();
220
+ };
221
+
63
222
  makeImageSnapshot(picture: SkPicture, rect?: SkRect): SkImage | null {
64
- if (!this.surface) {
223
+ if (!this.surface || this.isContextLost()) {
65
224
  return null;
66
225
  }
67
226
  const canvas = this.surface.getCanvas();
@@ -73,7 +232,10 @@ class WebGLRenderer implements Renderer {
73
232
 
74
233
  onResize() {
75
234
  const { canvas } = this;
76
- if (!this.grContext) {
235
+ if (!this.grContext || this.isContextLost()) {
236
+ // Waiting for the context to be restored (the restore path resizes),
237
+ // or the loss hasn't been announced yet and onContextLost is about to
238
+ // release everything: either way there is nothing to build on.
77
239
  return;
78
240
  }
79
241
  this.pd = window.devicePixelRatio;
@@ -101,41 +263,57 @@ class WebGLRenderer implements Renderer {
101
263
  }
102
264
 
103
265
  draw(picture: SkPicture) {
104
- if (this.surface) {
105
- const canvas = this.surface.getCanvas();
106
- canvas.clear(Float32Array.of(0, 0, 0, 0));
107
- canvas.save();
108
- canvas.scale(this.pd, this.pd);
109
- canvas.drawPicture(picture);
110
- canvas.restore();
111
- this.surface.ref.flush();
266
+ if (!this.surface || this.isContextLost()) {
267
+ return;
112
268
  }
269
+ const canvas = this.surface.getCanvas();
270
+ canvas.clear(Float32Array.of(0, 0, 0, 0));
271
+ canvas.save();
272
+ canvas.scale(this.pd, this.pd);
273
+ canvas.drawPicture(picture);
274
+ canvas.restore();
275
+ this.surface.ref.flush();
113
276
  }
114
277
 
115
278
  dispose(): void {
116
- this.surface?.ref.delete();
117
- this.surface = null;
118
- if (this.grContext) {
119
- this.grContext.releaseResourcesAndAbandonContext();
120
- this.grContext.delete();
121
- this.grContext = null;
279
+ const { canvas } = this;
280
+ canvas.removeEventListener("webglcontextlost", this.onContextLost);
281
+ canvas.removeEventListener("webglcontextrestored", this.onContextRestored);
282
+ if (this.restoreRequest !== null) {
283
+ clearTimeout(this.restoreRequest);
284
+ this.restoreRequest = null;
122
285
  }
123
- this.canvas
124
- ?.getContext("webgl2")
125
- ?.getExtension("WEBGL_lose_context")
126
- ?.loseContext();
127
- if (this.contextHandle) {
128
- // Unregister the context from CanvasKit's internal registry, otherwise
129
- // it retains the canvas element (and its detached DOM tree) forever.
130
- CanvasKit.deleteContext(this.contextHandle);
131
- // Making the now-deleted handle current clears CanvasKit's
132
- // current-context globals (GLctx/Module.ctx), which would otherwise
133
- // keep referencing the context (and the canvas) until another surface
134
- // becomes current. With a deleted handle this is a no-op that returns
135
- // null without creating anything.
136
- CanvasKit.MakeWebGLContext(this.contextHandle);
137
- this.contextHandle = 0;
286
+ this.restoreWanted = false;
287
+ this.release();
288
+ // Free the drawing buffer, which CanvasKit requests with depth and stencil
289
+ // attachments (about 8 bytes per pixel): a canvas that stays in the
290
+ // document without a renderer, hidden in an Activity say, would otherwise
291
+ // keep a full-size buffer around. The next renderer's onResize() sizes it
292
+ // again.
293
+ canvas.width = 0;
294
+ canvas.height = 0;
295
+ const entry = canvasWebGL.get(canvas);
296
+ if (!entry) {
297
+ return;
138
298
  }
299
+ entry.owner = null;
300
+ // Whether the element is going away is only known once the commit is
301
+ // over: on a real unmount React runs this cleanup *before* removing the
302
+ // node from the document, while StrictMode's double-invoke and an
303
+ // Activity hide keep the node in place (and the former builds the next
304
+ // renderer on it right away). Losing the context of a detached element
305
+ // matters: it would otherwise stay alive until the element is garbage
306
+ // collected, and browsers cap the number of live contexts per page
307
+ // (16 in Chrome, which then evicts the oldest one, visible or not).
308
+ queueMicrotask(() => {
309
+ if (
310
+ entry.owner === null &&
311
+ !canvas.isConnected &&
312
+ !entry.gl.isContextLost()
313
+ ) {
314
+ entry.loseContext?.loseContext();
315
+ }
316
+ });
139
317
  }
140
318
  }
141
319
 
@@ -370,22 +548,30 @@ export const SkiaPictureView = (props: SkiaPictureViewProps) => {
370
548
  // - The renderer is created synchronously on mount (the canvas element is
371
549
  // guaranteed to exist in useLayoutEffect; a zero size at that point is
372
550
  // fine, the surface is created once the canvas is measurable).
373
- // - Redraw requests coalesce into a single animation frame. A picture
374
- // dispatched before the canvas has a size stays in pictureRef (drawing
375
- // while unmeasured is a no-op) and is painted by the resize path below
376
- // once the canvas becomes measurable, so it is never lost.
551
+ // - Redraw requests coalesce into a single microtask. A microtask (not an
552
+ // animation frame) so that a picture produced inside a rAF callback (the
553
+ // Reanimated mapper) is drawn before the current frame paints: deferring
554
+ // to the next rAF alternates between "flush pending in this frame" and
555
+ // "flush scheduled for the next frame", drawing on every other frame
556
+ // only and halving the effective frame rate. A picture dispatched before
557
+ // the canvas has a size stays in pictureRef (drawing while unmeasured is
558
+ // a no-op) and is painted by the resize path below once the canvas
559
+ // becomes measurable, so it is never lost.
377
560
  // - A ResizeObserver on the canvas itself recreates the surface and repaints
378
561
  // synchronously (its callbacks run after layout, before paint), and is
379
562
  // also the source of the user-facing onLayout event.
563
+ // - The renderer belongs to the layout effect, the WebGL context to the
564
+ // <canvas> element, whose lifetime is longer; see WebGLRenderer for how
565
+ // the two are reconciled.
380
566
  const redrawPendingRef = useRef(false);
381
- const frameRef = useRef(0);
567
+ const flushScheduledRef = useRef(false);
382
568
  const onLayoutRef = useRef(onLayout);
383
569
  useLayoutEffect(() => {
384
570
  onLayoutRef.current = onLayout;
385
571
  }, [onLayout]);
386
572
 
387
573
  const flushRedraw = useCallback(() => {
388
- frameRef.current = 0;
574
+ flushScheduledRef.current = false;
389
575
  if (redrawPendingRef.current && rendererRef.current && pictureRef.current) {
390
576
  redrawPendingRef.current = false;
391
577
  rendererRef.current.draw(pictureRef.current);
@@ -396,8 +582,9 @@ export const SkiaPictureView = (props: SkiaPictureViewProps) => {
396
582
 
397
583
  const redraw = useCallback(() => {
398
584
  redrawPendingRef.current = true;
399
- if (frameRef.current === 0) {
400
- frameRef.current = requestAnimationFrame(flushRedraw);
585
+ if (!flushScheduledRef.current) {
586
+ flushScheduledRef.current = true;
587
+ queueMicrotask(flushRedraw);
401
588
  }
402
589
  }, [flushRedraw]);
403
590
 
@@ -482,7 +669,7 @@ export const SkiaPictureView = (props: SkiaPictureViewProps) => {
482
669
  }
483
670
  const renderer = isStatic
484
671
  ? new StaticWebGLRenderer(canvas)
485
- : new WebGLRenderer(canvas);
672
+ : new WebGLRenderer(canvas, redraw);
486
673
  rendererRef.current = renderer;
487
674
 
488
675
  const drawPicture = () => {
@@ -566,14 +753,11 @@ export const SkiaPictureView = (props: SkiaPictureViewProps) => {
566
753
  rendererRef.current = null;
567
754
  renderer.dispose();
568
755
  };
569
- }, [isStatic]);
756
+ }, [isStatic, redraw]);
570
757
 
571
- useEffect(() => {
572
- return () => {
573
- cancelAnimationFrame(frameRef.current);
574
- frameRef.current = 0;
575
- };
576
- }, []);
758
+ // No flush cancellation is needed on unmount: a microtask queued before
759
+ // unmount runs within the same task, and flushRedraw no-ops once the
760
+ // layout-effect cleanup has nulled rendererRef.
577
761
 
578
762
  useImperativeHandle(
579
763
  ref,
@@ -632,6 +816,10 @@ export const SkiaPictureView = (props: SkiaPictureViewProps) => {
632
816
  return (
633
817
  <Platform.View {...viewProps}>
634
818
  <canvas
819
+ // A canvas element is bound to one context kind for life (WebGL for
820
+ // the live renderer, 2D for the static one), so switching renderers
821
+ // needs a fresh element.
822
+ key={isStatic ? "static" : "webgl"}
635
823
  ref={canvasRef}
636
824
  style={{ display: "block", width: "100%", height: "100%" }}
637
825
  />
@@ -1,7 +1,5 @@
1
1
  import type { ViewProps } from "react-native";
2
- import type { SharedValue } from "react-native-reanimated";
3
2
 
4
- import type { Node } from "../dom/types";
5
3
  import type { SkImage, SkPicture, SkRect, SkSize } from "../skia/types";
6
4
 
7
5
  export type NativeSkiaViewProps = ViewProps & {
@@ -9,6 +7,22 @@ export type NativeSkiaViewProps = ViewProps & {
9
7
  opaque?: boolean;
10
8
  };
11
9
 
10
+ export type AndroidSurfaceType = "SurfaceView" | "TextureView";
11
+
12
+ export interface AndroidCanvasProps {
13
+ /**
14
+ * Backing view. Defaults to `SurfaceView` when the canvas is `opaque` and to
15
+ * `TextureView` otherwise; both composite correctly in React Native stacking
16
+ * order without further flags.
17
+ */
18
+ surfaceType?: AndroidSurfaceType;
19
+ /**
20
+ * SurfaceView only: composite above every React Native view in the window,
21
+ * ignoring `zIndex`. Ignored for TextureView. Defaults to false.
22
+ */
23
+ zOrderOnTop?: boolean;
24
+ }
25
+
12
26
  export interface ISkiaViewApi {
13
27
  web?: boolean;
14
28
  setJsiProperty: <T>(nativeId: number, name: string, value: T) => void;
@@ -24,12 +38,12 @@ export interface SkiaBaseViewProps extends ViewProps {
24
38
  * average time it takes to render.
25
39
  */
26
40
  debug?: boolean;
41
+
27
42
  /**
28
- * Pass an animated value to the onSize property to get updates when
29
- * the Skia view is resized.
43
+ * Declares that the canvas covers every pixel of its bounds. On Android an
44
+ * opaque canvas is backed by a `SurfaceView` by default, the cheapest path
45
+ * (see `android.surfaceType`). Defaults to false.
30
46
  */
31
- onSize?: SharedValue<SkSize>;
32
-
33
47
  opaque?: boolean;
34
48
 
35
49
  /**
@@ -39,6 +53,9 @@ export interface SkiaBaseViewProps extends ViewProps {
39
53
  */
40
54
  highBitDepth?: boolean;
41
55
 
56
+ /** Android-only rendering options. Ignored on iOS and web. */
57
+ android?: AndroidCanvasProps;
58
+
42
59
  // On web, only 16 WebGL contextes are allowed. If the drawing is non-animated, set
43
60
  // __destroyWebGLContextAfterRender to true to release the context after each draw.
44
61
  __destroyWebGLContextAfterRender?: boolean;
@@ -48,7 +65,3 @@ export interface SkiaPictureViewNativeProps extends SkiaBaseViewProps {
48
65
  picture?: SkPicture;
49
66
  androidWarmup?: boolean;
50
67
  }
51
-
52
- export interface SkiaDomViewNativeProps extends SkiaBaseViewProps {
53
- root?: Node<unknown>;
54
- }
@@ -1,11 +0,0 @@
1
- package com.shopify.reactnative.skia;
2
-
3
- import com.facebook.react.bridge.ReactContext;
4
- import com.facebook.react.bridge.RuntimeExecutor;
5
-
6
- /* package */ final class ReactNativeCompatible {
7
- public static RuntimeExecutor getRuntimeExecutor(ReactContext context) {
8
- return context.getCatalystInstance().getRuntimeExecutor();
9
- }
10
- }
11
-
@@ -1,79 +0,0 @@
1
- // RnskiaPackage.java
2
-
3
- package com.shopify.reactnative.skia;
4
-
5
- import androidx.annotation.Nullable;
6
-
7
- import java.util.Arrays;
8
- import java.util.HashMap;
9
- import java.util.List;
10
- import java.util.Map;
11
-
12
- import com.facebook.react.TurboReactPackage;
13
- import com.facebook.react.bridge.NativeModule;
14
- import com.facebook.react.bridge.ReactApplicationContext;
15
- import com.facebook.react.module.annotations.ReactModule;
16
- import com.facebook.react.module.annotations.ReactModuleList;
17
- import com.facebook.react.module.model.ReactModuleInfo;
18
- import com.facebook.react.module.model.ReactModuleInfoProvider;
19
- import com.facebook.react.turbomodule.core.interfaces.TurboModule;
20
- import com.facebook.react.uimanager.ViewManager;
21
-
22
- @ReactModuleList(
23
- nativeModules = {
24
- RNSkiaModule.class,
25
- })
26
- public class RNSkiaPackage extends TurboReactPackage {
27
- @Override
28
- public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
29
- return Arrays.<NativeModule>asList(new RNSkiaModule(reactContext));
30
- }
31
-
32
- @Nullable
33
- @Override
34
- public NativeModule getModule(String s, ReactApplicationContext reactApplicationContext) {
35
- switch (s) {
36
- case RNSkiaModule.NAME:
37
- return new RNSkiaModule(reactApplicationContext);
38
- default:
39
- return null;
40
- }
41
- }
42
-
43
- @Override
44
- public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
45
- return Arrays.<ViewManager>asList(
46
- new SkiaPictureViewManager()
47
- );
48
- }
49
-
50
- @Override
51
- public ReactModuleInfoProvider getReactModuleInfoProvider() {
52
- return new ReactModuleInfoProvider() {
53
- @Override
54
- public Map<String, ReactModuleInfo> getReactModuleInfos() {
55
- final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
56
- Class<? extends NativeModule>[] moduleList =
57
- new Class[] {
58
- RNSkiaModule.class,
59
- };
60
-
61
- for (Class<? extends NativeModule> moduleClass : moduleList) {
62
- ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class);
63
-
64
- reactModuleInfoMap.put(
65
- reactModule.name(),
66
- new ReactModuleInfo(
67
- reactModule.name(),
68
- moduleClass.getName(),
69
- reactModule.canOverrideExistingModule(),
70
- reactModule.needsEagerInit(),
71
- reactModule.hasConstants(),
72
- reactModule.isCxxModule(),
73
- TurboModule.class.isAssignableFrom(moduleClass)));
74
- }
75
- return reactModuleInfoMap;
76
- }
77
- };
78
- }
79
- }
@@ -1,113 +0,0 @@
1
- package com.shopify.reactnative.skia;
2
-
3
-
4
- import android.annotation.SuppressLint;
5
- import android.content.Context;
6
- import android.graphics.Bitmap;
7
- import android.graphics.Canvas;
8
- import android.graphics.Matrix;
9
- import android.graphics.PixelFormat;
10
- import android.hardware.HardwareBuffer;
11
- import android.media.Image;
12
- import android.media.ImageReader;
13
- import android.os.Build;
14
- import android.util.Log;
15
- import android.view.View;
16
-
17
- import androidx.annotation.NonNull;
18
- import androidx.annotation.RequiresApi;
19
-
20
- @SuppressLint("ViewConstructor")
21
- @RequiresApi(api = Build.VERSION_CODES.Q)
22
- public class SkiaAHBView extends View implements ImageReader.OnImageAvailableListener {
23
-
24
- private ImageReader mReader;
25
-
26
- private Bitmap mBitmap = null;
27
-
28
- private final Matrix matrix = new Matrix();
29
-
30
- SkiaViewAPI mApi;
31
- boolean mDebug;
32
-
33
- public SkiaAHBView(Context context, SkiaViewAPI api, boolean debug) {
34
- super(context);
35
- mApi = api;
36
- mDebug = debug;
37
- }
38
-
39
- private ImageReader createReader() {
40
- ImageReader reader = ImageReader.newInstance(getWidth(), getHeight(), PixelFormat.RGBA_8888, 2, HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE |
41
- HardwareBuffer.USAGE_GPU_COLOR_OUTPUT);
42
- reader.setOnImageAvailableListener(this, null);
43
- return reader;
44
- }
45
-
46
- @Override
47
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
48
- super.onLayout(changed, left, top, right, bottom);
49
- int width = getWidth();
50
- int height = getHeight();
51
- if (mReader == null) {
52
- mReader = createReader();
53
- mApi.onSurfaceCreated(mReader.getSurface(), width, height);
54
- } else {
55
- mReader = createReader();
56
- mApi.onSurfaceChanged(mReader.getSurface(), width, height);
57
- }
58
- }
59
-
60
- @Override
61
- public void onImageAvailable(ImageReader reader) {
62
- try (Image image = reader.acquireLatestImage()) {
63
- if (image != null) {
64
- HardwareBuffer hb = image.getHardwareBuffer();
65
- if (mDebug) {
66
- textureUpdated(image.getTimestamp());
67
- }
68
- if (hb != null) {
69
- Bitmap bitmap = Bitmap.wrapHardwareBuffer(hb, null);
70
- if (bitmap != null) {
71
- mBitmap = bitmap;
72
- hb.close();
73
- invalidate();
74
- }
75
- }
76
- }
77
- }
78
- }
79
-
80
- @Override
81
- protected void onDraw(@NonNull Canvas canvas) {
82
- super.onDraw(canvas);
83
- if (mBitmap != null) {
84
- float viewWidth = getWidth();
85
- float viewHeight = getHeight();
86
- float bitmapWidth = mBitmap.getWidth();
87
- float bitmapHeight = mBitmap.getHeight();
88
-
89
- // Calculate the scale factors
90
- float scaleX = viewWidth / bitmapWidth;
91
- float scaleY = viewHeight / bitmapHeight;
92
-
93
- // Reset the matrix and apply scaling
94
- matrix.reset();
95
- matrix.setScale(scaleX, scaleY);
96
-
97
- canvas.drawBitmap(mBitmap, matrix, null);
98
- }
99
- }
100
-
101
- private long _prevTimestamp = 0;
102
- public void textureUpdated(long ts) {
103
- long frameDuration = (ts - _prevTimestamp)/1000000;
104
- Log.i("SkiaAHBView", "onSurfaceTextureUpdated "+frameDuration+"ms");
105
- _prevTimestamp = ts;
106
- }
107
-
108
- @Override
109
- protected void onDetachedFromWindow() {
110
- super.onDetachedFromWindow();
111
- mApi.onSurfaceDestroyed();
112
- }
113
- }