@shopify/react-native-skia 1.5.2 → 1.5.4

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 (87) hide show
  1. package/android/CMakeLists.txt +52 -13
  2. package/android/build.gradle +3 -0
  3. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +18 -1
  4. package/android/cpp/rnskia-android/RNSkAndroidVideo.cpp +12 -1
  5. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +13 -0
  6. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +0 -1
  7. package/cpp/api/JsiSkImage.h +15 -2
  8. package/cpp/api/JsiSkSurface.h +18 -3
  9. package/cpp/rnskia/DawnContext.h +241 -0
  10. package/cpp/rnskia/DawnUtils.h +127 -0
  11. package/cpp/rnskia/DawnWindowContext.cpp +19 -0
  12. package/cpp/rnskia/DawnWindowContext.h +77 -0
  13. package/cpp/rnskia/ImageProvider.h +100 -0
  14. package/cpp/rnskia/RNSkView.h +6 -0
  15. package/cpp/skia/include/android/SkAnimatedImage.h +14 -0
  16. package/cpp/skia/include/codec/SkAvifDecoder.h +39 -3
  17. package/cpp/skia/include/codec/SkCodec.h +16 -0
  18. package/cpp/skia/include/config/SkUserConfig.h +3 -2
  19. package/cpp/skia/include/core/SkCanvas.h +55 -10
  20. package/cpp/skia/include/core/SkContourMeasure.h +76 -0
  21. package/cpp/skia/include/core/SkFontScanner.h +48 -0
  22. package/cpp/skia/include/core/SkMilestone.h +1 -1
  23. package/cpp/skia/include/core/SkPath.h +11 -0
  24. package/cpp/skia/include/core/SkPathEffect.h +0 -23
  25. package/cpp/skia/include/core/SkRect.h +2 -0
  26. package/cpp/skia/include/core/SkString.h +4 -0
  27. package/cpp/skia/include/core/SkTypeface.h +5 -0
  28. package/cpp/skia/include/core/SkTypes.h +8 -3
  29. package/cpp/skia/include/docs/SkPDFDocument.h +3 -3
  30. package/cpp/skia/include/effects/SkOverdrawColorFilter.h +1 -0
  31. package/cpp/skia/include/effects/SkRuntimeEffect.h +2 -1
  32. package/cpp/skia/include/encode/SkJpegEncoder.h +4 -0
  33. package/cpp/skia/include/gpu/ganesh/GrContextOptions.h +8 -1
  34. package/cpp/skia/include/gpu/ganesh/GrDirectContext.h +18 -3
  35. package/cpp/skia/include/gpu/ganesh/GrTypes.h +11 -0
  36. package/cpp/skia/include/gpu/ganesh/gl/GrGLFunctions.h +5 -1
  37. package/cpp/skia/include/gpu/ganesh/gl/GrGLInterface.h +0 -1
  38. package/cpp/skia/include/gpu/graphite/GraphiteTypes.h +8 -0
  39. package/cpp/skia/include/gpu/graphite/Recorder.h +0 -2
  40. package/cpp/skia/include/gpu/graphite/precompile/PaintOptions.h +7 -4
  41. package/cpp/skia/include/gpu/graphite/precompile/PrecompileImageFilter.h +2 -0
  42. package/cpp/skia/include/gpu/graphite/precompile/PrecompileMaskFilter.h +2 -0
  43. package/cpp/skia/include/ports/SkFontMgr_android_ndk.h +25 -0
  44. package/cpp/skia/include/ports/SkFontMgr_fontconfig.h +4 -2
  45. package/cpp/skia/include/ports/SkFontScanner_Fontations.h +15 -0
  46. package/cpp/skia/include/ports/SkFontScanner_FreeType.h +15 -0
  47. package/cpp/skia/include/private/SkExif.h +14 -0
  48. package/cpp/skia/include/private/SkPathRef.h +36 -0
  49. package/cpp/skia/include/private/base/SkFeatures.h +4 -0
  50. package/cpp/skia/include/private/base/SkSpan_impl.h +1 -3
  51. package/cpp/skia/modules/skcms/src/skcms_internals.h +3 -0
  52. package/cpp/skia/src/base/SkMathPriv.h +1 -8
  53. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +1 -2
  54. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.mm +25 -19
  55. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +23 -0
  56. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +23 -9
  57. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +0 -20
  58. package/libs/android/arm64-v8a/libskia.a +0 -0
  59. package/libs/android/arm64-v8a/libskottie.a +0 -0
  60. package/libs/android/armeabi-v7a/libskia.a +0 -0
  61. package/libs/android/armeabi-v7a/libskottie.a +0 -0
  62. package/libs/android/x86/libskia.a +0 -0
  63. package/libs/android/x86/libskottie.a +0 -0
  64. package/libs/android/x86_64/libskia.a +0 -0
  65. package/libs/android/x86_64/libskottie.a +0 -0
  66. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  67. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  68. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
  69. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
  70. package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e/libskparagraph.a +0 -0
  71. package/libs/ios/libskparagraph.xcframework/ios-arm64_arm64e_x86_64-simulator/libskparagraph.a +0 -0
  72. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
  73. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
  74. package/libs/ios/libskshaper.xcframework/Info.plist +5 -5
  75. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  76. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  77. package/libs/ios/libskunicode_core.xcframework/Info.plist +5 -5
  78. package/libs/ios/libskunicode_core.xcframework/ios-arm64_arm64e/libskunicode_core.a +0 -0
  79. package/libs/ios/libskunicode_core.xcframework/ios-arm64_arm64e_x86_64-simulator/libskunicode_core.a +0 -0
  80. package/libs/ios/libskunicode_libgrapheme.xcframework/Info.plist +5 -5
  81. package/libs/ios/libskunicode_libgrapheme.xcframework/ios-arm64_arm64e/libskunicode_libgrapheme.a +0 -0
  82. package/libs/ios/libskunicode_libgrapheme.xcframework/ios-arm64_arm64e_x86_64-simulator/libskunicode_libgrapheme.a +0 -0
  83. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  84. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  85. package/package.json +1 -1
  86. package/react-native-skia.podspec +35 -9
  87. package/src/renderer/__tests__/e2e/SVG.spec.tsx +2 -1
@@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.4.1)
3
3
 
4
4
  set (CMAKE_VERBOSE_MAKEFILE ON)
5
5
  set (CMAKE_CXX_STANDARD 17)
6
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSK_GL -DSK_GANESH -DSK_BUILD_FOR_ANDROID -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")
7
-
6
+ set(SK_GRAPHITE OFF)
7
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSK_BUILD_FOR_ANDROID -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")
8
8
  set (PACKAGE_NAME "rnskia")
9
9
  set (SKIA_LIB "skia")
10
10
  set (SKIA_SVG_LIB "svg")
@@ -13,6 +13,7 @@ set (SKIA_SKPARAGRAPH_LIB "skparagraph")
13
13
  set (SKIA_SKUNICODE_CORE_LIB "skunicode_core")
14
14
  set (SKIA_SKUNICODE_ICU_LIB "skunicode_icu")
15
15
 
16
+
16
17
  # Clear some variables
17
18
  unset(LIBRN_DIR CACHE)
18
19
  unset(libfbjni_link_DIRS CACHE)
@@ -30,6 +31,34 @@ message("-- LIBRN : " ${LIBRN_DIR})
30
31
 
31
32
  link_directories(../libs/android/${ANDROID_ABI}/)
32
33
 
34
+ # Import prebuilt SKIA libraries
35
+ set (SKIA_LIBS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/${ANDROID_ABI}")
36
+
37
+ if(SK_GRAPHITE)
38
+ add_definitions(-DSK_GRAPHITE)
39
+ set(DAWN_NATIVE_LIB "libdawn_native_static")
40
+ set(DAWN_PLATFORM_LIB "libdawn_platform_static")
41
+ set(DAWN_PROC_LIB "libdawn_proc_static")
42
+ set(BACKEND_SOURCES
43
+ #TODO: is this source needed to be added?
44
+ "${PROJECT_SOURCE_DIR}/../cpp/rnskia/DawnWindowContext.cpp"
45
+ )
46
+ add_library(libdawn_native_static STATIC IMPORTED)
47
+ set_property(TARGET libdawn_native_static PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libdawn_native_static.a")
48
+
49
+ add_library(libdawn_platform_static STATIC IMPORTED)
50
+ set_property(TARGET libdawn_platform_static PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libdawn_platform_static.a")
51
+
52
+ add_library(libdawn_proc_static STATIC IMPORTED)
53
+ set_property(TARGET libdawn_proc_static PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libdawn_proc_static.a")
54
+ else()
55
+ add_definitions(-DSK_GL -DSK_GANESH)
56
+ set(BACKEND_SOURCES
57
+ "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/SkiaOpenGLSurfaceFactory.cpp"
58
+ "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/GrAHardwareBufferUtils.cpp"
59
+ )
60
+ endif()
61
+
33
62
  if(${REACT_NATIVE_VERSION} LESS 66)
34
63
  file(
35
64
  TO_CMAKE_PATH
@@ -46,8 +75,6 @@ add_library(
46
75
 
47
76
  "${PROJECT_SOURCE_DIR}/cpp/jni/JniPlatformContext.cpp"
48
77
  "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp"
49
- "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/SkiaOpenGLSurfaceFactory.cpp"
50
- "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/GrAHardwareBufferUtils.cpp"
51
78
  "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/AHardwareBufferUtils.cpp"
52
79
  "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/RNSkAndroidVideo.cpp"
53
80
 
@@ -66,9 +93,10 @@ add_library(
66
93
 
67
94
  "${PROJECT_SOURCE_DIR}/../cpp/api/third_party/CSSColorParser.cpp"
68
95
  "${PROJECT_SOURCE_DIR}/../cpp/api/third_party/base64.cpp"
69
-
96
+ ${BACKEND_SOURCES}
70
97
  )
71
98
 
99
+
72
100
  target_include_directories(
73
101
  ${PACKAGE_NAME}
74
102
  PRIVATE
@@ -88,6 +116,7 @@ target_include_directories(
88
116
  # so "include/core/SkRef.h" instead of "SkRef.h", as otherwise
89
117
  # the prefab cannot be shipped.
90
118
  ../cpp/skia
119
+ ../cpp/dawn/include
91
120
 
92
121
  ../cpp/api
93
122
  ../cpp/jsi
@@ -102,9 +131,6 @@ target_include_directories(
102
131
  ${libfbjni_include_DIRS}
103
132
  )
104
133
 
105
- # Import prebuilt SKIA libraries
106
- set (SKIA_LIBS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../libs/android/${ANDROID_ABI}")
107
-
108
134
  add_library(skia STATIC IMPORTED)
109
135
  set_property(TARGET skia PROPERTY IMPORTED_LOCATION "${SKIA_LIBS_PATH}/libskia.a")
110
136
 
@@ -240,8 +266,7 @@ add_definitions(-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION})
240
266
 
241
267
  # Link
242
268
  if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76)
243
- target_link_libraries(
244
- ${PACKAGE_NAME}
269
+ set(COMMON_LIBS
245
270
  ${LOG_LIB}
246
271
  ${REACT_LIB}
247
272
  ${FBJNI_LIBRARY}
@@ -258,8 +283,7 @@ if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76)
258
283
  -landroid
259
284
  )
260
285
  else()
261
- target_link_libraries(
262
- ${PACKAGE_NAME}
286
+ set(COMMON_LIBS
263
287
  ${LOG_LIB}
264
288
  ${FBJNI_LIBRARY}
265
289
  ${REACT_LIB}
@@ -278,4 +302,19 @@ else()
278
302
  -lEGL
279
303
  -landroid
280
304
  )
281
- endif()
305
+ endif()
306
+
307
+ if(SK_GRAPHITE)
308
+ target_link_libraries(${PACKAGE_NAME}
309
+ ${COMMON_LIBS}
310
+ ${DAWN_NATIVE_LIB}
311
+ ${DAWN_PLATFORM_LIB}
312
+ ${DAWN_PROC_LIB}
313
+ )
314
+ else()
315
+ target_link_libraries(${PACKAGE_NAME}
316
+ ${COMMON_LIBS}
317
+ -lGLESv2
318
+ -lEGL
319
+ )
320
+ endif()
@@ -330,6 +330,9 @@ if (ENABLE_PREFAB) {
330
330
  } else if (path.contains("skia/include") || path.contains("skia/modules") || path.contains("skia/src")) {
331
331
  // Skip flattening Skia dir
332
332
  path = path.substring("skia/".length())
333
+ } else if (path.startsWith("dawn/include/")) {
334
+ // Flatten dawn/include/dawn and dawn/include/webgpu
335
+ path = path.substring("dawn/include/".length())
333
336
  } else {
334
337
  // flatten anything else
335
338
  path = path.substring(path.lastIndexOf("/") + 1)
@@ -9,9 +9,14 @@
9
9
  #include <memory>
10
10
  #include <string>
11
11
 
12
+ #if defined(SK_GRAPHITE)
13
+ #include "DawnContext.h"
14
+ #else
15
+ #include "OpenGLContext.h"
16
+ #endif
17
+
12
18
  #include "AHardwareBufferUtils.h"
13
19
  #include "JniPlatformContext.h"
14
- #include "OpenGLContext.h"
15
20
  #include "RNSkAndroidVideo.h"
16
21
  #include "RNSkPlatformContext.h"
17
22
 
@@ -51,17 +56,29 @@ public:
51
56
  }
52
57
 
53
58
  sk_sp<SkSurface> makeOffscreenSurface(int width, int height) override {
59
+ #if defined(SK_GRAPHITE)
60
+ return DawnContext::getInstance().MakeOffscreen(width, height);
61
+ #else
54
62
  return OpenGLContext::getInstance().MakeOffscreen(width, height);
63
+ #endif
55
64
  }
56
65
 
57
66
  std::shared_ptr<WindowContext>
58
67
  makeContextFromNativeSurface(void *surface, int width, int height) override {
68
+ #if defined(SK_GRAPHITE)
69
+ return DawnContext::getInstance().MakeWindow(surface, width, height);
70
+ #else
59
71
  return OpenGLContext::getInstance().MakeWindow(
60
72
  reinterpret_cast<ANativeWindow *>(surface), width, height);
73
+ #endif
61
74
  }
62
75
 
63
76
  sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) override {
77
+ #if defined(SK_GRAPHITE)
78
+ return DawnContext::getInstance().MakeImageFromBuffer(buffer);
79
+ #else
64
80
  return OpenGLContext::getInstance().MakeImageFromBuffer(buffer);
81
+ #endif
65
82
  }
66
83
 
67
84
  std::shared_ptr<RNSkVideo> createVideo(const std::string &url) override {
@@ -5,6 +5,14 @@
5
5
  #include <android/hardware_buffer_jni.h>
6
6
  #endif
7
7
 
8
+ #include "RNSkLog.h"
9
+
10
+ #if defined(SK_GRAPHITE)
11
+ #include "DawnContext.h"
12
+ #else
13
+ #include "OpenGLContext.h"
14
+ #endif
15
+
8
16
  #pragma clang diagnostic push
9
17
  #pragma clang diagnostic ignored "-Wdocumentation"
10
18
 
@@ -13,7 +21,6 @@
13
21
 
14
22
  #pragma clang diagnostic pop
15
23
 
16
- #include "OpenGLContext.h"
17
24
  #include "RNSkAndroidVideo.h"
18
25
 
19
26
  namespace RNSkia {
@@ -52,7 +59,11 @@ sk_sp<SkImage> RNSkAndroidVideo::nextImage(double *timeStamp) {
52
59
  // Convert jobject to AHardwareBuffer
53
60
  AHardwareBuffer *buffer =
54
61
  AHardwareBuffer_fromHardwareBuffer(env, jHardwareBuffer);
62
+ #if defined(SK_GRAPHITE)
63
+ return DawnContext::getInstance().MakeImageFromBuffer(buffer);
64
+ #else
55
65
  return OpenGLContext::getInstance().MakeImageFromBuffer(buffer);
66
+ #endif
56
67
  #else
57
68
  return nullptr;
58
69
  #endif
@@ -1,8 +1,17 @@
1
1
  #include "RNSkOpenGLCanvasProvider.h"
2
2
 
3
+ #include <android/native_window_jni.h>
4
+ #include <fbjni/fbjni.h>
5
+ #include <jni.h>
3
6
  #include <memory>
4
7
 
8
+ #include "RNSkLog.h"
9
+
10
+ #if defined(SK_GRAPHITE)
11
+ #include "DawnContext.h"
12
+ #else
5
13
  #include "OpenGLContext.h"
14
+ #endif
6
15
 
7
16
  #pragma clang diagnostic push
8
17
  #pragma clang diagnostic ignored "-Wdocumentation"
@@ -89,8 +98,12 @@ void RNSkOpenGLCanvasProvider::surfaceAvailable(jobject jSurfaceTexture,
89
98
  env->DeleteLocalRef(jSurface);
90
99
  env->DeleteLocalRef(surfaceClass);
91
100
  env->DeleteLocalRef(surfaceTextureClass);
101
+ #if defined(SK_GRAPHITE)
102
+ _surfaceHolder = DawnContext::getInstance().MakeWindow(window, width, height);
103
+ #else
92
104
  _surfaceHolder =
93
105
  OpenGLContext::getInstance().MakeWindow(window, width, height);
106
+ #endif
94
107
 
95
108
  // Post redraw request to ensure we paint in the next draw cycle.
96
109
  _requestRedraw();
@@ -3,7 +3,6 @@ package com.shopify.reactnative.skia;
3
3
  import android.content.Context;
4
4
  import android.graphics.SurfaceTexture;
5
5
  import android.util.Log;
6
- import android.view.MotionEvent;
7
6
  import android.view.TextureView;
8
7
 
9
8
  import com.facebook.react.views.view.ReactViewGroup;
@@ -12,6 +12,11 @@
12
12
 
13
13
  #include "RNSkTypedArray.h"
14
14
 
15
+ #if defined(SK_GRAPHITE)
16
+ #include "DawnContext.h"
17
+ #include "include/gpu/graphite/Context.h"
18
+ #endif
19
+
15
20
  #pragma clang diagnostic push
16
21
  #pragma clang diagnostic ignored "-Wdocumentation"
17
22
 
@@ -83,9 +88,13 @@ public:
83
88
  ? arguments[1].asNumber()
84
89
  : 100.0;
85
90
  auto image = getObject();
91
+ #if defined(SK_GRAPHITE)
92
+ image = DawnContext::getInstance().MakeRasterImage(image);
93
+ #else
86
94
  if (image->isTextureBacked()) {
87
95
  image = image->makeNonTextureImage();
88
96
  }
97
+ #endif
89
98
  sk_sp<SkData> data;
90
99
 
91
100
  if (format == SkEncodedImageFormat::kJPEG) {
@@ -181,9 +190,13 @@ public:
181
190
  }
182
191
 
183
192
  JSI_HOST_FUNCTION(makeNonTextureImage) {
184
- auto image = getObject()->makeNonTextureImage();
193
+ #if defined(SK_GRAPHITE)
194
+ auto rasterImage = DawnContext::getInstance().MakeRasterImage(getObject());
195
+ #else
196
+ auto rasterImage = getObject()->makeNonTextureImage();
197
+ #endif
185
198
  return jsi::Object::createFromHostObject(
186
- runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
199
+ runtime, std::make_shared<JsiSkImage>(getContext(), rasterImage));
187
200
  }
188
201
 
189
202
  EXPORT_JSI_API_TYPENAME(JsiSkImage, Image)
@@ -10,6 +10,10 @@
10
10
  #include "JsiSkCanvas.h"
11
11
  #include "JsiSkImage.h"
12
12
 
13
+ #if defined(SK_GRAPHITE)
14
+ #include "DawnContext.h"
15
+ #endif
16
+
13
17
  #pragma clang diagnostic push
14
18
  #pragma clang diagnostic ignored "-Wdocumentation"
15
19
 
@@ -44,21 +48,32 @@ public:
44
48
  }
45
49
 
46
50
  JSI_HOST_FUNCTION(flush) {
47
- if (auto dContext = GrAsDirectContext(getObject()->recordingContext())) {
51
+ auto surface = getObject();
52
+ #if defined(SK_GRAPHITE)
53
+ auto recording = surface->recorder()->snap();
54
+ DawnContext::getInstance().submitRecording(recording.get());
55
+ #else
56
+ if (auto dContext = GrAsDirectContext(surface->recordingContext())) {
48
57
  dContext->flushAndSubmit();
49
58
  }
59
+ #endif
50
60
  return jsi::Value::undefined();
51
61
  }
52
62
 
53
63
  JSI_HOST_FUNCTION(makeImageSnapshot) {
64
+ auto surface = getObject();
54
65
  sk_sp<SkImage> image;
55
66
  if (count == 1) {
56
67
  auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
57
- image = getObject()->makeImageSnapshot(SkIRect::MakeXYWH(
68
+ image = surface->makeImageSnapshot(SkIRect::MakeXYWH(
58
69
  rect->x(), rect->y(), rect->width(), rect->height()));
59
70
  } else {
60
- image = getObject()->makeImageSnapshot();
71
+ image = surface->makeImageSnapshot();
61
72
  }
73
+ #if defined(SK_GRAPHITE)
74
+ auto recording = surface->recorder()->snap();
75
+ DawnContext::getInstance().submitRecording(recording.get());
76
+ #endif
62
77
  return jsi::Object::createFromHostObject(
63
78
  runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
64
79
  }
@@ -0,0 +1,241 @@
1
+ #pragma once
2
+
3
+ #include <memory>
4
+ #include <mutex>
5
+
6
+ #include "DawnUtils.h"
7
+ #include "DawnWindowContext.h"
8
+ #include "ImageProvider.h"
9
+
10
+ #include "include/core/SkData.h"
11
+ #include "include/gpu/graphite/BackendTexture.h"
12
+ #include "include/gpu/graphite/Context.h"
13
+ #include "include/gpu/graphite/ContextOptions.h"
14
+ #include "include/gpu/graphite/GraphiteTypes.h"
15
+ #include "include/gpu/graphite/Recorder.h"
16
+ #include "include/gpu/graphite/Recording.h"
17
+ #include "include/gpu/graphite/Surface.h"
18
+ #include "include/gpu/graphite/dawn/DawnBackendContext.h"
19
+ #include "include/gpu/graphite/dawn/DawnTypes.h"
20
+ #include "include/gpu/graphite/dawn/DawnUtils.h"
21
+
22
+ #include "src/gpu/graphite/ContextOptionsPriv.h"
23
+
24
+ #ifdef __APPLE__
25
+ #include <CoreVideo/CVPixelBuffer.h>
26
+ #else
27
+ #include <android/hardware_buffer.h>
28
+ #include <android/hardware_buffer_jni.h>
29
+ #endif
30
+
31
+ namespace RNSkia {
32
+
33
+ struct AsyncContext {
34
+ bool fCalled = false;
35
+ std::unique_ptr<const SkSurface::AsyncReadResult> fResult;
36
+ };
37
+
38
+ struct SharedTextureContext {
39
+ wgpu::SharedTextureMemory sharedTextureMemory;
40
+ wgpu::Texture texture;
41
+ };
42
+
43
+ static void
44
+ async_callback(void *c,
45
+ std::unique_ptr<const SkImage::AsyncReadResult> result) {
46
+ auto context = static_cast<AsyncContext *>(c);
47
+ context->fResult = std::move(result);
48
+ context->fCalled = true;
49
+ }
50
+
51
+ class DawnContext {
52
+ public:
53
+ // TODO: remove
54
+ friend class RNSkiOSPlatformContext;
55
+
56
+ DawnContext(const DawnContext &) = delete;
57
+ DawnContext &operator=(const DawnContext &) = delete;
58
+
59
+ static DawnContext &getInstance() {
60
+ static DawnContext instance;
61
+ return instance;
62
+ }
63
+
64
+ sk_sp<SkImage> MakeRasterImage(sk_sp<SkImage> image) {
65
+ if (!image->isTextureBacked()) {
66
+ return image;
67
+ }
68
+ std::lock_guard<std::mutex> lock(_mutex);
69
+ AsyncContext asyncContext;
70
+ fGraphiteContext->asyncRescaleAndReadPixels(
71
+ image.get(), image->imageInfo(), image->imageInfo().bounds(),
72
+ SkImage::RescaleGamma::kSrc, SkImage::RescaleMode::kNearest,
73
+ async_callback, &asyncContext);
74
+ fGraphiteContext->submit();
75
+ while (!asyncContext.fCalled) {
76
+ tick();
77
+ fGraphiteContext->checkAsyncWorkCompletion();
78
+ }
79
+ auto bytesPerRow = asyncContext.fResult->rowBytes(0);
80
+ auto bufferSize = bytesPerRow * image->imageInfo().height();
81
+ auto data = SkData::MakeWithProc(
82
+ asyncContext.fResult->data(0), bufferSize,
83
+ [](const void *ptr, void *context) {
84
+ auto *result =
85
+ reinterpret_cast<const SkSurface::AsyncReadResult *>(context);
86
+ delete result;
87
+ },
88
+ reinterpret_cast<void *>(const_cast<SkSurface::AsyncReadResult *>(
89
+ asyncContext.fResult.release())));
90
+ auto rasterImage =
91
+ SkImages::RasterFromData(image->imageInfo(), data, bytesPerRow);
92
+ return rasterImage;
93
+ }
94
+
95
+ void submitRecording(
96
+ skgpu::graphite::Recording *recording,
97
+ skgpu::graphite::SyncToCpu syncToCpu = skgpu::graphite::SyncToCpu::kNo) {
98
+ std::lock_guard<std::mutex> lock(_mutex);
99
+ skgpu::graphite::InsertRecordingInfo info;
100
+ info.fRecording = recording;
101
+ fGraphiteContext->insertRecording(info);
102
+ fGraphiteContext->submit(syncToCpu);
103
+ }
104
+
105
+ sk_sp<SkImage> MakeImageFromBuffer(void *buffer) {
106
+ #ifdef __APPLE__
107
+ wgpu::SharedTextureMemoryIOSurfaceDescriptor platformDesc;
108
+ auto ioSurface = CVPixelBufferGetIOSurface((CVPixelBufferRef)buffer);
109
+ platformDesc.ioSurface = ioSurface;
110
+ int width = static_cast<int>(IOSurfaceGetWidth(ioSurface));
111
+ int height = static_cast<int>(IOSurfaceGetHeight(ioSurface));
112
+ #else
113
+ wgpu::SharedTextureMemoryAHardwareBufferDescriptor platformDesc;
114
+ auto ahb = (AHardwareBuffer *)buffer;
115
+ platformDesc.handle = ahb;
116
+ platformDesc.useExternalFormat = true;
117
+ AHardwareBuffer_Desc adesc;
118
+ AHardwareBuffer_describe(ahb, &adesc);
119
+ int width = adesc.width;
120
+ int height = adesc.height;
121
+ #endif
122
+
123
+ wgpu::SharedTextureMemoryDescriptor desc = {};
124
+ desc.nextInChain = &platformDesc;
125
+ wgpu::SharedTextureMemory memory =
126
+ backendContext.fDevice.ImportSharedTextureMemory(&desc);
127
+
128
+ wgpu::TextureDescriptor textureDesc;
129
+ textureDesc.format = DawnUtils::PreferredTextureFormat;
130
+ textureDesc.dimension = wgpu::TextureDimension::e2D;
131
+ textureDesc.usage =
132
+ wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopySrc;
133
+ textureDesc.size = {static_cast<uint32_t>(width),
134
+ static_cast<uint32_t>(height), 1};
135
+
136
+ wgpu::Texture texture = memory.CreateTexture(&textureDesc);
137
+
138
+ wgpu::SharedTextureMemoryBeginAccessDescriptor beginAccessDesc;
139
+ beginAccessDesc.initialized = true;
140
+ beginAccessDesc.fenceCount = 0;
141
+ bool success = memory.BeginAccess(texture, &beginAccessDesc);
142
+
143
+ if (success) {
144
+ skgpu::graphite::BackendTexture betFromView =
145
+ skgpu::graphite::BackendTextures::MakeDawn(texture.Get());
146
+ auto result = SkImages::WrapTexture(
147
+ getRecorder(), betFromView, DawnUtils::PreferedColorType,
148
+ kPremul_SkAlphaType, nullptr,
149
+ [](void *context) {
150
+ auto ctx = static_cast<SharedTextureContext *>(context);
151
+ wgpu::SharedTextureMemoryEndAccessState endState = {};
152
+ ctx->sharedTextureMemory.EndAccess(ctx->texture, &endState);
153
+ delete ctx;
154
+ },
155
+ new SharedTextureContext{memory, texture});
156
+ return result;
157
+ }
158
+ if (!success) {
159
+ return nullptr;
160
+ }
161
+ return nullptr;
162
+ }
163
+
164
+ // Create offscreen surface
165
+ sk_sp<SkSurface> MakeOffscreen(int width, int height) {
166
+ SkImageInfo info = SkImageInfo::Make(
167
+ width, height, DawnUtils::PreferedColorType, kPremul_SkAlphaType);
168
+ sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(getRecorder(), info);
169
+
170
+ if (!surface) {
171
+ throw std::runtime_error("Failed to create offscreen Skia surface.");
172
+ }
173
+
174
+ return surface;
175
+ }
176
+
177
+ // Create onscreen surface with window
178
+ std::unique_ptr<WindowContext> MakeWindow(void *window, int width,
179
+ int height) {
180
+ // 1. Create Surface
181
+ wgpu::SurfaceDescriptor surfaceDescriptor;
182
+ #ifdef __APPLE__
183
+ wgpu::SurfaceDescriptorFromMetalLayer metalSurfaceDesc;
184
+ metalSurfaceDesc.layer = window;
185
+ surfaceDescriptor.nextInChain = &metalSurfaceDesc;
186
+ #else
187
+ wgpu::SurfaceDescriptorFromAndroidNativeWindow androidSurfaceDesc;
188
+ androidSurfaceDesc.window = window;
189
+ surfaceDescriptor.nextInChain = &androidSurfaceDesc;
190
+ #endif
191
+ auto surface =
192
+ wgpu::Instance(instance->Get()).CreateSurface(&surfaceDescriptor);
193
+ return std::make_unique<DawnWindowContext>(
194
+ getRecorder(), backendContext.fDevice, surface, width, height);
195
+ }
196
+
197
+ private:
198
+ std::unique_ptr<dawn::native::Instance> instance;
199
+ std::unique_ptr<skgpu::graphite::Context> fGraphiteContext;
200
+ skgpu::graphite::DawnBackendContext backendContext;
201
+ std::mutex _mutex;
202
+
203
+ DawnContext() {
204
+ DawnProcTable backendProcs = dawn::native::GetProcs();
205
+ dawnProcSetProcs(&backendProcs);
206
+ WGPUInstanceDescriptor desc{};
207
+ desc.features.timedWaitAnyEnable = true;
208
+ instance = std::make_unique<dawn::native::Instance>(&desc);
209
+
210
+ backendContext = DawnUtils::createDawnBackendContext(instance.get());
211
+
212
+ skgpu::graphite::ContextOptions ctxOptions;
213
+ skgpu::graphite::ContextOptionsPriv contextOptionsPriv;
214
+ ctxOptions.fOptionsPriv = &contextOptionsPriv;
215
+ ctxOptions.fOptionsPriv->fStoreContextRefInRecorder = true;
216
+ fGraphiteContext =
217
+ skgpu::graphite::ContextFactory::MakeDawn(backendContext, ctxOptions);
218
+
219
+ if (!fGraphiteContext) {
220
+ throw std::runtime_error("Failed to create graphite context");
221
+ }
222
+ }
223
+
224
+ void tick() { backendContext.fTick(backendContext.fInstance); }
225
+
226
+ skgpu::graphite::Recorder *getRecorder() {
227
+ static thread_local skgpu::graphite::RecorderOptions recorderOptions;
228
+ if (!recorderOptions.fImageProvider) {
229
+ auto imageProvider = ImageProvider::Make();
230
+ recorderOptions.fImageProvider = imageProvider;
231
+ }
232
+ static thread_local auto recorder =
233
+ fGraphiteContext->makeRecorder(recorderOptions);
234
+ if (!recorder) {
235
+ throw std::runtime_error("Failed to create graphite context");
236
+ }
237
+ return recorder.get();
238
+ }
239
+ };
240
+
241
+ } // namespace RNSkia