@shopify/react-native-skia 1.5.10 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. package/android/cpp/jni/include/JniSkiaBaseView.h +6 -5
  2. package/android/cpp/jni/include/JniSkiaDomView.h +6 -4
  3. package/android/cpp/jni/include/JniSkiaManager.h +2 -2
  4. package/android/cpp/jni/include/JniSkiaPictureView.h +6 -4
  5. package/android/cpp/rnskia-android/MainThreadDispatcher.h +3 -0
  6. package/android/cpp/rnskia-android/OpenGLContext.h +44 -9
  7. package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +0 -1
  8. package/android/cpp/rnskia-android/OpenGLWindowContext.h +2 -2
  9. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +61 -3
  10. package/android/cpp/rnskia-android/RNSkAndroidView.h +10 -13
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +42 -36
  12. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +2 -2
  13. package/android/cpp/rnskia-android/gl/Display.h +2 -0
  14. package/android/src/main/java/com/shopify/reactnative/skia/SkiaAHBView.java +113 -0
  15. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +45 -54
  16. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseViewManager.java +5 -0
  17. package/android/src/main/java/com/shopify/reactnative/skia/SkiaDomView.java +2 -2
  18. package/android/src/main/java/com/shopify/reactnative/skia/SkiaPictureView.java +2 -2
  19. package/android/src/main/java/com/shopify/reactnative/skia/SkiaSurfaceView.java +42 -0
  20. package/android/src/main/java/com/shopify/reactnative/skia/SkiaTextureView.java +90 -0
  21. package/android/src/main/java/com/shopify/reactnative/skia/SkiaViewAPI.java +16 -0
  22. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDomViewManagerDelegate.java +3 -0
  23. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDomViewManagerInterface.java +1 -0
  24. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaPictureViewManagerDelegate.java +3 -1
  25. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaPictureViewManagerInterface.java +1 -0
  26. package/cpp/api/JsiSkContourMeasure.h +1 -5
  27. package/cpp/api/JsiSkImage.h +11 -0
  28. package/cpp/api/JsiSkImageFactory.h +14 -0
  29. package/cpp/api/JsiSkPicture.h +2 -0
  30. package/cpp/api/JsiSkSurface.h +7 -0
  31. package/cpp/api/JsiTextureInfo.h +53 -0
  32. package/cpp/jsi/ViewProperty.h +48 -0
  33. package/cpp/rnskia/RNSkDomView.cpp +0 -20
  34. package/cpp/rnskia/RNSkDomView.h +4 -9
  35. package/cpp/rnskia/RNSkJsiViewApi.h +3 -3
  36. package/cpp/rnskia/RNSkPictureView.h +11 -28
  37. package/cpp/rnskia/RNSkPlatformContext.h +18 -12
  38. package/cpp/rnskia/RNSkView.h +5 -29
  39. package/ios/RNSkia-iOS/MetalContext.h +101 -15
  40. package/ios/RNSkia-iOS/MetalContext.mm +9 -8
  41. package/ios/RNSkia-iOS/MetalWindowContext.h +39 -0
  42. package/ios/RNSkia-iOS/MetalWindowContext.mm +60 -0
  43. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +13 -25
  44. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +88 -2
  45. package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +9 -2
  46. package/ios/RNSkia-iOS/SkiaDomViewManager.mm +5 -0
  47. package/ios/RNSkia-iOS/SkiaManager.mm +1 -2
  48. package/ios/RNSkia-iOS/SkiaPictureView.mm +1 -0
  49. package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +5 -0
  50. package/ios/RNSkia-iOS/SkiaUIView.h +1 -0
  51. package/ios/RNSkia-iOS/SkiaUIView.mm +21 -14
  52. package/lib/commonjs/skia/types/Image/ColorType.d.ts +21 -0
  53. package/lib/commonjs/skia/types/Image/ColorType.js +29 -0
  54. package/lib/commonjs/skia/types/Image/ColorType.js.map +1 -0
  55. package/lib/commonjs/skia/types/Image/ColorType.web.d.ts +19 -0
  56. package/lib/commonjs/skia/types/Image/ColorType.web.js +27 -0
  57. package/lib/commonjs/skia/types/Image/ColorType.web.js.map +1 -0
  58. package/lib/commonjs/skia/types/Image/Image.d.ts +10 -0
  59. package/lib/commonjs/skia/types/Image/Image.js.map +1 -1
  60. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +19 -19
  61. package/lib/commonjs/skia/types/Image/ImageFactory.js +1 -21
  62. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  63. package/lib/commonjs/skia/types/Image/index.d.ts +1 -0
  64. package/lib/commonjs/skia/types/Image/index.js +11 -0
  65. package/lib/commonjs/skia/types/Image/index.js.map +1 -1
  66. package/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  67. package/lib/commonjs/skia/types/Matrix4.js +69 -1
  68. package/lib/commonjs/skia/types/Matrix4.js.map +1 -1
  69. package/lib/commonjs/skia/types/Surface/Surface.d.ts +11 -0
  70. package/lib/commonjs/skia/types/Surface/Surface.js.map +1 -1
  71. package/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  72. package/lib/commonjs/skia/web/JsiSkImage.js +4 -0
  73. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  74. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  75. package/lib/commonjs/skia/web/JsiSkImageFactory.js +3 -0
  76. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  77. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  78. package/lib/commonjs/skia/web/JsiSkSurface.js +4 -0
  79. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  80. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.d.ts +1 -0
  81. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  82. package/lib/commonjs/views/SkiaDomView.js +3 -1
  83. package/lib/commonjs/views/SkiaDomView.js.map +1 -1
  84. package/lib/commonjs/views/SkiaPictureView.js +5 -1
  85. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  86. package/lib/commonjs/views/types.d.ts +2 -0
  87. package/lib/commonjs/views/types.js.map +1 -1
  88. package/lib/module/skia/types/Image/ColorType.d.ts +21 -0
  89. package/lib/module/skia/types/Image/ColorType.js +23 -0
  90. package/lib/module/skia/types/Image/ColorType.js.map +1 -0
  91. package/lib/module/skia/types/Image/ColorType.web.d.ts +19 -0
  92. package/lib/module/skia/types/Image/ColorType.web.js +21 -0
  93. package/lib/module/skia/types/Image/ColorType.web.js.map +1 -0
  94. package/lib/module/skia/types/Image/Image.d.ts +10 -0
  95. package/lib/module/skia/types/Image/Image.js.map +1 -1
  96. package/lib/module/skia/types/Image/ImageFactory.d.ts +19 -19
  97. package/lib/module/skia/types/Image/ImageFactory.js +0 -20
  98. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  99. package/lib/module/skia/types/Image/index.d.ts +1 -0
  100. package/lib/module/skia/types/Image/index.js +1 -0
  101. package/lib/module/skia/types/Image/index.js.map +1 -1
  102. package/lib/module/skia/types/Matrix4.d.ts +6 -0
  103. package/lib/module/skia/types/Matrix4.js +67 -0
  104. package/lib/module/skia/types/Matrix4.js.map +1 -1
  105. package/lib/module/skia/types/Surface/Surface.d.ts +11 -0
  106. package/lib/module/skia/types/Surface/Surface.js.map +1 -1
  107. package/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  108. package/lib/module/skia/web/JsiSkImage.js +4 -0
  109. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  110. package/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  111. package/lib/module/skia/web/JsiSkImageFactory.js +3 -0
  112. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  113. package/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  114. package/lib/module/skia/web/JsiSkSurface.js +4 -0
  115. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  116. package/lib/module/specs/SkiaPictureViewNativeComponent.d.ts +1 -0
  117. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  118. package/lib/module/views/SkiaDomView.js +3 -1
  119. package/lib/module/views/SkiaDomView.js.map +1 -1
  120. package/lib/module/views/SkiaPictureView.js +5 -1
  121. package/lib/module/views/SkiaPictureView.js.map +1 -1
  122. package/lib/module/views/types.d.ts +2 -0
  123. package/lib/module/views/types.js.map +1 -1
  124. package/lib/typescript/lib/commonjs/skia/types/Image/ColorType.d.ts +2 -0
  125. package/lib/typescript/lib/commonjs/skia/types/Image/ColorType.web.d.ts +2 -0
  126. package/lib/typescript/lib/commonjs/skia/types/Image/ImageFactory.d.ts +0 -1
  127. package/lib/typescript/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  128. package/lib/typescript/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  129. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  130. package/lib/typescript/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  131. package/lib/typescript/lib/module/mock/index.d.ts +1 -0
  132. package/lib/typescript/lib/module/skia/types/Image/ColorType.d.ts +1 -0
  133. package/lib/typescript/lib/module/skia/types/Image/ColorType.web.d.ts +1 -0
  134. package/lib/typescript/lib/module/skia/types/Image/ImageFactory.d.ts +0 -1
  135. package/lib/typescript/lib/module/skia/types/Image/index.d.ts +1 -0
  136. package/lib/typescript/lib/module/skia/types/Matrix4.d.ts +1 -0
  137. package/lib/typescript/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  138. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  139. package/lib/typescript/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  140. package/lib/typescript/src/skia/types/Image/ColorType.d.ts +21 -0
  141. package/lib/typescript/src/skia/types/Image/ColorType.web.d.ts +19 -0
  142. package/lib/typescript/src/skia/types/Image/Image.d.ts +10 -0
  143. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +19 -19
  144. package/lib/typescript/src/skia/types/Image/index.d.ts +1 -0
  145. package/lib/typescript/src/skia/types/Matrix4.d.ts +6 -0
  146. package/lib/typescript/src/skia/types/Surface/Surface.d.ts +11 -0
  147. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +1 -0
  148. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +1 -0
  149. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +1 -0
  150. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.d.ts +1 -0
  151. package/lib/typescript/src/views/types.d.ts +2 -0
  152. package/package.json +1 -1
  153. package/src/renderer/__tests__/e2e/Matrix4.spec.tsx +93 -0
  154. package/src/skia/__tests__/Enums.spec.ts +2 -2
  155. package/src/skia/types/Image/ColorType.ts +21 -0
  156. package/src/skia/types/Image/ColorType.web.ts +19 -0
  157. package/src/skia/types/Image/Image.ts +11 -0
  158. package/src/skia/types/Image/ImageFactory.ts +25 -20
  159. package/src/skia/types/Image/index.ts +1 -0
  160. package/src/skia/types/Matrix4.ts +101 -0
  161. package/src/skia/types/Surface/Surface.ts +12 -0
  162. package/src/skia/web/JsiSkImage.ts +5 -0
  163. package/src/skia/web/JsiSkImageFactory.ts +4 -0
  164. package/src/skia/web/JsiSkSurface.ts +5 -0
  165. package/src/specs/SkiaPictureViewNativeComponent.ts +1 -0
  166. package/src/views/SkiaDomView.tsx +2 -1
  167. package/src/views/SkiaPictureView.tsx +4 -1
  168. package/src/views/types.ts +3 -0
  169. package/cpp/jsi/JsiValueWrapper.h +0 -164
  170. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +0 -128
  171. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +0 -92
@@ -9,9 +9,9 @@
9
9
  #include <vector>
10
10
 
11
11
  #include "JsiHostObject.h"
12
- #include "JsiValueWrapper.h"
13
12
  #include "RNSkPlatformContext.h"
14
13
  #include "RNSkView.h"
14
+ #include "ViewProperty.h"
15
15
  #include <jsi/jsi.h>
16
16
 
17
17
  namespace RNSkia {
@@ -20,7 +20,7 @@ namespace jsi = facebook::jsi;
20
20
  using RNSkViewInfo = struct RNSkViewInfo {
21
21
  RNSkViewInfo() { view = nullptr; }
22
22
  std::shared_ptr<RNSkView> view;
23
- std::unordered_map<std::string, RNJsi::JsiValueWrapper> props;
23
+ std::unordered_map<std::string, RNJsi::ViewProperty> props;
24
24
  };
25
25
 
26
26
  class RNSkJsiViewApi : public RNJsi::JsiHostObject,
@@ -57,7 +57,7 @@ public:
57
57
  auto info = getEnsuredViewInfo(nativeId);
58
58
 
59
59
  info->props.insert_or_assign(arguments[1].asString(runtime).utf8(runtime),
60
- RNJsi::JsiValueWrapper(runtime, arguments[2]));
60
+ RNJsi::ViewProperty(runtime, arguments[2]));
61
61
 
62
62
  // Now let's see if we have a view that we can update
63
63
  if (info->view != nullptr) {
@@ -9,8 +9,8 @@
9
9
 
10
10
  #include <jsi/jsi.h>
11
11
 
12
- #include "JsiValueWrapper.h"
13
12
  #include "RNSkView.h"
13
+ #include "ViewProperty.h"
14
14
 
15
15
  #include "JsiSkPicture.h"
16
16
  #include "RNSkLog.h"
@@ -40,46 +40,36 @@ class RNSkPictureRenderer
40
40
  public:
41
41
  RNSkPictureRenderer(std::function<void()> requestRedraw,
42
42
  std::shared_ptr<RNSkPlatformContext> context)
43
- : RNSkRenderer(requestRedraw), _platformContext(context) {}
44
-
45
- bool tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
46
- return performDraw(canvasProvider);
47
- }
43
+ : RNSkRenderer(std::move(requestRedraw)),
44
+ _platformContext(std::move(context)) {}
48
45
 
49
46
  void
50
47
  renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
51
48
  performDraw(canvasProvider);
52
49
  }
53
50
 
54
- void setPicture(std::shared_ptr<jsi::HostObject> picture) {
55
- if (picture == nullptr) {
56
- _picture = nullptr;
57
- } else {
58
- _picture = std::dynamic_pointer_cast<JsiSkPicture>(picture);
59
- }
51
+ void setPicture(sk_sp<SkPicture> picture) {
52
+ _picture = picture;
60
53
  _requestRedraw();
61
54
  }
62
55
 
63
56
  private:
64
57
  bool performDraw(std::shared_ptr<RNSkCanvasProvider> canvasProvider) {
65
- canvasProvider->renderToCanvas([=](SkCanvas *canvas) {
58
+ return canvasProvider->renderToCanvas([=](SkCanvas *canvas) {
66
59
  // Make sure to scale correctly
67
60
  auto pd = _platformContext->getPixelDensity();
68
61
  canvas->clear(SK_ColorTRANSPARENT);
69
62
  canvas->save();
70
63
  canvas->scale(pd, pd);
71
-
72
64
  if (_picture != nullptr) {
73
- canvas->drawPicture(_picture->getObject());
65
+ canvas->drawPicture(_picture);
74
66
  }
75
-
76
67
  canvas->restore();
77
68
  });
78
- return true;
79
69
  }
80
70
 
81
71
  std::shared_ptr<RNSkPlatformContext> _platformContext;
82
- std::shared_ptr<JsiSkPicture> _picture;
72
+ sk_sp<SkPicture> _picture;
83
73
  };
84
74
 
85
75
  class RNSkPictureView : public RNSkView {
@@ -95,27 +85,20 @@ public:
95
85
  std::bind(&RNSkPictureView::requestRedraw, this), context)) {}
96
86
 
97
87
  void setJsiProperties(
98
- std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) override {
99
-
100
- RNSkView::setJsiProperties(props);
88
+ std::unordered_map<std::string, RNJsi::ViewProperty> &props) override {
101
89
 
102
90
  for (auto &prop : props) {
103
91
  if (prop.first == "picture") {
104
- if (prop.second.isUndefinedOrNull()) {
92
+ if (prop.second.isNull()) {
105
93
  // Clear picture
106
94
  std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())
107
95
  ->setPicture(nullptr);
108
96
  continue;
109
- } else if (prop.second.getType() !=
110
- RNJsi::JsiWrapperValueType::HostObject) {
111
- // We expect a function for the picture custom property
112
- throw std::runtime_error(
113
- "Expected an object for the picture custom property.");
114
97
  }
115
98
 
116
99
  // Save picture
117
100
  std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())
118
- ->setPicture(prop.second.getAsHostObject());
101
+ ->setPicture(prop.second.getPicture());
119
102
  }
120
103
  }
121
104
  }
@@ -23,8 +23,6 @@
23
23
 
24
24
  #pragma clang diagnostic pop
25
25
 
26
- #include <jsi/jsi.h>
27
-
28
26
  #include <ReactCommon/CallInvoker.h>
29
27
 
30
28
  namespace RNSkia {
@@ -32,16 +30,22 @@ namespace RNSkia {
32
30
  namespace jsi = facebook::jsi;
33
31
  namespace react = facebook::react;
34
32
 
33
+ struct TextureInfo {
34
+ const void *mtlTexture = nullptr;
35
+ unsigned int glTarget = 0;
36
+ unsigned int glID = 0;
37
+ unsigned int glFormat = 0;
38
+ bool glProtected = false;
39
+ };
40
+
35
41
  class RNSkPlatformContext {
36
42
  public:
37
43
  /**
38
44
  * Constructor
39
45
  */
40
- RNSkPlatformContext(jsi::Runtime *runtime,
41
- std::shared_ptr<react::CallInvoker> callInvoker,
46
+ RNSkPlatformContext(std::shared_ptr<react::CallInvoker> callInvoker,
42
47
  float pixelDensity)
43
- : _pixelDensity(pixelDensity), _jsRuntime(runtime),
44
- _callInvoker(callInvoker) {}
48
+ : _pixelDensity(pixelDensity), _callInvoker(callInvoker) {}
45
49
 
46
50
  virtual ~RNSkPlatformContext() = default;
47
51
 
@@ -65,11 +69,6 @@ public:
65
69
  */
66
70
  virtual sk_sp<SkImage> takeScreenshotFromViewTag(size_t tag) = 0;
67
71
 
68
- /**
69
- Returns the javascript runtime
70
- */
71
- jsi::Runtime *getJsRuntime() { return _jsRuntime; }
72
-
73
72
  /**
74
73
  * Returns an SkStream wrapping the require uri provided.
75
74
  * @param sourceUri Uri for the resource to load as a string
@@ -107,6 +106,10 @@ public:
107
106
  */
108
107
  virtual sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) = 0;
109
108
 
109
+ virtual sk_sp<SkImage>
110
+ makeImageFromNativeTexture(const TextureInfo &textureInfo, int width,
111
+ int height, bool mipMapped) = 0;
112
+
110
113
  #if !defined(SK_GRAPHITE)
111
114
  virtual GrDirectContext *getDirectContext() = 0;
112
115
  #endif
@@ -115,6 +118,10 @@ public:
115
118
 
116
119
  virtual uint64_t makeNativeBuffer(sk_sp<SkImage> image) = 0;
117
120
 
121
+ virtual const TextureInfo getTexture(sk_sp<SkSurface> image) = 0;
122
+
123
+ virtual const TextureInfo getTexture(sk_sp<SkImage> image) = 0;
124
+
118
125
  virtual std::shared_ptr<RNSkVideo> createVideo(const std::string &url) = 0;
119
126
 
120
127
  /**
@@ -154,7 +161,6 @@ public:
154
161
 
155
162
  private:
156
163
  float _pixelDensity;
157
- jsi::Runtime *_jsRuntime;
158
164
  std::shared_ptr<react::CallInvoker> _callInvoker;
159
165
  };
160
166
  } // namespace RNSkia
@@ -6,8 +6,8 @@
6
6
  #include <unordered_map>
7
7
  #include <vector>
8
8
 
9
- #include "JsiValueWrapper.h"
10
9
  #include "RNSkPlatformContext.h"
10
+ #include "ViewProperty.h"
11
11
 
12
12
  #include "JsiSkImage.h"
13
13
  #include "JsiSkPoint.h"
@@ -52,33 +52,15 @@ protected:
52
52
  class RNSkRenderer {
53
53
  public:
54
54
  explicit RNSkRenderer(std::function<void()> requestRedraw)
55
- : _requestRedraw(requestRedraw) {}
55
+ : _requestRedraw(std::move(requestRedraw)), _showDebugOverlays(false) {}
56
56
 
57
- /**
58
- Tries to render the current set of drawing operations. If we're busy we'll
59
- return false so that the calling RNSkBaseDrawView can request a new render
60
- next frame. The tryRender method is typically called on each frame if there
61
- are any redraw requests. The method will be called from the main thread, so
62
- the implementor must make sure any thread requirements are met before
63
- rendering. This method will also allow the rendering to be dispatched to
64
- another thread.
65
- */
66
- virtual bool
67
- tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
68
-
69
- /**
70
- Renders directly to the canvas in the canvas provider. This method is called
71
- from a Javascript call to render a snapshot of the SkiaView to an image, and
72
- can therefore run outside the tryRender loop and directly in the javascript
73
- thread.
74
- */
75
57
  virtual void
76
58
  renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
77
59
 
78
60
  void setShowDebugOverlays(bool showDebugOverlays) {
79
61
  _showDebugOverlays = showDebugOverlays;
80
62
  }
81
- bool getShowDebugOverlays() { return _showDebugOverlays; }
63
+ bool getShowDebugOverlays() const { return _showDebugOverlays; }
82
64
 
83
65
  protected:
84
66
  std::function<void()> _requestRedraw;
@@ -161,14 +143,8 @@ public:
161
143
  */
162
144
  virtual ~RNSkView() {}
163
145
 
164
- /**
165
- Sets custom properties. Custom properties are properties that are set
166
- directly from Javascript without having to go through the async bridge.
167
- */
168
146
  virtual void setJsiProperties(
169
- std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) {
170
- // Nothing here...
171
- }
147
+ std::unordered_map<std::string, RNJsi::ViewProperty> &props) = 0;
172
148
 
173
149
  void requestRedraw() {
174
150
  if (!_redrawRequested) {
@@ -180,7 +156,7 @@ public:
180
156
  // Try to lock the weak pointer
181
157
  if (auto strongThis = weakThis.lock()) {
182
158
  // Only proceed if the object still exists
183
- if (strongThis->_renderer) {
159
+ if (strongThis->_renderer && strongThis->_redrawRequested) {
184
160
  strongThis->_renderer->renderImmediate(strongThis->_canvasProvider);
185
161
  strongThis->_redrawRequested = false;
186
162
  }
@@ -1,13 +1,64 @@
1
1
  #pragma once
2
2
 
3
- #include "SkiaMetalSurfaceFactory.h"
4
- #include "WindowContext.h"
3
+ #include "MetalWindowContext.h"
4
+ #include "SkiaCVPixelBufferUtils.h"
5
5
 
6
6
  #include "include/core/SkSurface.h"
7
7
 
8
- namespace RNSkia {
9
- class RNSkiOSPlatformContext;
10
- }
8
+ #import <include/gpu/ganesh/GrBackendSurface.h>
9
+ #import <include/gpu/ganesh/GrDirectContext.h>
10
+ #import <include/gpu/ganesh/SkImageGanesh.h>
11
+ #import <include/gpu/ganesh/SkSurfaceGanesh.h>
12
+ #import <include/gpu/ganesh/mtl/GrMtlBackendContext.h>
13
+ #import <include/gpu/ganesh/mtl/GrMtlBackendSurface.h>
14
+ #import <include/gpu/ganesh/mtl/GrMtlDirectContext.h>
15
+ #import <include/gpu/ganesh/mtl/SkSurfaceMetal.h>
16
+
17
+ // namespace RNSkia {
18
+ // class RNSkiOSPlatformContext;
19
+ // }
20
+
21
+ class MetalSharedContext {
22
+ public:
23
+ static MetalSharedContext &getInstance() {
24
+ static MetalSharedContext instance;
25
+ return instance;
26
+ }
27
+
28
+ id<MTLDevice> getDevice() { return _device; }
29
+
30
+ private:
31
+ MetalSharedContext() {
32
+ _device = MTLCreateSystemDefaultDevice();
33
+ if (!_device) {
34
+ throw std::runtime_error("Failed to create Metal device");
35
+ }
36
+ }
37
+
38
+ MetalSharedContext(const MetalSharedContext &) = delete;
39
+ MetalSharedContext &operator=(const MetalSharedContext &) = delete;
40
+
41
+ id<MTLDevice> _device;
42
+ };
43
+
44
+ struct OffscreenRenderContext {
45
+ id<MTLTexture> texture;
46
+
47
+ OffscreenRenderContext(id<MTLDevice> device,
48
+ sk_sp<GrDirectContext> skiaContext,
49
+ id<MTLCommandQueue> commandQueue, int width,
50
+ int height) {
51
+ // Create a Metal texture descriptor
52
+ MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor
53
+ texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
54
+ width:width
55
+ height:height
56
+ mipmapped:NO];
57
+ textureDescriptor.usage =
58
+ MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead;
59
+ texture = [device newTextureWithDescriptor:textureDescriptor];
60
+ }
61
+ };
11
62
 
12
63
  class MetalContext {
13
64
 
@@ -21,28 +72,63 @@ public:
21
72
  }
22
73
 
23
74
  sk_sp<SkSurface> MakeOffscreen(int width, int height) {
24
- return SkiaMetalSurfaceFactory::makeOffscreenSurface(_device, &_context,
25
- width, height);
75
+ auto device = MetalSharedContext::getInstance().getDevice();
76
+ auto ctx = new OffscreenRenderContext(device, _directContext, _commandQueue,
77
+ width, height);
78
+
79
+ // Create a GrBackendTexture from the Metal texture
80
+ GrMtlTextureInfo info;
81
+ info.fTexture.retain((__bridge void *)ctx->texture);
82
+ GrBackendTexture backendTexture =
83
+ GrBackendTextures::MakeMtl(width, height, skgpu::Mipmapped::kNo, info);
84
+
85
+ // Create a SkSurface from the GrBackendTexture
86
+ auto surface = SkSurfaces::WrapBackendTexture(
87
+ _directContext.get(), backendTexture, kTopLeft_GrSurfaceOrigin, 0,
88
+ kBGRA_8888_SkColorType, nullptr, nullptr,
89
+ [](void *addr) { delete (OffscreenRenderContext *)addr; }, ctx);
90
+
91
+ return surface;
26
92
  }
27
93
 
28
94
  sk_sp<SkImage> MakeImageFromBuffer(void *buffer) {
95
+
29
96
  CVPixelBufferRef sampleBuffer = (CVPixelBufferRef)buffer;
30
- return SkiaMetalSurfaceFactory::makeTextureFromCVPixelBuffer(&_context,
31
- sampleBuffer);
97
+ SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat format =
98
+ SkiaCVPixelBufferUtils::getCVPixelBufferBaseFormat(sampleBuffer);
99
+ switch (format) {
100
+ case SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat::rgb: {
101
+ // CVPixelBuffer is in any RGB format, single-plane
102
+ return SkiaCVPixelBufferUtils::RGB::makeSkImageFromCVPixelBuffer(
103
+ _directContext.get(), sampleBuffer);
104
+ }
105
+ case SkiaCVPixelBufferUtils::CVPixelBufferBaseFormat::yuv: {
106
+ // CVPixelBuffer is in any YUV format, multi-plane
107
+ return SkiaCVPixelBufferUtils::YUV::makeSkImageFromCVPixelBuffer(
108
+ _directContext.get(), sampleBuffer);
109
+ }
110
+ default:
111
+ [[unlikely]] {
112
+ throw std::runtime_error("Failed to convert NativeBuffer to SkImage - "
113
+ "NativeBuffer has unsupported PixelFormat! " +
114
+ std::to_string(static_cast<int>(format)));
115
+ }
116
+ }
32
117
  }
33
118
 
34
119
  std::unique_ptr<RNSkia::WindowContext> MakeWindow(CALayer *window, int width,
35
120
  int height) {
36
- return SkiaMetalSurfaceFactory::makeContext(&_context, window, width,
37
- height);
121
+ auto device = MetalSharedContext::getInstance().getDevice();
122
+ return std::make_unique<MetalWindowContext>(
123
+ _directContext.get(), device, _commandQueue, window, width, height);
38
124
  }
39
125
 
40
- GrDirectContext *getDirectContext() { return _context.skContext.get(); }
126
+ GrDirectContext *getDirectContext() { return _directContext.get(); }
41
127
 
42
128
  private:
43
- friend class RNSkia::RNSkiOSPlatformContext;
44
- id<MTLDevice> _device;
45
- SkiaMetalContext _context;
129
+ // friend class RNSkia::RNSkiOSPlatformContext;
130
+ id<MTLCommandQueue> _commandQueue = nullptr;
131
+ sk_sp<GrDirectContext> _directContext = nullptr;
46
132
 
47
133
  MetalContext();
48
134
  };
@@ -1,5 +1,7 @@
1
1
  #include "MetalContext.h"
2
2
 
3
+ #include "RNSkLog.h"
4
+
3
5
  #import <MetalKit/MetalKit.h>
4
6
 
5
7
  #pragma clang diagnostic push
@@ -16,18 +18,17 @@
16
18
  #pragma clang diagnostic pop
17
19
 
18
20
  MetalContext::MetalContext() {
19
- _device = MTLCreateSystemDefaultDevice();
20
- _context.commandQueue =
21
- id<MTLCommandQueue>(CFRetain((GrMTLHandle)[_device newCommandQueue]));
22
-
21
+ auto device = MetalSharedContext::getInstance().getDevice();
22
+ _commandQueue =
23
+ id<MTLCommandQueue>(CFRetain((GrMTLHandle)[device newCommandQueue]));
23
24
  GrMtlBackendContext backendContext = {};
24
- backendContext.fDevice.reset((__bridge void *)_device);
25
- backendContext.fQueue.reset((__bridge void *)_context.commandQueue);
25
+ backendContext.fDevice.reset((__bridge void *)device);
26
+ backendContext.fQueue.reset((__bridge void *)_commandQueue);
26
27
  GrContextOptions grContextOptions; // set different options here.
27
28
 
28
29
  // Create the Skia Direct Context
29
- _context.skContext = GrDirectContexts::MakeMetal(backendContext);
30
- if (_context.skContext == nullptr) {
30
+ _directContext = GrDirectContexts::MakeMetal(backendContext);
31
+ if (_directContext == nullptr) {
31
32
  RNSkia::RNSkLogger::logToConsole("Couldn't create a Skia Metal Context");
32
33
  }
33
34
  }
@@ -0,0 +1,39 @@
1
+ #pragma once
2
+
3
+ #import <MetalKit/MetalKit.h>
4
+
5
+ #include "WindowContext.h"
6
+
7
+ class SkiaMetalContext;
8
+
9
+ class MetalWindowContext : public RNSkia::WindowContext {
10
+ public:
11
+ MetalWindowContext(GrDirectContext *directContext, id<MTLDevice> device,
12
+ id<MTLCommandQueue> commandQueue, CALayer *layer,
13
+ int width, int height);
14
+ ~MetalWindowContext() = default;
15
+
16
+ sk_sp<SkSurface> getSurface() override;
17
+
18
+ void present() override;
19
+
20
+ int getWidth() override {
21
+ return _layer.frame.size.width * _layer.contentsScale;
22
+ };
23
+
24
+ int getHeight() override {
25
+ return _layer.frame.size.height * _layer.contentsScale;
26
+ };
27
+
28
+ void resize(int width, int height) override { _skSurface = nullptr; }
29
+
30
+ private:
31
+ GrDirectContext *_directContext;
32
+ id<MTLCommandQueue> _commandQueue;
33
+ sk_sp<SkSurface> _skSurface = nullptr;
34
+ #pragma clang diagnostic push
35
+ #pragma clang diagnostic ignored "-Wunguarded-availability-new"
36
+ CAMetalLayer *_layer;
37
+ #pragma clang diagnostic pop
38
+ id<CAMetalDrawable> _currentDrawable = nil;
39
+ };
@@ -0,0 +1,60 @@
1
+ #include "MetalWindowContext.h"
2
+
3
+ #include "MetalContext.h"
4
+ #include "RNSkLog.h"
5
+
6
+ MetalWindowContext::MetalWindowContext(GrDirectContext *directContext,
7
+ id<MTLDevice> device,
8
+ id<MTLCommandQueue> commandQueue,
9
+ CALayer *layer, int width, int height)
10
+ : _directContext(directContext), _commandQueue(commandQueue) {
11
+ #pragma clang diagnostic push
12
+ #pragma clang diagnostic ignored "-Wunguarded-availability-new"
13
+ _layer = (CAMetalLayer *)layer;
14
+ #pragma clang diagnostic pop
15
+ _layer.framebufferOnly = NO;
16
+ _layer.device = device;
17
+ _layer.opaque = false;
18
+ _layer.contentsScale = [UIScreen mainScreen].scale;
19
+ _layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
20
+ _layer.contentsGravity = kCAGravityBottomLeft;
21
+ _layer.drawableSize = CGSizeMake(width, height);
22
+ }
23
+
24
+ sk_sp<SkSurface> MetalWindowContext::getSurface() {
25
+ if (_skSurface) {
26
+ return _skSurface;
27
+ }
28
+
29
+ // Get the next drawable from the CAMetalLayer
30
+ _currentDrawable = [_layer nextDrawable];
31
+ if (!_currentDrawable) {
32
+ RNSkia::RNSkLogger::logToConsole(
33
+ "Could not retrieve drawable from CAMetalLayer");
34
+ return nullptr;
35
+ }
36
+
37
+ // Get the texture from the drawable
38
+ GrMtlTextureInfo fbInfo;
39
+ fbInfo.fTexture.retain((__bridge void *)_currentDrawable.texture);
40
+
41
+ GrBackendRenderTarget backendRT = GrBackendRenderTargets::MakeMtl(
42
+ _layer.drawableSize.width, _layer.drawableSize.height, fbInfo);
43
+
44
+ _skSurface = SkSurfaces::WrapBackendRenderTarget(
45
+ _directContext, backendRT, kTopLeft_GrSurfaceOrigin,
46
+ kBGRA_8888_SkColorType, nullptr, nullptr);
47
+
48
+ return _skSurface;
49
+ }
50
+
51
+ void MetalWindowContext::present() {
52
+ if (auto dContext = GrAsDirectContext(_skSurface->recordingContext())) {
53
+ dContext->flushAndSubmit();
54
+ }
55
+
56
+ id<MTLCommandBuffer> commandBuffer([_commandQueue commandBuffer]);
57
+ [commandBuffer presentDrawable:_currentDrawable];
58
+ [commandBuffer commit];
59
+ _skSurface = nullptr;
60
+ }
@@ -22,33 +22,19 @@ namespace RNSkia {
22
22
 
23
23
  namespace jsi = facebook::jsi;
24
24
 
25
- static void handleNotification(CFNotificationCenterRef center, void *observer,
26
- CFStringRef name, const void *object,
27
- CFDictionaryRef userInfo);
28
-
29
25
  class RNSkiOSPlatformContext : public RNSkPlatformContext {
30
26
  public:
31
27
  RNSkiOSPlatformContext(
32
- jsi::Runtime *runtime, RCTBridge *bridge,
28
+ RCTBridge *bridge,
33
29
  std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
34
- : RNSkPlatformContext(runtime, jsCallInvoker,
35
- [[UIScreen mainScreen] scale]) {
36
-
37
- // We need to make sure we invalidate when modules are freed
38
- CFNotificationCenterAddObserver(
39
- CFNotificationCenterGetLocalCenter(), this, &handleNotification,
40
- (__bridge CFStringRef)RCTBridgeWillInvalidateModulesNotification, NULL,
41
- CFNotificationSuspensionBehaviorDeliverImmediately);
30
+ : RNSkPlatformContext(jsCallInvoker, [[UIScreen mainScreen] scale]) {
42
31
 
43
32
  // Create screenshot manager
44
33
  _screenshotService =
45
34
  [[ViewScreenshotService alloc] initWithUiManager:bridge.uiManager];
46
35
  }
47
36
 
48
- ~RNSkiOSPlatformContext() {
49
- CFNotificationCenterRemoveEveryObserver(
50
- CFNotificationCenterGetLocalCenter(), this);
51
- }
37
+ ~RNSkiOSPlatformContext() = default;
52
38
 
53
39
  void runOnMainThread(std::function<void()>) override;
54
40
 
@@ -56,8 +42,16 @@ public:
56
42
 
57
43
  sk_sp<SkImage> makeImageFromNativeBuffer(void *buffer) override;
58
44
 
45
+ sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &textureInfo,
46
+ int width, int height,
47
+ bool mipMapped) override;
48
+
59
49
  uint64_t makeNativeBuffer(sk_sp<SkImage> image) override;
60
50
 
51
+ const TextureInfo getTexture(sk_sp<SkSurface> image) override;
52
+
53
+ const TextureInfo getTexture(sk_sp<SkImage> image) override;
54
+
61
55
  void releaseNativeBuffer(uint64_t pointer) override;
62
56
 
63
57
  std::shared_ptr<RNSkVideo> createVideo(const std::string &url) override;
@@ -76,16 +70,10 @@ public:
76
70
  #endif
77
71
  sk_sp<SkFontMgr> createFontMgr() override;
78
72
 
79
- void willInvalidateModules() {}
80
-
81
73
  private:
82
74
  ViewScreenshotService *_screenshotService;
83
- };
84
75
 
85
- static void handleNotification(CFNotificationCenterRef center, void *observer,
86
- CFStringRef name, const void *object,
87
- CFDictionaryRef userInfo) {
88
- (static_cast<RNSkiOSPlatformContext *>(observer))->willInvalidateModules();
89
- }
76
+ SkColorType mtlPixelFormatToSkColorType(MTLPixelFormat pixelFormat);
77
+ };
90
78
 
91
79
  } // namespace RNSkia