@shopify/react-native-skia 1.6.0 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. package/android/cpp/jni/include/JniSkiaBaseView.h +2 -3
  2. package/android/cpp/jni/include/JniSkiaManager.h +2 -2
  3. package/android/cpp/rnskia-android/GrAHardwareBufferUtils.cpp +14 -14
  4. package/android/cpp/rnskia-android/MainThreadDispatcher.h +4 -6
  5. package/android/cpp/rnskia-android/OpenGLContext.h +24 -10
  6. package/android/cpp/rnskia-android/OpenGLWindowContext.h +3 -3
  7. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +64 -8
  8. package/android/cpp/rnskia-android/RNSkAndroidVideo.cpp +1 -1
  9. package/android/cpp/rnskia-android/RNSkAndroidVideo.h +1 -1
  10. package/android/cpp/rnskia-android/RNSkAndroidView.h +0 -4
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +3 -3
  12. package/android/cpp/rnskia-android/gl/Display.h +2 -0
  13. package/cpp/api/JsiSkApi.h +1 -1
  14. package/cpp/api/JsiSkCanvas.h +1 -1
  15. package/cpp/api/JsiSkColor.h +2 -2
  16. package/cpp/api/JsiSkContourMeasure.h +1 -5
  17. package/cpp/api/JsiSkImage.h +11 -0
  18. package/cpp/api/JsiSkImageFactory.h +14 -0
  19. package/cpp/api/JsiSkPicture.h +2 -0
  20. package/cpp/api/JsiSkSurface.h +7 -0
  21. package/cpp/api/JsiTextureInfo.h +53 -0
  22. package/cpp/api/third_party/CSSColorParser.h +1 -1
  23. package/cpp/api/third_party/base64.cpp +4 -4
  24. package/cpp/jsi/ViewProperty.h +48 -0
  25. package/cpp/rnskia/RNSkDomView.h +4 -7
  26. package/cpp/rnskia/RNSkJsiViewApi.h +3 -3
  27. package/cpp/rnskia/RNSkManager.cpp +1 -1
  28. package/cpp/rnskia/RNSkPictureView.h +8 -18
  29. package/cpp/rnskia/RNSkPlatformContext.h +18 -13
  30. package/cpp/rnskia/RNSkView.h +2 -8
  31. package/ios/RNSkia-iOS/MetalContext.h +101 -15
  32. package/ios/RNSkia-iOS/MetalContext.mm +9 -8
  33. package/ios/RNSkia-iOS/MetalWindowContext.h +39 -0
  34. package/ios/RNSkia-iOS/MetalWindowContext.mm +60 -0
  35. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +13 -29
  36. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +88 -2
  37. package/ios/RNSkia-iOS/SkiaCVPixelBufferUtils.mm +17 -6
  38. package/ios/RNSkia-iOS/SkiaDomView.mm +3 -3
  39. package/ios/RNSkia-iOS/SkiaDomViewManager.mm +2 -1
  40. package/ios/RNSkia-iOS/SkiaManager.h +6 -0
  41. package/ios/RNSkia-iOS/SkiaManager.mm +18 -2
  42. package/ios/RNSkia-iOS/SkiaPictureView.mm +3 -3
  43. package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +2 -1
  44. package/ios/RNSkia-iOS/SkiaUIView.h +0 -1
  45. package/ios/RNSkia-iOS/SkiaUIView.mm +18 -22
  46. package/lib/commonjs/skia/types/Image/Image.d.ts +10 -0
  47. package/lib/commonjs/skia/types/Image/Image.js.map +1 -1
  48. package/lib/commonjs/skia/types/Image/ImageFactory.d.ts +18 -0
  49. package/lib/commonjs/skia/types/Image/ImageFactory.js.map +1 -1
  50. package/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  51. package/lib/commonjs/skia/types/Matrix4.js +69 -1
  52. package/lib/commonjs/skia/types/Matrix4.js.map +1 -1
  53. package/lib/commonjs/skia/types/Surface/Surface.d.ts +11 -0
  54. package/lib/commonjs/skia/types/Surface/Surface.js.map +1 -1
  55. package/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  56. package/lib/commonjs/skia/web/JsiSkImage.js +4 -0
  57. package/lib/commonjs/skia/web/JsiSkImage.js.map +1 -1
  58. package/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  59. package/lib/commonjs/skia/web/JsiSkImageFactory.js +3 -0
  60. package/lib/commonjs/skia/web/JsiSkImageFactory.js.map +1 -1
  61. package/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  62. package/lib/commonjs/skia/web/JsiSkSurface.js +4 -0
  63. package/lib/commonjs/skia/web/JsiSkSurface.js.map +1 -1
  64. package/lib/module/skia/types/Image/Image.d.ts +10 -0
  65. package/lib/module/skia/types/Image/Image.js.map +1 -1
  66. package/lib/module/skia/types/Image/ImageFactory.d.ts +18 -0
  67. package/lib/module/skia/types/Image/ImageFactory.js.map +1 -1
  68. package/lib/module/skia/types/Matrix4.d.ts +6 -0
  69. package/lib/module/skia/types/Matrix4.js +67 -0
  70. package/lib/module/skia/types/Matrix4.js.map +1 -1
  71. package/lib/module/skia/types/Surface/Surface.d.ts +11 -0
  72. package/lib/module/skia/types/Surface/Surface.js.map +1 -1
  73. package/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  74. package/lib/module/skia/web/JsiSkImage.js +4 -0
  75. package/lib/module/skia/web/JsiSkImage.js.map +1 -1
  76. package/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  77. package/lib/module/skia/web/JsiSkImageFactory.js +3 -0
  78. package/lib/module/skia/web/JsiSkImageFactory.js.map +1 -1
  79. package/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  80. package/lib/module/skia/web/JsiSkSurface.js +4 -0
  81. package/lib/module/skia/web/JsiSkSurface.js.map +1 -1
  82. package/lib/typescript/lib/commonjs/skia/types/Matrix4.d.ts +6 -0
  83. package/lib/typescript/lib/commonjs/skia/web/JsiSkImage.d.ts +1 -0
  84. package/lib/typescript/lib/commonjs/skia/web/JsiSkImageFactory.d.ts +1 -0
  85. package/lib/typescript/lib/commonjs/skia/web/JsiSkSurface.d.ts +1 -0
  86. package/lib/typescript/lib/module/mock/index.d.ts +1 -0
  87. package/lib/typescript/lib/module/skia/types/Matrix4.d.ts +1 -0
  88. package/lib/typescript/lib/module/skia/web/JsiSkImage.d.ts +1 -0
  89. package/lib/typescript/lib/module/skia/web/JsiSkImageFactory.d.ts +1 -0
  90. package/lib/typescript/lib/module/skia/web/JsiSkSurface.d.ts +1 -0
  91. package/lib/typescript/src/skia/types/Image/Image.d.ts +10 -0
  92. package/lib/typescript/src/skia/types/Image/ImageFactory.d.ts +18 -0
  93. package/lib/typescript/src/skia/types/Matrix4.d.ts +6 -0
  94. package/lib/typescript/src/skia/types/Surface/Surface.d.ts +11 -0
  95. package/lib/typescript/src/skia/web/JsiSkImage.d.ts +1 -0
  96. package/lib/typescript/src/skia/web/JsiSkImageFactory.d.ts +1 -0
  97. package/lib/typescript/src/skia/web/JsiSkSurface.d.ts +1 -0
  98. package/package.json +1 -1
  99. package/src/renderer/__tests__/e2e/Matrix4.spec.tsx +93 -0
  100. package/src/skia/types/Image/Image.ts +11 -0
  101. package/src/skia/types/Image/ImageFactory.ts +24 -0
  102. package/src/skia/types/Matrix4.ts +101 -0
  103. package/src/skia/types/Surface/Surface.ts +12 -0
  104. package/src/skia/web/JsiSkImage.ts +5 -0
  105. package/src/skia/web/JsiSkImageFactory.ts +4 -0
  106. package/src/skia/web/JsiSkSurface.ts +5 -0
  107. package/cpp/jsi/JsiValueWrapper.h +0 -164
  108. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.h +0 -128
  109. package/ios/RNSkia-iOS/SkiaMetalSurfaceFactory.mm +0 -92
@@ -20,9 +20,9 @@ class JniSkiaBaseView {
20
20
  public:
21
21
  JniSkiaBaseView(jni::alias_ref<JniSkiaManager::javaobject> skiaManager,
22
22
  std::shared_ptr<RNSkBaseAndroidView> skiaView)
23
- : _manager(skiaManager->cthis()), _skiaAndroidView(skiaView) {}
23
+ : _manager(skiaManager->cthis()), _skiaAndroidView(std::move(skiaView)) {}
24
24
 
25
- ~JniSkiaBaseView() {}
25
+ ~JniSkiaBaseView() = default;
26
26
 
27
27
  std::shared_ptr<RNSkManager> getSkiaManager() {
28
28
  return _manager->getSkiaManager();
@@ -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;
@@ -41,7 +41,7 @@ GrBackendFormat GetGLBackendFormat(GrDirectContext *dContext,
41
41
  bool requireKnownFormat) {
42
42
  GrBackendApi backend = dContext->backend();
43
43
  if (backend != GrBackendApi::kOpenGL) {
44
- return GrBackendFormat();
44
+ return {};
45
45
  }
46
46
  switch (bufferFormat) {
47
47
  // TODO: find out if we can detect, which graphic buffers support
@@ -63,7 +63,7 @@ GrBackendFormat GetGLBackendFormat(GrDirectContext *dContext,
63
63
  #endif
64
64
  default:
65
65
  if (requireKnownFormat) {
66
- return GrBackendFormat();
66
+ return {};
67
67
  } else {
68
68
  return GrBackendFormats::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL);
69
69
  }
@@ -110,12 +110,12 @@ void GLTextureHelper::rebind(GrDirectContext *dContext) {
110
110
  }
111
111
 
112
112
  void delete_gl_texture(void *context) {
113
- GLTextureHelper *cleanupHelper = static_cast<GLTextureHelper *>(context);
113
+ auto cleanupHelper = static_cast<GLTextureHelper *>(context);
114
114
  delete cleanupHelper;
115
115
  }
116
116
 
117
117
  void update_gl_texture(void *context, GrDirectContext *dContext) {
118
- GLTextureHelper *cleanupHelper = static_cast<GLTextureHelper *>(context);
118
+ auto cleanupHelper = static_cast<GLTextureHelper *>(context);
119
119
  cleanupHelper->rebind(dContext);
120
120
  }
121
121
 
@@ -127,13 +127,13 @@ static GrBackendTexture make_gl_backend_texture(
127
127
  while (GL_NO_ERROR != glGetError()) {
128
128
  } // clear GL errors
129
129
 
130
- EGLGetNativeClientBufferANDROIDProc eglGetNativeClientBufferANDROID =
130
+ auto eglGetNativeClientBufferANDROID =
131
131
  (EGLGetNativeClientBufferANDROIDProc)eglGetProcAddress(
132
132
  "eglGetNativeClientBufferANDROID");
133
133
  if (!eglGetNativeClientBufferANDROID) {
134
134
  RNSkLogger::logToConsole(
135
135
  "Failed to get the eglGetNativeClientBufferAndroid proc");
136
- return GrBackendTexture();
136
+ return {};
137
137
  }
138
138
 
139
139
  EGLClientBuffer clientBuffer =
@@ -149,14 +149,14 @@ static GrBackendTexture make_gl_backend_texture(
149
149
  if (EGL_NO_IMAGE_KHR == image) {
150
150
  SkDebugf("Could not create EGL image, err = (%#x)",
151
151
  static_cast<int>(eglGetError()));
152
- return GrBackendTexture();
152
+ return {};
153
153
  }
154
154
 
155
155
  GrGLuint texID;
156
156
  glGenTextures(1, &texID);
157
157
  if (!texID) {
158
158
  eglDestroyImageKHR(display, image);
159
- return GrBackendTexture();
159
+ return {};
160
160
  }
161
161
 
162
162
  GrGLuint target = isRenderable ? GR_GL_TEXTURE_2D : GR_GL_TEXTURE_EXTERNAL;
@@ -167,7 +167,7 @@ static GrBackendTexture make_gl_backend_texture(
167
167
  SkDebugf("glBindTexture failed (%#x)", static_cast<int>(status));
168
168
  glDeleteTextures(1, &texID);
169
169
  eglDestroyImageKHR(display, image);
170
- return GrBackendTexture();
170
+ return {};
171
171
  }
172
172
  glEGLImageTargetTexture2DOES(target, image);
173
173
  if ((status = glGetError()) != GL_NO_ERROR) {
@@ -175,7 +175,7 @@ static GrBackendTexture make_gl_backend_texture(
175
175
  static_cast<int>(status));
176
176
  glDeleteTextures(1, &texID);
177
177
  eglDestroyImageKHR(display, image);
178
- return GrBackendTexture();
178
+ return {};
179
179
  }
180
180
  dContext->resetContext(kTextureBinding_GrGLBackendState);
181
181
 
@@ -223,16 +223,16 @@ MakeGLBackendTexture(GrDirectContext *dContext, AHardwareBuffer *hardwareBuffer,
223
223
  bool isProtectedContent,
224
224
  const GrBackendFormat &backendFormat, bool isRenderable) {
225
225
  SkASSERT(dContext);
226
- if (!dContext || dContext->abandoned()) {
227
- return GrBackendTexture();
226
+ if (dContext->abandoned()) {
227
+ return {};
228
228
  }
229
229
 
230
230
  if (GrBackendApi::kOpenGL != dContext->backend()) {
231
- return GrBackendTexture();
231
+ return {};
232
232
  }
233
233
 
234
234
  if (isProtectedContent && !can_import_protected_content(dContext)) {
235
- return GrBackendTexture();
235
+ return {};
236
236
  }
237
237
 
238
238
  return make_gl_backend_texture(
@@ -1,13 +1,13 @@
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:
9
- ALooper *mainLooper;
10
- int messagePipe[2];
9
+ ALooper *mainLooper = nullptr;
10
+ int messagePipe[2] = { -1, -1 };
11
11
  std::queue<std::function<void()>> taskQueue;
12
12
  std::mutex queueMutex;
13
13
 
@@ -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
 
@@ -111,17 +117,23 @@ public:
111
117
  // GR_GL_TEXTURE_2D
112
118
  case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
113
119
  format = GrBackendFormats::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL);
120
+ break;
114
121
  case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
115
122
  format = GrBackendFormats::MakeGL(GR_GL_RGBA16F, GR_GL_TEXTURE_EXTERNAL);
123
+ break;
116
124
  case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
117
- format = GrBackendFormats::MakeGL(GR_GL_RGB565, GR_GL_TEXTURE_EXTERNAL);
125
+ GrBackendFormats::MakeGL(GR_GL_RGB565, GR_GL_TEXTURE_EXTERNAL);
126
+ break;
118
127
  case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
119
128
  format = GrBackendFormats::MakeGL(GR_GL_RGB10_A2, GR_GL_TEXTURE_EXTERNAL);
129
+ break;
120
130
  case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
121
131
  format = GrBackendFormats::MakeGL(GR_GL_RGB8, GR_GL_TEXTURE_EXTERNAL);
132
+ break;
122
133
  #if __ANDROID_API__ >= 33
123
134
  case AHARDWAREBUFFER_FORMAT_R8_UNORM:
124
135
  format = GrBackendFormats::MakeGL(GR_GL_R8, GR_GL_TEXTURE_EXTERNAL);
136
+ break;
125
137
  #endif
126
138
  default:
127
139
  if (requireKnownFormat) {
@@ -130,10 +142,11 @@ public:
130
142
  format = GrBackendFormats::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL);
131
143
  }
132
144
  }
133
-
145
+ auto width = static_cast<int>(description.width);
146
+ auto height = static_cast<int>(description.height);
134
147
  auto backendTex = MakeGLBackendTexture(
135
148
  _directContext.get(), const_cast<AHardwareBuffer *>(hardwareBuffer),
136
- description.width, description.height, &deleteImageProc,
149
+ width, height, &deleteImageProc,
137
150
  &updateImageProc, &deleteImageCtx, false, format, false);
138
151
  if (!backendTex.isValid()) {
139
152
  RNSkLogger::logToConsole(
@@ -153,14 +166,15 @@ public:
153
166
  }
154
167
 
155
168
  // TODO: remove width, height
156
- std::unique_ptr<WindowContext> MakeWindow(ANativeWindow *window, int width,
157
- int height) {
169
+ std::unique_ptr<WindowContext> MakeWindow(ANativeWindow *window) {
158
170
  auto display = OpenGLSharedContext::getInstance().getDisplay();
159
- return std::make_unique<OpenGLWindowContext>(_directContext.get(), display,
160
- _glContext.get(), window);
171
+ return std::make_unique<OpenGLWindowContext>(
172
+ _directContext.get(), display, _glContext.get(), window,
173
+ OpenGLSharedContext::getInstance().getConfig());
161
174
  }
162
175
 
163
176
  GrDirectContext *getDirectContext() { return _directContext.get(); }
177
+ void makeCurrent() { _glContext->makeCurrent(_glSurface.get()); }
164
178
 
165
179
  private:
166
180
  std::unique_ptr<gl::Context> _glContext;
@@ -170,7 +184,7 @@ private:
170
184
  OpenGLContext() {
171
185
  auto display = OpenGLSharedContext::getInstance().getDisplay();
172
186
  auto sharedContext = OpenGLSharedContext::getInstance().getContext();
173
- auto glConfig = display->chooseConfig();
187
+ auto glConfig = OpenGLSharedContext::getInstance().getConfig();
174
188
  _glContext = display->makeContext(glConfig, sharedContext);
175
189
  _glSurface = display->makePixelBufferSurface(glConfig, 1, 1);
176
190
  _glContext->makeCurrent(_glSurface.get());
@@ -34,15 +34,15 @@ 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
 
45
- ~OpenGLWindowContext() {
45
+ ~OpenGLWindowContext() override {
46
46
  _skSurface = nullptr;
47
47
  _glSurface = nullptr;
48
48
  ANativeWindow_release(_window);
@@ -1,6 +1,5 @@
1
1
  #pragma once
2
2
 
3
- // TODO: Add android flags
4
3
  #if __ANDROID_API__ >= 26
5
4
  #include <android/hardware_buffer.h>
6
5
  #endif
@@ -20,7 +19,6 @@
20
19
  #include "MainThreadDispatcher.h"
21
20
  #include "RNSkAndroidVideo.h"
22
21
  #include "RNSkPlatformContext.h"
23
-
24
22
  #pragma clang diagnostic push
25
23
  #pragma clang diagnostic ignored "-Wdocumentation"
26
24
 
@@ -29,18 +27,17 @@
29
27
  #pragma clang diagnostic pop
30
28
 
31
29
  namespace RNSkia {
32
- namespace jsi = facebook::jsi;
33
30
 
34
31
  class RNSkAndroidPlatformContext : public RNSkPlatformContext {
35
32
  public:
36
33
  RNSkAndroidPlatformContext(
37
- JniPlatformContext *jniPlatformContext, jsi::Runtime *runtime,
34
+ JniPlatformContext *jniPlatformContext,
38
35
  std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
39
- : RNSkPlatformContext(runtime, jsCallInvoker,
36
+ : RNSkPlatformContext(std::move(jsCallInvoker),
40
37
  jniPlatformContext->getPixelDensity()),
41
38
  _jniPlatformContext(jniPlatformContext) {}
42
39
 
43
- ~RNSkAndroidPlatformContext() {}
40
+ ~RNSkAndroidPlatformContext() override = default;
44
41
 
45
42
  void performStreamOperation(
46
43
  const std::string &sourceUri,
@@ -66,7 +63,7 @@ public:
66
63
  return DawnContext::getInstance().MakeWindow(surface, width, height);
67
64
  #else
68
65
  auto aWindow = reinterpret_cast<ANativeWindow *>(surface);
69
- return OpenGLContext::getInstance().MakeWindow(aWindow, width, height);
66
+ return OpenGLContext::getInstance().MakeWindow(aWindow);
70
67
  #endif
71
68
  }
72
69
 
@@ -78,6 +75,31 @@ public:
78
75
  #endif
79
76
  }
80
77
 
78
+ sk_sp<SkImage> makeImageFromNativeTexture(const TextureInfo &texInfo,
79
+ int width, int height,
80
+ bool mipMapped) override {
81
+ GrGLTextureInfo textureInfo;
82
+ textureInfo.fTarget = (GrGLenum)texInfo.glTarget;
83
+ textureInfo.fID = (GrGLuint)texInfo.glID;
84
+ textureInfo.fFormat = (GrGLenum)texInfo.glFormat;
85
+ textureInfo.fProtected =
86
+ texInfo.glProtected ? skgpu::Protected::kYes : skgpu::Protected::kNo;
87
+
88
+ OpenGLContext::getInstance().makeCurrent();
89
+ if (glIsTexture(textureInfo.fID) == GL_FALSE) {
90
+ throw std::runtime_error("Invalid textureInfo");
91
+ }
92
+
93
+ GrBackendTexture backendTexture = GrBackendTextures::MakeGL(
94
+ width, height,
95
+ mipMapped ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo,
96
+ textureInfo);
97
+ return SkImages::BorrowTextureFrom(
98
+ OpenGLContext::getInstance().getDirectContext(), backendTexture,
99
+ kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType,
100
+ nullptr);
101
+ }
102
+
81
103
  std::shared_ptr<RNSkVideo> createVideo(const std::string &url) override {
82
104
  auto jniVideo = _jniPlatformContext->createVideo(url);
83
105
  return std::make_shared<RNSkAndroidVideo>(jniVideo);
@@ -85,7 +107,7 @@ public:
85
107
 
86
108
  void releaseNativeBuffer(uint64_t pointer) override {
87
109
  #if __ANDROID_API__ >= 26
88
- AHardwareBuffer *buffer = reinterpret_cast<AHardwareBuffer *>(pointer);
110
+ auto *buffer = reinterpret_cast<AHardwareBuffer *>(pointer);
89
111
  AHardwareBuffer_release(buffer);
90
112
  #endif
91
113
  }
@@ -149,6 +171,40 @@ public:
149
171
  #endif
150
172
  }
151
173
 
174
+ const TextureInfo getTexture(sk_sp<SkImage> image) override {
175
+ GrBackendTexture texture;
176
+ if (!SkImages::GetBackendTextureFromImage(image, &texture, true)) {
177
+ throw std::runtime_error("Couldn't get backend texture from image.");
178
+ }
179
+ return getTextureInfo(texture);
180
+ }
181
+
182
+ const TextureInfo getTexture(sk_sp<SkSurface> surface) override {
183
+ GrBackendTexture texture = SkSurfaces::GetBackendTexture(
184
+ surface.get(), SkSurface::BackendHandleAccess::kFlushRead);
185
+ return getTextureInfo(texture);
186
+ }
187
+
188
+ static TextureInfo getTextureInfo(const GrBackendTexture &texture) {
189
+ if (!texture.isValid()) {
190
+ throw std::runtime_error("invalid backend texture");
191
+ }
192
+ GrGLTextureInfo textureInfo;
193
+ if (!GrBackendTextures::GetGLTextureInfo(texture, &textureInfo)) {
194
+ throw std::runtime_error("couldn't get OpenGL texture");
195
+ }
196
+
197
+ OpenGLContext::getInstance().makeCurrent();
198
+ glFlush();
199
+
200
+ TextureInfo texInfo;
201
+ texInfo.glProtected = textureInfo.isProtected();
202
+ texInfo.glID = textureInfo.fID;
203
+ texInfo.glFormat = textureInfo.fFormat;
204
+ texInfo.glTarget = textureInfo.fTarget;
205
+ return texInfo;
206
+ }
207
+
152
208
  #if !defined(SK_GRAPHITE)
153
209
  GrDirectContext *getDirectContext() override {
154
210
  return OpenGLContext::getInstance().getDirectContext();
@@ -35,7 +35,7 @@ RNSkAndroidVideo::RNSkAndroidVideo(jni::global_ref<jobject> jniVideo)
35
35
  #endif
36
36
  }
37
37
 
38
- RNSkAndroidVideo::~RNSkAndroidVideo() {}
38
+ RNSkAndroidVideo::~RNSkAndroidVideo() = default;
39
39
 
40
40
  sk_sp<SkImage> RNSkAndroidVideo::nextImage(double *timeStamp) {
41
41
  #if __ANDROID_API__ >= 26
@@ -26,7 +26,7 @@ private:
26
26
 
27
27
  public:
28
28
  explicit RNSkAndroidVideo(jni::global_ref<jobject> jniVideo);
29
- ~RNSkAndroidVideo();
29
+ ~RNSkAndroidVideo() override;
30
30
  sk_sp<SkImage> nextImage(double *timeStamp = nullptr) override;
31
31
  double duration() override;
32
32
  double framerate() override;
@@ -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
  }
@@ -26,9 +26,9 @@ namespace RNSkia {
26
26
  RNSkOpenGLCanvasProvider::RNSkOpenGLCanvasProvider(
27
27
  std::function<void()> requestRedraw,
28
28
  std::shared_ptr<RNSkia::RNSkPlatformContext> platformContext)
29
- : RNSkCanvasProvider(requestRedraw), _platformContext(platformContext) {}
29
+ : RNSkCanvasProvider(std::move(requestRedraw)), _platformContext(std::move(platformContext)) {}
30
30
 
31
- RNSkOpenGLCanvasProvider::~RNSkOpenGLCanvasProvider() {}
31
+ RNSkOpenGLCanvasProvider::~RNSkOpenGLCanvasProvider() = default;
32
32
 
33
33
  float RNSkOpenGLCanvasProvider::getScaledWidth() {
34
34
  if (_surfaceHolder) {
@@ -110,7 +110,7 @@ void RNSkOpenGLCanvasProvider::surfaceAvailable(jobject jSurfaceTexture,
110
110
  _surfaceHolder = DawnContext::getInstance().MakeWindow(window, width, height);
111
111
  #else
112
112
  _surfaceHolder =
113
- OpenGLContext::getInstance().MakeWindow(window, width, height);
113
+ OpenGLContext::getInstance().MakeWindow(window);
114
114
  #endif
115
115
 
116
116
  // Post redraw request to ensure we paint in the next draw cycle.
@@ -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,
@@ -64,7 +64,7 @@ public:
64
64
  * and provide functions for accessing and creating the Skia wrapper objects
65
65
  * @param context Platform context
66
66
  */
67
- JsiSkApi(jsi::Runtime &runtime, std::shared_ptr<RNSkPlatformContext> context)
67
+ explicit JsiSkApi(const std::shared_ptr<RNSkPlatformContext> &context)
68
68
  : JsiSkHostObject(context) {
69
69
  // We create the system font manager eagerly since it has proven to be too
70
70
  // slow to do it on demand
@@ -83,7 +83,7 @@ public:
83
83
  auto src = JsiSkRect::fromValue(runtime, arguments[1]);
84
84
  auto dest = JsiSkRect::fromValue(runtime, arguments[2]);
85
85
  auto paint = JsiSkPaint::fromValue(runtime, arguments[3]);
86
- auto fastSample = count < 5 ? false : arguments[4].getBool();
86
+ auto fastSample = count >= 5 && arguments[4].getBool();
87
87
  _canvas->drawImageRect(image, *src, *dest, SkSamplingOptions(), paint.get(),
88
88
  fastSample ? SkCanvas::kFast_SrcRectConstraint
89
89
  : SkCanvas::kStrict_SrcRectConstraint);
@@ -24,7 +24,7 @@ class JsiSkColor : public RNJsi::JsiHostObject {
24
24
  public:
25
25
  JsiSkColor() : JsiHostObject() {}
26
26
 
27
- ~JsiSkColor() {}
27
+ ~JsiSkColor() override = default;
28
28
 
29
29
  static jsi::Object toValue(jsi::Runtime &runtime, SkColor color) {
30
30
  auto result = runtime.global()
@@ -63,7 +63,7 @@ public:
63
63
  * @return A function for creating a new host object wrapper for the SkColor
64
64
  * class
65
65
  */
66
- static const jsi::HostFunctionType createCtor() {
66
+ static jsi::HostFunctionType createCtor() {
67
67
  return JSI_HOST_FUNCTION_LAMBDA {
68
68
  if (arguments[0].isNumber()) {
69
69
  return JsiSkColor::toValue(runtime, arguments[0].getNumber());
@@ -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