@shopify/react-native-skia 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/android/cpp/jni/include/JniSkiaBaseView.h +0 -1
  2. package/android/cpp/jni/include/JniSkiaManager.h +2 -2
  3. package/android/cpp/rnskia-android/MainThreadDispatcher.h +2 -4
  4. package/android/cpp/rnskia-android/OpenGLContext.h +13 -5
  5. package/android/cpp/rnskia-android/OpenGLWindowContext.h +2 -2
  6. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +61 -3
  7. package/android/cpp/rnskia-android/RNSkAndroidView.h +0 -4
  8. package/android/cpp/rnskia-android/gl/Display.h +2 -0
  9. package/cpp/api/JsiSkContourMeasure.h +1 -5
  10. package/cpp/api/JsiSkImage.h +11 -0
  11. package/cpp/api/JsiSkImageFactory.h +14 -0
  12. package/cpp/api/JsiSkPicture.h +2 -0
  13. package/cpp/api/JsiSkSurface.h +7 -0
  14. package/cpp/api/JsiTextureInfo.h +53 -0
  15. package/cpp/jsi/ViewProperty.h +48 -0
  16. package/cpp/rnskia/RNSkDomView.h +4 -7
  17. package/cpp/rnskia/RNSkJsiViewApi.h +3 -3
  18. package/cpp/rnskia/RNSkPictureView.h +8 -18
  19. package/cpp/rnskia/RNSkPlatformContext.h +18 -12
  20. package/cpp/rnskia/RNSkView.h +2 -8
  21. package/ios/RNSkia-iOS/MetalContext.h +101 -15
  22. package/ios/RNSkia-iOS/MetalContext.mm +9 -8
  23. package/ios/RNSkia-iOS/MetalWindowContext.h +39 -0
  24. package/ios/RNSkia-iOS/MetalWindowContext.mm +60 -0
  25. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +13 -25
  26. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +88 -2
  27. package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +17 -6
  28. package/ios/RNSkia-iOS/SkiaManager.mm +1 -2
  29. package/ios/RNSkia-iOS/SkiaUIView.mm +17 -15
  30. package/lib/commonjs/skia/types/Image/Image.d.ts +10 -0
  31. package/lib/commonjs/skia/types/Image/Image.js.map +1 -1
  32. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +18 -0
  33. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  34. package/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  35. package/lib/commonjs/skia/types/Matrix4.js +69 -1
  36. package/lib/commonjs/skia/types/Matrix4.js.map +1 -1
  37. package/lib/commonjs/skia/types/Surface/Surface.d.ts +11 -0
  38. package/lib/commonjs/skia/types/Surface/Surface.js.map +1 -1
  39. package/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  40. package/lib/commonjs/skia/web/JsiSkImage.js +4 -0
  41. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  42. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  43. package/lib/commonjs/skia/web/JsiSkImageFactory.js +3 -0
  44. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  45. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  46. package/lib/commonjs/skia/web/JsiSkSurface.js +4 -0
  47. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  48. package/lib/module/skia/types/Image/Image.d.ts +10 -0
  49. package/lib/module/skia/types/Image/Image.js.map +1 -1
  50. package/lib/module/skia/types/Image/ImageFactory.d.ts +18 -0
  51. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  52. package/lib/module/skia/types/Matrix4.d.ts +6 -0
  53. package/lib/module/skia/types/Matrix4.js +67 -0
  54. package/lib/module/skia/types/Matrix4.js.map +1 -1
  55. package/lib/module/skia/types/Surface/Surface.d.ts +11 -0
  56. package/lib/module/skia/types/Surface/Surface.js.map +1 -1
  57. package/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  58. package/lib/module/skia/web/JsiSkImage.js +4 -0
  59. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  60. package/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  61. package/lib/module/skia/web/JsiSkImageFactory.js +3 -0
  62. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  63. package/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  64. package/lib/module/skia/web/JsiSkSurface.js +4 -0
  65. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  66. package/lib/typescript/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  67. package/lib/typescript/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  68. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  69. package/lib/typescript/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  70. package/lib/typescript/lib/module/mock/index.d.ts +1 -0
  71. package/lib/typescript/lib/module/skia/types/Matrix4.d.ts +1 -0
  72. package/lib/typescript/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  73. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  74. package/lib/typescript/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  75. package/lib/typescript/src/skia/types/Image/Image.d.ts +10 -0
  76. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +18 -0
  77. package/lib/typescript/src/skia/types/Matrix4.d.ts +6 -0
  78. package/lib/typescript/src/skia/types/Surface/Surface.d.ts +11 -0
  79. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +1 -0
  80. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +1 -0
  81. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +1 -0
  82. package/package.json +1 -1
  83. package/src/renderer/__tests__/e2e/Matrix4.spec.tsx +93 -0
  84. package/src/skia/types/Image/Image.ts +11 -0
  85. package/src/skia/types/Image/ImageFactory.ts +24 -0
  86. package/src/skia/types/Matrix4.ts +101 -0
  87. package/src/skia/types/Surface/Surface.ts +12 -0
  88. package/src/skia/web/JsiSkImage.ts +5 -0
  89. package/src/skia/web/JsiSkImageFactory.ts +4 -0
  90. package/src/skia/web/JsiSkSurface.ts +5 -0
  91. package/cpp/jsi/JsiValueWrapper.h +0 -164
  92. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +0 -128
  93. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +0 -92
@@ -55,7 +55,6 @@ protected:
55
55
  _skiaAndroidView->getSkiaView()->getNativeId(), nullptr);
56
56
  getSkiaManager()->unregisterSkiaView(
57
57
  _skiaAndroidView->getSkiaView()->getNativeId());
58
- _skiaAndroidView->viewDidUnmount();
59
58
  }
60
59
 
61
60
  private:
@@ -44,8 +44,8 @@ public:
44
44
  JniPlatformContext *platformContext)
45
45
  : _javaPart(jni::make_global(jThis)), _jsRuntime(runtime),
46
46
  _jsCallInvoker(jsCallInvoker),
47
- _context(std::make_shared<RNSkAndroidPlatformContext>(
48
- platformContext, runtime, jsCallInvoker)) {}
47
+ _context(std::make_shared<RNSkAndroidPlatformContext>(platformContext,
48
+ jsCallInvoker)) {}
49
49
 
50
50
  std::shared_ptr<RNSkAndroidPlatformContext> getPlatformContext() {
51
51
  return _context;
@@ -1,8 +1,8 @@
1
1
  #pragma once
2
2
 
3
3
  #include <android/looper.h>
4
- #include <unistd.h>
5
4
  #include <queue>
5
+ #include <unistd.h>
6
6
 
7
7
  class MainThreadDispatcher {
8
8
  private:
@@ -28,9 +28,7 @@ public:
28
28
  return instance;
29
29
  }
30
30
 
31
- bool isOnMainThread() {
32
- return ALooper_forThread() == mainLooper;
33
- }
31
+ bool isOnMainThread() { return ALooper_forThread() == mainLooper; }
34
32
 
35
33
  void post(std::function<void()> task) {
36
34
  // TODO: this is disabled for now but we can clean this up
@@ -28,15 +28,21 @@ public:
28
28
 
29
29
  gl::Display *getDisplay() { return _glDisplay.get(); }
30
30
  gl::Context *getContext() { return _glContext.get(); }
31
+ gl::Surface *getSurface() { return _glSurface.get(); };
32
+ EGLConfig getConfig() { return _glConfig; }
31
33
 
32
34
  private:
33
35
  std::unique_ptr<gl::Display> _glDisplay;
34
36
  std::unique_ptr<gl::Context> _glContext;
37
+ std::unique_ptr<gl::Surface> _glSurface;
38
+ EGLConfig _glConfig;
35
39
 
36
40
  OpenGLSharedContext() {
37
41
  _glDisplay = std::make_unique<gl::Display>();
38
- auto glConfig = _glDisplay->chooseConfig();
39
- _glContext = _glDisplay->makeContext(glConfig, nullptr);
42
+ _glConfig = _glDisplay->chooseConfig();
43
+ _glContext = _glDisplay->makeContext(_glConfig, nullptr);
44
+ _glSurface = _glDisplay->makePixelBufferSurface(_glConfig, 1, 1);
45
+ _glContext->makeCurrent(_glSurface.get());
40
46
  }
41
47
  };
42
48
 
@@ -156,11 +162,13 @@ public:
156
162
  std::unique_ptr<WindowContext> MakeWindow(ANativeWindow *window, int width,
157
163
  int height) {
158
164
  auto display = OpenGLSharedContext::getInstance().getDisplay();
159
- return std::make_unique<OpenGLWindowContext>(_directContext.get(), display,
160
- _glContext.get(), window);
165
+ return std::make_unique<OpenGLWindowContext>(
166
+ _directContext.get(), display, _glContext.get(), window,
167
+ OpenGLSharedContext::getInstance().getConfig());
161
168
  }
162
169
 
163
170
  GrDirectContext *getDirectContext() { return _directContext.get(); }
171
+ void makeCurrent() { _glContext->makeCurrent(_glSurface.get()); }
164
172
 
165
173
  private:
166
174
  std::unique_ptr<gl::Context> _glContext;
@@ -170,7 +178,7 @@ private:
170
178
  OpenGLContext() {
171
179
  auto display = OpenGLSharedContext::getInstance().getDisplay();
172
180
  auto sharedContext = OpenGLSharedContext::getInstance().getContext();
173
- auto glConfig = display->chooseConfig();
181
+ auto glConfig = OpenGLSharedContext::getInstance().getConfig();
174
182
  _glContext = display->makeContext(glConfig, sharedContext);
175
183
  _glSurface = display->makePixelBufferSurface(glConfig, 1, 1);
176
184
  _glContext->makeCurrent(_glSurface.get());
@@ -34,11 +34,11 @@ namespace RNSkia {
34
34
  class OpenGLWindowContext : public WindowContext {
35
35
  public:
36
36
  OpenGLWindowContext(GrDirectContext *directContext, gl::Display *display,
37
- gl::Context *glContext, ANativeWindow *window)
37
+ gl::Context *glContext, ANativeWindow *window,
38
+ EGLConfig config)
38
39
  : _directContext(directContext), _display(display), _glContext(glContext),
39
40
  _window(window) {
40
41
  ANativeWindow_acquire(_window);
41
- auto config = display->chooseConfig();
42
42
  _glSurface = display->makeWindowSurface(config, _window);
43
43
  }
44
44
 
@@ -20,7 +20,6 @@
20
20
  #include "MainThreadDispatcher.h"
21
21
  #include "RNSkAndroidVideo.h"
22
22
  #include "RNSkPlatformContext.h"
23
-
24
23
  #pragma clang diagnostic push
25
24
  #pragma clang diagnostic ignored "-Wdocumentation"
26
25
 
@@ -34,9 +33,9 @@ namespace jsi = facebook::jsi;
34
33
  class RNSkAndroidPlatformContext : public RNSkPlatformContext {
35
34
  public:
36
35
  RNSkAndroidPlatformContext(
37
- JniPlatformContext *jniPlatformContext, jsi::Runtime *runtime,
36
+ JniPlatformContext *jniPlatformContext,
38
37
  std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
39
- : RNSkPlatformContext(runtime, jsCallInvoker,
38
+ : RNSkPlatformContext(jsCallInvoker,
40
39
  jniPlatformContext->getPixelDensity()),
41
40
  _jniPlatformContext(jniPlatformContext) {}
42
41
 
@@ -78,6 +77,31 @@ public:
78
77
  #endif
79
78
  }
80
79
 
80
+ sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &texInfo,
81
+ int width, int height,
82
+ bool mipMapped) override {
83
+ GrGLTextureInfo textureInfo;
84
+ textureInfo.fTarget = (GrGLenum)texInfo.glTarget;
85
+ textureInfo.fID = (GrGLuint)texInfo.glID;
86
+ textureInfo.fFormat = (GrGLenum)texInfo.glFormat;
87
+ textureInfo.fProtected =
88
+ texInfo.glProtected ? skgpu::Protected::kYes : skgpu::Protected::kNo;
89
+
90
+ OpenGLContext::getInstance().makeCurrent();
91
+ if (glIsTexture(textureInfo.fID) == GL_FALSE) {
92
+ throw new std::runtime_error("Invalid textureInfo");
93
+ }
94
+
95
+ GrBackendTexture backendTexture = GrBackendTextures::MakeGL(
96
+ width, height,
97
+ mipMapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
98
+ textureInfo);
99
+ return SkImages::BorrowTextureFrom(
100
+ OpenGLContext::getInstance().getDirectContext(), backendTexture,
101
+ kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType,
102
+ nullptr);
103
+ }
104
+
81
105
  std::shared_ptr<RNSkVideo> createVideo(const std::string &url) override {
82
106
  auto jniVideo = _jniPlatformContext->createVideo(url);
83
107
  return std::make_shared<RNSkAndroidVideo>(jniVideo);
@@ -149,6 +173,40 @@ public:
149
173
  #endif
150
174
  }
151
175
 
176
+ const TextureInfo getTexture(sk_sp<SkImage> image) override {
177
+ GrBackendTexture texture;
178
+ if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
179
+ throw std::runtime_error("Couldn't get backend texture from image.");
180
+ }
181
+ return getJSITextureInfo(texture);
182
+ }
183
+
184
+ const TextureInfo getTexture(sk_sp<SkSurface> surface) override {
185
+ GrBackendTexture texture = SkSurfaces::GetBackendTexture(
186
+ surface.get(), SkSurface::BackendHandleAccess::kFlushRead);
187
+ return getJSITextureInfo(texture);
188
+ }
189
+
190
+ static const TextureInfo getJSITextureInfo(const GrBackendTexture &texture) {
191
+ if (!texture.isValid()) {
192
+ throw std::runtime_error("invalid backend texture");
193
+ }
194
+ GrGLTextureInfo textureInfo;
195
+ if (!GrBackendTextures::GetGLTextureInfo(texture, &textureInfo)) {
196
+ throw std::runtime_error("couldn't get OpenGL texture");
197
+ }
198
+
199
+ OpenGLContext::getInstance().makeCurrent();
200
+ glFlush();
201
+
202
+ TextureInfo texInfo;
203
+ texInfo.glProtected = textureInfo.isProtected();
204
+ texInfo.glID = textureInfo.fID;
205
+ texInfo.glFormat = textureInfo.fFormat;
206
+ texInfo.glTarget = textureInfo.fTarget;
207
+ return texInfo;
208
+ }
209
+
152
210
  #if !defined(SK_GRAPHITE)
153
211
  GrDirectContext *getDirectContext() override {
154
212
  return OpenGLContext::getInstance().getDirectContext();
@@ -23,8 +23,6 @@ public:
23
23
 
24
24
  virtual void setShowDebugInfo(bool show) = 0;
25
25
 
26
- virtual void viewDidUnmount() = 0;
27
-
28
26
  virtual std::shared_ptr<RNSkView> getSkiaView() = 0;
29
27
  };
30
28
 
@@ -63,8 +61,6 @@ public:
63
61
 
64
62
  void setShowDebugInfo(bool show) override { T::setShowDebugOverlays(show); }
65
63
 
66
- void viewDidUnmount() override {}
67
-
68
64
  std::shared_ptr<RNSkView> getSkiaView() override {
69
65
  return T::shared_from_this();
70
66
  }
@@ -43,6 +43,8 @@ public:
43
43
 
44
44
  EGLint att[] = {EGL_RENDERABLE_TYPE,
45
45
  EGL_OPENGL_ES2_BIT,
46
+ EGL_SURFACE_TYPE,
47
+ EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
46
48
  EGL_ALPHA_SIZE,
47
49
  8,
48
50
  EGL_BLUE_SIZE,
@@ -26,11 +26,7 @@ public:
26
26
  JsiSkContourMeasure(std::shared_ptr<RNSkPlatformContext> context,
27
27
  const sk_sp<SkContourMeasure> contourMeasure)
28
28
  : JsiSkWrappingSkPtrHostObject(std::move(context),
29
- std::move(contourMeasure)) {
30
- if (contourMeasure == nullptr) {
31
- throw jsi::JSError(*context->getJsRuntime(), "Contour measure is null");
32
- }
33
- }
29
+ std::move(contourMeasure)) {}
34
30
 
35
31
  JSI_HOST_FUNCTION(getPosTan) {
36
32
  auto dist = arguments[0].asNumber();
@@ -10,6 +10,7 @@
10
10
  #include "JsiSkShader.h"
11
11
  #include "third_party/base64.h"
12
12
 
13
+ #include "JsiTextureInfo.h"
13
14
  #include "RNSkTypedArray.h"
14
15
 
15
16
  #if defined(SK_GRAPHITE)
@@ -215,6 +216,15 @@ public:
215
216
  runtime, std::make_shared<JsiSkImage>(getContext(), rasterImage));
216
217
  }
217
218
 
219
+ JSI_HOST_FUNCTION(getNativeTextureUnstable) {
220
+ auto image = getObject();
221
+ if (!image->isTextureBacked()) {
222
+ return jsi::Value::null();
223
+ }
224
+ auto texInfo = getContext()->getTexture(image);
225
+ return JsiTextureInfo::toValue(runtime, texInfo);
226
+ }
227
+
218
228
  EXPORT_JSI_API_TYPENAME(JsiSkImage, Image)
219
229
 
220
230
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkImage, width),
@@ -226,6 +236,7 @@ public:
226
236
  JSI_EXPORT_FUNC(JsiSkImage, encodeToBase64),
227
237
  JSI_EXPORT_FUNC(JsiSkImage, readPixels),
228
238
  JSI_EXPORT_FUNC(JsiSkImage, makeNonTextureImage),
239
+ JSI_EXPORT_FUNC(JsiSkImage, getNativeTextureUnstable),
229
240
  JSI_EXPORT_FUNC(JsiSkImage, dispose))
230
241
 
231
242
  JsiSkImage(std::shared_ptr<RNSkPlatformContext> context,
@@ -78,10 +78,24 @@ public:
78
78
  });
79
79
  }
80
80
 
81
+ JSI_HOST_FUNCTION(MakeImageFromNativeTextureUnstable) {
82
+ auto texInfo = JsiTextureInfo::fromValue(runtime, arguments[0]);
83
+ auto image = getContext()->makeImageFromNativeTexture(
84
+ texInfo, arguments[1].asNumber(), arguments[2].asNumber(),
85
+ count > 3 && arguments[3].asBool());
86
+ if (image == nullptr) {
87
+ throw std::runtime_error("Failed to convert native texture to SkImage!");
88
+ }
89
+ return jsi::Object::createFromHostObject(
90
+ runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
91
+ }
92
+
81
93
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImageFromEncoded),
82
94
  JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImageFromViewTag),
83
95
  JSI_EXPORT_FUNC(JsiSkImageFactory,
84
96
  MakeImageFromNativeBuffer),
97
+ JSI_EXPORT_FUNC(JsiSkImageFactory,
98
+ MakeImageFromNativeTextureUnstable),
85
99
  JSI_EXPORT_FUNC(JsiSkImageFactory, MakeImage))
86
100
 
87
101
  explicit JsiSkImageFactory(std::shared_ptr<RNSkPlatformContext> context)
@@ -4,6 +4,8 @@
4
4
 
5
5
  #include "JsiSkData.h"
6
6
  #include "JsiSkHostObjects.h"
7
+ #include "JsiSkMatrix.h"
8
+ #include "JsiSkRect.h"
7
9
  #include "JsiSkShader.h"
8
10
 
9
11
  #pragma clang diagnostic push
@@ -6,6 +6,7 @@
6
6
  #include <jsi/jsi.h>
7
7
 
8
8
  #include "JsiSkHostObjects.h"
9
+ #include "JsiTextureInfo.h"
9
10
 
10
11
  #include "JsiSkCanvas.h"
11
12
  #include "JsiSkImage.h"
@@ -78,11 +79,17 @@ public:
78
79
  runtime, std::make_shared<JsiSkImage>(getContext(), std::move(image)));
79
80
  }
80
81
 
82
+ JSI_HOST_FUNCTION(getNativeTextureUnstable) {
83
+ auto texInfo = getContext()->getTexture(getObject());
84
+ return JsiTextureInfo::toValue(runtime, texInfo);
85
+ }
86
+
81
87
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkSurface, width),
82
88
  JSI_EXPORT_FUNC(JsiSkSurface, height),
83
89
  JSI_EXPORT_FUNC(JsiSkSurface, getCanvas),
84
90
  JSI_EXPORT_FUNC(JsiSkSurface, makeImageSnapshot),
85
91
  JSI_EXPORT_FUNC(JsiSkSurface, flush),
92
+ JSI_EXPORT_FUNC(JsiSkSurface, getNativeTextureUnstable),
86
93
  JSI_EXPORT_FUNC(JsiSkSurface, dispose))
87
94
  };
88
95
 
@@ -0,0 +1,53 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ #include "RNSkPlatformContext.h"
6
+
7
+ namespace jsi = facebook::jsi;
8
+ namespace react = facebook::react;
9
+
10
+ namespace RNSkia {
11
+
12
+ namespace JsiTextureInfo {
13
+
14
+ inline jsi::Value toValue(jsi::Runtime &runtime, const TextureInfo &texInfo) {
15
+ jsi::Object textureInfo(runtime);
16
+ textureInfo.setProperty(
17
+ runtime, "mtlTexture",
18
+ jsi::BigInt::fromUint64(runtime,
19
+ reinterpret_cast<uint64_t>(texInfo.mtlTexture)));
20
+ textureInfo.setProperty(runtime, "glTarget",
21
+ static_cast<int>(texInfo.glTarget));
22
+ textureInfo.setProperty(runtime, "glID", static_cast<int>(texInfo.glID));
23
+ textureInfo.setProperty(runtime, "glFormat",
24
+ static_cast<int>(texInfo.glFormat));
25
+ textureInfo.setProperty(runtime, "glProtected",
26
+ static_cast<int>(texInfo.glProtected));
27
+ return textureInfo;
28
+ }
29
+
30
+ inline TextureInfo fromValue(jsi::Runtime &runtime, const jsi::Value &value) {
31
+ auto object = value.getObject(runtime);
32
+ TextureInfo texInfo;
33
+ if (object.hasProperty(runtime, "mtlTexture")) {
34
+ texInfo.mtlTexture =
35
+ reinterpret_cast<const void *>(object.getProperty(runtime, "mtlTexture")
36
+ .asBigInt(runtime)
37
+ .asUint64(runtime));
38
+ }
39
+ if (object.hasProperty(runtime, "glID")) {
40
+ texInfo.glTarget = static_cast<unsigned int>(
41
+ object.getProperty(runtime, "glTarget").asNumber());
42
+ texInfo.glID = static_cast<unsigned int>(
43
+ object.getProperty(runtime, "glID").asNumber());
44
+ texInfo.glFormat = static_cast<unsigned int>(
45
+ object.getProperty(runtime, "glFormat").asNumber());
46
+ texInfo.glProtected =
47
+ object.getProperty(runtime, "glProtected").asNumber() != 0;
48
+ }
49
+ return texInfo;
50
+ }
51
+
52
+ } // namespace JsiTextureInfo
53
+ } // namespace RNSkia
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <memory>
5
+ #include <string>
6
+
7
+ #include "JsiDomRenderNode.h"
8
+ #include "JsiSkPicture.h"
9
+
10
+ namespace RNJsi {
11
+ namespace jsi = facebook::jsi;
12
+
13
+ class ViewProperty {
14
+ public:
15
+ ViewProperty(jsi::Runtime &runtime, const jsi::Value &value) {
16
+ if (value.isObject()) {
17
+ auto object = value.asObject(runtime);
18
+ if (object.isHostObject(runtime)) {
19
+ auto hostObject = object.asHostObject(runtime);
20
+ auto dom =
21
+ std::dynamic_pointer_cast<RNSkia::JsiDomRenderNode>(hostObject);
22
+ if (dom) {
23
+ _value = dom;
24
+ } else {
25
+ auto jsiPicture =
26
+ std::dynamic_pointer_cast<RNSkia::JsiSkPicture>(hostObject);
27
+ if (jsiPicture) {
28
+ _value = jsiPicture->getObject();
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ bool isNull() { return std::holds_alternative<nullptr_t>(_value); }
36
+
37
+ std::shared_ptr<RNSkia::JsiDomRenderNode> getDomRenderNode() {
38
+ return std::get<std::shared_ptr<RNSkia::JsiDomRenderNode>>(_value);
39
+ }
40
+
41
+ sk_sp<SkPicture> getPicture() { return std::get<sk_sp<SkPicture>>(_value); }
42
+
43
+ private:
44
+ std::variant<nullptr_t, sk_sp<SkPicture>,
45
+ std::shared_ptr<RNSkia::JsiDomRenderNode>>
46
+ _value = nullptr;
47
+ };
48
+ } // namespace RNJsi
@@ -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 "JsiDomRenderNode.h"
16
16
  #include "RNSkLog.h"
@@ -75,20 +75,17 @@ public:
75
75
  std::bind(&RNSkView::requestRedraw, this), context)) {}
76
76
 
77
77
  void setJsiProperties(
78
- std::unordered_map<std::string, JsiValueWrapper> &props) override {
79
-
80
- RNSkView::setJsiProperties(props);
78
+ std::unordered_map<std::string, ViewProperty> &props) override {
81
79
 
82
80
  for (auto &prop : props) {
83
81
  if (prop.first == "root") {
84
82
  // Save root
85
- if (prop.second.isUndefined() || prop.second.isNull()) {
83
+ if (prop.second.isNull()) {
86
84
  std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
87
85
  ->setRoot(nullptr);
88
86
  } else {
89
87
  std::static_pointer_cast<RNSkDomRenderer>(getRenderer())
90
- ->setRoot(std::dynamic_pointer_cast<JsiDomRenderNode>(
91
- prop.second.getAsHostObject()));
88
+ ->setRoot(prop.second.getDomRenderNode());
92
89
  }
93
90
 
94
91
  // Request redraw
@@ -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,19 +40,16 @@ class RNSkPictureRenderer
40
40
  public:
41
41
  RNSkPictureRenderer(std::function<void()> requestRedraw,
42
42
  std::shared_ptr<RNSkPlatformContext> context)
43
- : RNSkRenderer(std::move(requestRedraw)), _platformContext(std::move(context)) {}
43
+ : RNSkRenderer(std::move(requestRedraw)),
44
+ _platformContext(std::move(context)) {}
44
45
 
45
46
  void
46
47
  renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
47
48
  performDraw(canvasProvider);
48
49
  }
49
50
 
50
- void setPicture(std::shared_ptr<jsi::HostObject> picture) {
51
- if (picture == nullptr) {
52
- _picture = nullptr;
53
- } else {
54
- _picture = std::dynamic_pointer_cast<JsiSkPicture>(picture)->getObject();
55
- }
51
+ void setPicture(sk_sp<SkPicture> picture) {
52
+ _picture = picture;
56
53
  _requestRedraw();
57
54
  }
58
55
 
@@ -88,27 +85,20 @@ public:
88
85
  std::bind(&RNSkPictureView::requestRedraw, this), context)) {}
89
86
 
90
87
  void setJsiProperties(
91
- std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) override {
92
-
93
- RNSkView::setJsiProperties(props);
88
+ std::unordered_map<std::string, RNJsi::ViewProperty> &props) override {
94
89
 
95
90
  for (auto &prop : props) {
96
91
  if (prop.first == "picture") {
97
- if (prop.second.isUndefinedOrNull()) {
92
+ if (prop.second.isNull()) {
98
93
  // Clear picture
99
94
  std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())
100
95
  ->setPicture(nullptr);
101
96
  continue;
102
- } else if (prop.second.getType() !=
103
- RNJsi::JsiWrapperValueType::HostObject) {
104
- // We expect a function for the picture custom property
105
- throw std::runtime_error(
106
- "Expected an object for the picture custom property.");
107
97
  }
108
98
 
109
99
  // Save picture
110
100
  std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())
111
- ->setPicture(prop.second.getAsHostObject());
101
+ ->setPicture(prop.second.getPicture());
112
102
  }
113
103
  }
114
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"
@@ -143,14 +143,8 @@ public:
143
143
  */
144
144
  virtual ~RNSkView() {}
145
145
 
146
- /**
147
- Sets custom properties. Custom properties are properties that are set
148
- directly from Javascript without having to go through the async bridge.
149
- */
150
146
  virtual void setJsiProperties(
151
- std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) {
152
- // Nothing here...
153
- }
147
+ std::unordered_map<std::string, RNJsi::ViewProperty> &props) = 0;
154
148
 
155
149
  void requestRedraw() {
156
150
  if (!_redrawRequested) {