@shopify/react-native-skia 1.5.7 → 1.5.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. package/android/CMakeLists.txt +1 -2
  2. package/android/cpp/jni/JniPlatformContext.cpp +0 -49
  3. package/android/cpp/jni/include/JniPlatformContext.h +1 -17
  4. package/android/cpp/jni/include/JniSkiaBaseView.h +0 -2
  5. package/android/cpp/jni/include/JniSkiaDomView.h +0 -3
  6. package/android/cpp/jni/include/JniSkiaManager.h +0 -2
  7. package/android/cpp/jni/include/JniSkiaPictureView.h +0 -3
  8. package/android/cpp/rnskia-android/MainThreadDispatcher.h +69 -0
  9. package/android/cpp/rnskia-android/OpenGLContext.h +3 -1
  10. package/android/cpp/rnskia-android/OpenGLWindowContext.cpp +1 -1
  11. package/android/cpp/rnskia-android/OpenGLWindowContext.h +4 -5
  12. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +10 -11
  13. package/android/cpp/rnskia-android/RNSkAndroidView.h +3 -13
  14. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +0 -1
  15. package/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java +0 -85
  16. package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaModule.java +1 -21
  17. package/android/src/main/java/com/shopify/reactnative/skia/SkiaManager.java +0 -4
  18. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDomViewManagerDelegate.java +1 -4
  19. package/android/src/paper/java/com/facebook/react/viewmanagers/SkiaDomViewManagerInterface.java +0 -1
  20. package/cpp/api/JsiSkImage.h +20 -4
  21. package/cpp/rnskia/RNSkManager.cpp +2 -3
  22. package/cpp/rnskia/RNSkPlatformContext.h +6 -114
  23. package/cpp/rnskia/RNSkView.h +21 -77
  24. package/ios/RNSkia-iOS/MetalContext.h +2 -0
  25. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +4 -10
  26. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.mm +6 -16
  27. package/ios/RNSkia-iOS/SkiaDomView.mm +0 -1
  28. package/ios/RNSkia-iOS/SkiaDomViewManager.mm +0 -6
  29. package/ios/RNSkia-iOS/SkiaPictureView.mm +0 -1
  30. package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +0 -6
  31. package/ios/RNSkia-iOS/SkiaUIView.h +0 -1
  32. package/ios/RNSkia-iOS/SkiaUIView.mm +1 -14
  33. package/lib/commonjs/renderer/Canvas.d.ts +1 -0
  34. package/lib/commonjs/renderer/Canvas.js +5 -5
  35. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  36. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.d.ts +0 -1
  37. package/lib/commonjs/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  38. package/lib/commonjs/views/SkiaBaseWebView.d.ts +1 -12
  39. package/lib/commonjs/views/SkiaBaseWebView.js +1 -19
  40. package/lib/commonjs/views/SkiaBaseWebView.js.map +1 -1
  41. package/lib/commonjs/views/SkiaDomView.d.ts +8 -1
  42. package/lib/commonjs/views/SkiaDomView.js +14 -2
  43. package/lib/commonjs/views/SkiaDomView.js.map +1 -1
  44. package/lib/commonjs/views/SkiaDomView.web.d.ts +3 -3
  45. package/lib/commonjs/views/SkiaDomView.web.js.map +1 -1
  46. package/lib/commonjs/views/SkiaJSDomView.d.ts +12 -6
  47. package/lib/commonjs/views/SkiaJSDomView.js +12 -3
  48. package/lib/commonjs/views/SkiaJSDomView.js.map +1 -1
  49. package/lib/commonjs/views/SkiaPictureView.d.ts +8 -1
  50. package/lib/commonjs/views/SkiaPictureView.js +14 -1
  51. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  52. package/lib/commonjs/views/SkiaPictureView.web.d.ts +3 -3
  53. package/lib/commonjs/views/SkiaPictureView.web.js.map +1 -1
  54. package/lib/commonjs/views/types.d.ts +2 -12
  55. package/lib/commonjs/views/types.js.map +1 -1
  56. package/lib/module/renderer/Canvas.d.ts +1 -0
  57. package/lib/module/renderer/Canvas.js +5 -5
  58. package/lib/module/renderer/Canvas.js.map +1 -1
  59. package/lib/module/specs/SkiaPictureViewNativeComponent.d.ts +0 -1
  60. package/lib/module/specs/SkiaPictureViewNativeComponent.js.map +1 -1
  61. package/lib/module/views/SkiaBaseWebView.d.ts +1 -12
  62. package/lib/module/views/SkiaBaseWebView.js +1 -19
  63. package/lib/module/views/SkiaBaseWebView.js.map +1 -1
  64. package/lib/module/views/SkiaDomView.d.ts +8 -1
  65. package/lib/module/views/SkiaDomView.js +14 -2
  66. package/lib/module/views/SkiaDomView.js.map +1 -1
  67. package/lib/module/views/SkiaDomView.web.d.ts +3 -3
  68. package/lib/module/views/SkiaDomView.web.js.map +1 -1
  69. package/lib/module/views/SkiaJSDomView.d.ts +12 -6
  70. package/lib/module/views/SkiaJSDomView.js +12 -3
  71. package/lib/module/views/SkiaJSDomView.js.map +1 -1
  72. package/lib/module/views/SkiaPictureView.d.ts +8 -1
  73. package/lib/module/views/SkiaPictureView.js +14 -1
  74. package/lib/module/views/SkiaPictureView.js.map +1 -1
  75. package/lib/module/views/SkiaPictureView.web.d.ts +3 -3
  76. package/lib/module/views/SkiaPictureView.web.js.map +1 -1
  77. package/lib/module/views/types.d.ts +2 -12
  78. package/lib/module/views/types.js.map +1 -1
  79. package/lib/typescript/lib/commonjs/views/SkiaBaseWebView.d.ts +0 -11
  80. package/lib/typescript/lib/commonjs/views/SkiaDomView.d.ts +3 -0
  81. package/lib/typescript/lib/commonjs/views/SkiaJSDomView.d.ts +2 -0
  82. package/lib/typescript/lib/commonjs/views/SkiaPictureView.d.ts +3 -0
  83. package/lib/typescript/lib/module/views/SkiaBaseWebView.d.ts +0 -11
  84. package/lib/typescript/lib/module/views/SkiaDomView.d.ts +3 -0
  85. package/lib/typescript/lib/module/views/SkiaJSDomView.d.ts +2 -0
  86. package/lib/typescript/lib/module/views/SkiaPictureView.d.ts +3 -0
  87. package/lib/typescript/src/renderer/Canvas.d.ts +1 -0
  88. package/lib/typescript/src/specs/SkiaPictureViewNativeComponent.d.ts +0 -1
  89. package/lib/typescript/src/views/SkiaBaseWebView.d.ts +1 -12
  90. package/lib/typescript/src/views/SkiaDomView.d.ts +8 -1
  91. package/lib/typescript/src/views/SkiaDomView.web.d.ts +3 -3
  92. package/lib/typescript/src/views/SkiaJSDomView.d.ts +12 -6
  93. package/lib/typescript/src/views/SkiaPictureView.d.ts +8 -1
  94. package/lib/typescript/src/views/SkiaPictureView.web.d.ts +3 -3
  95. package/lib/typescript/src/views/types.d.ts +2 -12
  96. package/package.json +1 -1
  97. package/react-native-skia.podspec +2 -2
  98. package/src/renderer/Canvas.tsx +4 -3
  99. package/src/renderer/__tests__/e2e/Image.spec.tsx +2 -64
  100. package/src/skia/__tests__/assets/oslo-mini.jpg +0 -0
  101. package/src/specs/SkiaPictureViewNativeComponent.ts +0 -1
  102. package/src/views/SkiaBaseWebView.tsx +3 -19
  103. package/src/views/SkiaDomView.tsx +23 -4
  104. package/src/views/SkiaDomView.web.tsx +3 -3
  105. package/src/views/SkiaJSDomView.tsx +30 -9
  106. package/src/views/SkiaPictureView.tsx +22 -2
  107. package/src/views/SkiaPictureView.web.tsx +3 -3
  108. package/src/views/types.ts +2 -13
  109. package/cpp/rnskia/RNSkDispatchQueue.cpp +0 -73
  110. package/cpp/rnskia/RNSkDispatchQueue.h +0 -49
  111. package/ios/RNSkia-iOS/DisplayLink.h +0 -17
  112. package/ios/RNSkia-iOS/DisplayLink.mm +0 -41
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.4.1)
4
4
  set (CMAKE_VERBOSE_MAKEFILE ON)
5
5
  set (CMAKE_CXX_STANDARD 17)
6
6
  set(SK_GRAPHITE OFF)
7
- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSK_BUILD_FOR_ANDROID -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")
7
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSK_BUILD_FOR_ANDROID -DSK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID")
8
8
  set (PACKAGE_NAME "rnskia")
9
9
  set (SKIA_LIB "skia")
10
10
  set (SKIA_SVG_LIB "svg")
@@ -87,7 +87,6 @@ add_library(
87
87
 
88
88
  "${PROJECT_SOURCE_DIR}/../cpp/rnskia/RNSkManager.cpp"
89
89
  "${PROJECT_SOURCE_DIR}/../cpp/rnskia/RNSkDomView.cpp"
90
- "${PROJECT_SOURCE_DIR}/../cpp/rnskia/RNSkDispatchQueue.cpp"
91
90
 
92
91
  "${PROJECT_SOURCE_DIR}/../cpp/rnskia/dom/base/DrawingContext.cpp"
93
92
  "${PROJECT_SOURCE_DIR}/../cpp/rnskia/dom/base/ConcatablePaint.cpp"
@@ -63,10 +63,6 @@ using TSelf = jni::local_ref<JniPlatformContext::jhybriddata>;
63
63
  void JniPlatformContext::registerNatives() {
64
64
  registerHybrid({
65
65
  makeNativeMethod("initHybrid", JniPlatformContext::initHybrid),
66
- makeNativeMethod("notifyDrawLoop",
67
- JniPlatformContext::notifyDrawLoopExternal),
68
- makeNativeMethod("notifyTaskReady",
69
- JniPlatformContext::notifyTaskReadyExternal),
70
66
  });
71
67
  }
72
68
 
@@ -134,51 +130,6 @@ sk_sp<SkImage> JniPlatformContext::takeScreenshotFromViewTag(size_t tag) {
134
130
  return skImage;
135
131
  }
136
132
 
137
- void JniPlatformContext::startDrawLoop() {
138
- jni::ThreadScope ts;
139
- // Start drawing loop
140
- static auto method =
141
- javaPart_->getClass()->getMethod<void(void)>("beginDrawLoop");
142
- method(javaPart_.get());
143
- }
144
-
145
- void JniPlatformContext::stopDrawLoop() {
146
- jni::ThreadScope ts;
147
- // Stop drawing loop
148
- static auto method =
149
- javaPart_->getClass()->getMethod<void(void)>("endDrawLoop");
150
- method(javaPart_.get());
151
- }
152
-
153
- void JniPlatformContext::notifyDrawLoopExternal() {
154
- jni::ThreadScope ts;
155
- _onNotifyDrawLoop();
156
- }
157
-
158
- void JniPlatformContext::runTaskOnMainThread(std::function<void()> task) {
159
- _taskMutex->lock();
160
- _taskCallbacks.push(task);
161
- _taskMutex->unlock();
162
-
163
- // Notify Java that task is ready
164
- static auto method = javaPart_->getClass()->getMethod<void(void)>(
165
- "notifyTaskReadyOnMainThread");
166
- method(javaPart_.get());
167
- }
168
-
169
- void JniPlatformContext::notifyTaskReadyExternal() {
170
- jni::ThreadScope ts;
171
- _taskMutex->lock();
172
- auto task = _taskCallbacks.front();
173
- if (task != nullptr) {
174
- _taskCallbacks.pop();
175
- _taskMutex->unlock();
176
- task();
177
- } else {
178
- _taskMutex->unlock();
179
- }
180
- }
181
-
182
133
  void JniPlatformContext::performStreamOperation(
183
134
  const std::string &sourceUri,
184
135
  const std::function<void(std::unique_ptr<SkStreamAsset>)> &op) {
@@ -33,11 +33,6 @@ public:
33
33
 
34
34
  void raiseError(const std::exception &err);
35
35
 
36
- void startDrawLoop();
37
- void stopDrawLoop();
38
-
39
- void notifyDrawLoopExternal();
40
-
41
36
  void notifyTaskReadyExternal();
42
37
 
43
38
  void runTaskOnMainThread(std::function<void()> task);
@@ -46,10 +41,6 @@ public:
46
41
 
47
42
  sk_sp<SkImage> takeScreenshotFromViewTag(size_t tag);
48
43
 
49
- void setOnNotifyDrawLoop(const std::function<void(void)> &callback) {
50
- _onNotifyDrawLoop = callback;
51
- }
52
-
53
44
  jni::global_ref<jobject> createVideo(const std::string &url);
54
45
 
55
46
  private:
@@ -58,16 +49,9 @@ private:
58
49
 
59
50
  float _pixelDensity;
60
51
 
61
- std::function<void(void)> _onNotifyDrawLoop;
62
-
63
- std::queue<std::function<void()>> _taskCallbacks;
64
-
65
- std::shared_ptr<std::mutex> _taskMutex;
66
-
67
52
  explicit JniPlatformContext(
68
53
  jni::alias_ref<JniPlatformContext::jhybridobject> jThis,
69
54
  const float pixelDensity)
70
- : _taskMutex(std::make_shared<std::mutex>()),
71
- javaPart_(jni::make_global(jThis)), _pixelDensity(pixelDensity) {}
55
+ : javaPart_(jni::make_global(jThis)), _pixelDensity(pixelDensity) {}
72
56
  };
73
57
  } // namespace RNSkia
@@ -39,8 +39,6 @@ protected:
39
39
 
40
40
  virtual void surfaceDestroyed() { _skiaAndroidView->surfaceDestroyed(); }
41
41
 
42
- virtual void setMode(std::string mode) { _skiaAndroidView->setMode(mode); }
43
-
44
42
  virtual void setDebugMode(bool show) {
45
43
  _skiaAndroidView->setShowDebugInfo(show);
46
44
  }
@@ -40,7 +40,6 @@ public:
40
40
  makeNativeMethod("surfaceDestroyed", JniSkiaDomView::surfaceDestroyed),
41
41
  makeNativeMethod("surfaceSizeChanged",
42
42
  JniSkiaDomView::surfaceSizeChanged),
43
- makeNativeMethod("setMode", JniSkiaDomView::setMode),
44
43
  makeNativeMethod("setDebugMode", JniSkiaDomView::setDebugMode),
45
44
  makeNativeMethod("registerView", JniSkiaDomView::registerView),
46
45
  makeNativeMethod("unregisterView", JniSkiaDomView::unregisterView)});
@@ -57,8 +56,6 @@ protected:
57
56
 
58
57
  void surfaceDestroyed() override { JniSkiaBaseView::surfaceDestroyed(); }
59
58
 
60
- void setMode(std::string mode) override { JniSkiaBaseView::setMode(mode); }
61
-
62
59
  void setDebugMode(bool show) override { JniSkiaBaseView::setDebugMode(show); }
63
60
 
64
61
  void registerView(int nativeId) override {
@@ -53,8 +53,6 @@ public:
53
53
  std::shared_ptr<RNSkManager> getSkiaManager() { return _skManager; }
54
54
 
55
55
  void invalidate() {
56
- _context->stopDrawLoop();
57
- _context->notifyDrawLoop(true);
58
56
  _skManager = nullptr;
59
57
  _context = nullptr;
60
58
  }
@@ -41,7 +41,6 @@ public:
41
41
  JniSkiaPictureView::surfaceDestroyed),
42
42
  makeNativeMethod("surfaceSizeChanged",
43
43
  JniSkiaPictureView::surfaceSizeChanged),
44
- makeNativeMethod("setMode", JniSkiaPictureView::setMode),
45
44
  makeNativeMethod("setDebugMode", JniSkiaPictureView::setDebugMode),
46
45
  makeNativeMethod("registerView", JniSkiaPictureView::registerView),
47
46
  makeNativeMethod("unregisterView",
@@ -59,8 +58,6 @@ protected:
59
58
 
60
59
  void surfaceDestroyed() override { JniSkiaBaseView::surfaceDestroyed(); }
61
60
 
62
- void setMode(std::string mode) override { JniSkiaBaseView::setMode(mode); }
63
-
64
61
  void setDebugMode(bool show) override { JniSkiaBaseView::setDebugMode(show); }
65
62
 
66
63
  void registerView(int nativeId) override {
@@ -0,0 +1,69 @@
1
+ #pragma once
2
+
3
+ #include <android/looper.h>
4
+ #include <unistd.h>
5
+
6
+ class MainThreadDispatcher {
7
+ private:
8
+ ALooper *mainLooper;
9
+ int messagePipe[2];
10
+ std::queue<std::function<void()>> taskQueue;
11
+ std::mutex queueMutex;
12
+
13
+ static constexpr int LOOPER_ID_MAIN = 1;
14
+
15
+ void processMessages() {
16
+ std::lock_guard<std::mutex> lock(queueMutex);
17
+ while (!taskQueue.empty()) {
18
+ auto task = taskQueue.front();
19
+ taskQueue.pop();
20
+ task();
21
+ }
22
+ }
23
+
24
+ public:
25
+ static MainThreadDispatcher &getInstance() {
26
+ static MainThreadDispatcher instance;
27
+ return instance;
28
+ }
29
+
30
+ void post(std::function<void()> task) {
31
+ // TODO: this is disabled for now but we can clean this up
32
+ // if (ALooper_forThread() == mainLooper) {
33
+ // task();
34
+ // } else {
35
+ {
36
+ std::lock_guard<std::mutex> lock(queueMutex);
37
+ taskQueue.push(std::move(task));
38
+ }
39
+ char wake = 1;
40
+ write(messagePipe[1], &wake, 1);
41
+ // }
42
+ }
43
+
44
+ ~MainThreadDispatcher() {
45
+ close(messagePipe[0]);
46
+ close(messagePipe[1]);
47
+ }
48
+
49
+ private:
50
+ MainThreadDispatcher() {
51
+ mainLooper = ALooper_forThread();
52
+ if (!mainLooper) {
53
+ mainLooper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
54
+ }
55
+
56
+ pipe(messagePipe);
57
+
58
+ ALooper_addFd(
59
+ mainLooper, messagePipe[0], LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT,
60
+ [](int fd, int events, void *data) -> int {
61
+ char buf[1];
62
+ read(fd, buf, 1);
63
+ auto dispatcher = static_cast<MainThreadDispatcher *>(data);
64
+ dispatcher->processMessages();
65
+ return 1;
66
+ },
67
+ this);
68
+ }
69
+ };
@@ -129,9 +129,11 @@ public:
129
129
  std::unique_ptr<WindowContext> MakeWindow(ANativeWindow *window, int width,
130
130
  int height) {
131
131
  return std::make_unique<OpenGLWindowContext>(
132
- _directContext, _glDisplay.get(), _glContext.get(), window);
132
+ _directContext.get(), _glDisplay.get(), _glContext.get(), window);
133
133
  }
134
134
 
135
+ GrDirectContext *getDirectContext() { return _directContext.get(); }
136
+
135
137
  private:
136
138
  EGLConfig _glConfig;
137
139
  std::unique_ptr<gl::Display> _glDisplay;
@@ -45,7 +45,7 @@ sk_sp<SkSurface> OpenGLWindowContext::getSurface() {
45
45
  sk_sp<SkColorSpace> colorSpace(nullptr);
46
46
  SkSurfaceProps surfaceProps(0, kRGB_H_SkPixelGeometry);
47
47
  _skSurface = SkSurfaces::WrapBackendRenderTarget(
48
- _directContext.get(), backendRT, kBottomLeft_GrSurfaceOrigin,
48
+ _directContext, backendRT, kBottomLeft_GrSurfaceOrigin,
49
49
  kRGBA_8888_SkColorType, colorSpace, &surfaceProps);
50
50
  }
51
51
  return _skSurface;
@@ -33,9 +33,8 @@ namespace RNSkia {
33
33
 
34
34
  class OpenGLWindowContext : public WindowContext {
35
35
  public:
36
- OpenGLWindowContext(sk_sp<GrDirectContext> directContext,
37
- gl::Display *display, gl::Context *glContext,
38
- ANativeWindow *window)
36
+ OpenGLWindowContext(GrDirectContext *directContext, gl::Display *display,
37
+ gl::Context *glContext, ANativeWindow *window)
39
38
  : _directContext(directContext), _display(display), _glContext(glContext),
40
39
  _window(window) {
41
40
  ANativeWindow_acquire(_window);
@@ -60,11 +59,11 @@ public:
60
59
  void resize(int width, int height) override { _skSurface = nullptr; }
61
60
 
62
61
  private:
63
- sk_sp<GrDirectContext> _directContext;
62
+ GrDirectContext *_directContext;
64
63
  gl::Display *_display;
64
+ gl::Context *_glContext = nullptr;
65
65
  ANativeWindow *_window;
66
66
  sk_sp<SkSurface> _skSurface = nullptr;
67
- gl::Context *_glContext = nullptr;
68
67
  std::unique_ptr<gl::Surface> _glSurface = nullptr;
69
68
  };
70
69
 
@@ -17,6 +17,7 @@
17
17
 
18
18
  #include "AHardwareBufferUtils.h"
19
19
  #include "JniPlatformContext.h"
20
+ #include "MainThreadDispatcher.h"
20
21
  #include "RNSkAndroidVideo.h"
21
22
  #include "RNSkPlatformContext.h"
22
23
 
@@ -37,13 +38,9 @@ public:
37
38
  std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker)
38
39
  : RNSkPlatformContext(runtime, jsCallInvoker,
39
40
  jniPlatformContext->getPixelDensity()),
40
- _jniPlatformContext(jniPlatformContext) {
41
- // Hook onto the notify draw loop callback in the platform context
42
- jniPlatformContext->setOnNotifyDrawLoop(
43
- [this]() { notifyDrawLoop(false); });
44
- }
41
+ _jniPlatformContext(jniPlatformContext) {}
45
42
 
46
- ~RNSkAndroidPlatformContext() { stopDrawLoop(); }
43
+ ~RNSkAndroidPlatformContext() {}
47
44
 
48
45
  void performStreamOperation(
49
46
  const std::string &sourceUri,
@@ -152,22 +149,24 @@ public:
152
149
  #endif
153
150
  }
154
151
 
152
+ #if !defined(SK_GRAPHITE)
153
+ GrDirectContext *getDirectContext() override {
154
+ return OpenGLContext::getInstance().getDirectContext();
155
+ }
156
+ #endif
157
+
155
158
  sk_sp<SkFontMgr> createFontMgr() override {
156
159
  return SkFontMgr_New_Android(nullptr);
157
160
  }
158
161
 
159
162
  void runOnMainThread(std::function<void()> task) override {
160
- _jniPlatformContext->runTaskOnMainThread(task);
163
+ MainThreadDispatcher::getInstance().post(std::move(task));
161
164
  }
162
165
 
163
166
  sk_sp<SkImage> takeScreenshotFromViewTag(size_t tag) override {
164
167
  return _jniPlatformContext->takeScreenshotFromViewTag(tag);
165
168
  }
166
169
 
167
- void startDrawLoop() override { _jniPlatformContext->startDrawLoop(); }
168
-
169
- void stopDrawLoop() override { _jniPlatformContext->stopDrawLoop(); }
170
-
171
170
  private:
172
171
  JniPlatformContext *_jniPlatformContext;
173
172
  };
@@ -19,8 +19,6 @@ public:
19
19
 
20
20
  virtual float getPixelDensity() = 0;
21
21
 
22
- virtual void setMode(std::string mode) = 0;
23
-
24
22
  virtual void setShowDebugInfo(bool show) = 0;
25
23
 
26
24
  virtual void viewDidUnmount() = 0;
@@ -42,7 +40,7 @@ public:
42
40
 
43
41
  // Try to render directly when the surface has been set so that
44
42
  // we don't have to wait until the draw loop returns.
45
- RNSkView::renderImmediate();
43
+ RNSkView::redraw();
46
44
  }
47
45
 
48
46
  void surfaceDestroyed() override {
@@ -55,24 +53,16 @@ public:
55
53
  ->surfaceSizeChanged(surface, width, height);
56
54
  // This is only need for the first time to frame, this renderImmediate call
57
55
  // will invoke updateTexImage for the previous frame
58
- RNSkView::renderImmediate();
56
+ RNSkView::redraw();
59
57
  }
60
58
 
61
59
  float getPixelDensity() override {
62
60
  return T::getPlatformContext()->getPixelDensity();
63
61
  }
64
62
 
65
- void setMode(std::string mode) override {
66
- if (mode.compare("continuous") == 0) {
67
- T::setDrawingMode(RNSkDrawingMode::Continuous);
68
- } else {
69
- T::setDrawingMode(RNSkDrawingMode::Default);
70
- }
71
- }
72
-
73
63
  void setShowDebugInfo(bool show) override { T::setShowDebugOverlays(show); }
74
64
 
75
- void viewDidUnmount() override { T::endDrawingLoop(); }
65
+ void viewDidUnmount() override {}
76
66
 
77
67
  std::shared_ptr<RNSkView> getSkiaView() override {
78
68
  return T::shared_from_this();
@@ -64,7 +64,6 @@ bool RNSkOpenGLCanvasProvider::renderToCanvas(
64
64
 
65
65
  // Swap buffers and show on screen
66
66
  _surfaceHolder->present();
67
-
68
67
  return true;
69
68
  } else {
70
69
  // the render context did not provide a surface
@@ -25,14 +25,8 @@ public class PlatformContext {
25
25
 
26
26
  private final ReactContext mContext;
27
27
 
28
- private boolean _drawLoopActive = false;
29
- private boolean _isPaused = false;
30
-
31
28
  private final String TAG = "PlatformContext";
32
29
 
33
- private final Handler mainHandler = new Handler(Looper.getMainLooper());
34
-
35
-
36
30
  public PlatformContext(ReactContext reactContext) {
37
31
  mContext = reactContext;
38
32
  mHybridData = initHybrid(reactContext.getResources().getDisplayMetrics().density);
@@ -53,69 +47,11 @@ public class PlatformContext {
53
47
  return buffer.toByteArray();
54
48
  }
55
49
 
56
- private void postFrameLoop() {
57
- Choreographer.FrameCallback frameCallback = new Choreographer.FrameCallback() {
58
- @Override
59
- public void doFrame(long frameTimeNanos) {
60
- if (_drawLoopActive) {
61
- Choreographer.getInstance().postFrameCallback(this);
62
- }
63
- if (_isPaused) {
64
- return;
65
- }
66
- notifyDrawLoop();
67
- }
68
- };
69
- Choreographer.getInstance().postFrameCallback(frameCallback);
70
- }
71
-
72
-
73
- @DoNotStrip
74
- public void notifyTaskReadyOnMainThread() {
75
- mainHandler.post(new Runnable() {
76
- @Override
77
- public void run() {
78
- notifyTaskReady();
79
- }
80
- });
81
- }
82
-
83
50
  @DoNotStrip
84
51
  Object takeScreenshotFromViewTag(int tag) {
85
52
  return ViewScreenshotService.makeViewScreenshotFromTag(mContext, tag);
86
53
  }
87
54
 
88
- @DoNotStrip
89
- public void raise(final String message) {
90
- mainHandler.post(new Runnable() {
91
- @Override
92
- public void run() {
93
- mContext.handleException(new Exception(message));
94
- }
95
- });
96
- }
97
-
98
- @DoNotStrip
99
- public void beginDrawLoop() {
100
- if (_drawLoopActive) {
101
- return;
102
- }
103
- _drawLoopActive = true;
104
- mainHandler.post(new Runnable() {
105
- @Override
106
- public void run() {
107
- postFrameLoop();
108
- }
109
- });
110
- }
111
-
112
- @DoNotStrip
113
- public void endDrawLoop() {
114
- if (_drawLoopActive) {
115
- _drawLoopActive = false;
116
- }
117
- }
118
-
119
55
  @DoNotStrip
120
56
  public byte[] getJniStreamFromSource(String sourceUri) throws IOException {
121
57
  // First try loading the input as a resource directly
@@ -163,25 +99,6 @@ public class PlatformContext {
163
99
  return null;
164
100
  }
165
101
 
166
- void onPause() {
167
- Log.i(TAG, "Paused");
168
- _isPaused = true;
169
- }
170
-
171
- void onResume() {
172
- _isPaused = false;
173
- Log.i(TAG, "Resume");
174
- if(_drawLoopActive) {
175
- // Restart draw loop
176
- mainHandler.post(new Runnable() {
177
- @Override
178
- public void run() {
179
- postFrameLoop();
180
- }
181
- });
182
- }
183
- }
184
-
185
102
  @Override
186
103
  protected void finalize() throws Throwable {
187
104
  mHybridData.resetNative();
@@ -190,6 +107,4 @@ public class PlatformContext {
190
107
 
191
108
  // Private c++ native methods
192
109
  private native HybridData initHybrid(float pixelDensity);
193
- private native void notifyDrawLoop();
194
- private native void notifyTaskReady();
195
110
  }
@@ -12,7 +12,7 @@ import com.facebook.react.module.annotations.ReactModule;
12
12
  import java.lang.ref.WeakReference;
13
13
 
14
14
  @ReactModule(name="RNSkiaModule")
15
- public class RNSkiaModule extends NativeSkiaModuleSpec implements LifecycleEventListener {
15
+ public class RNSkiaModule extends NativeSkiaModuleSpec {
16
16
  public static final String NAME = "RNSkiaModule";
17
17
 
18
18
  private final WeakReference<ReactApplicationContext> weakReactContext;
@@ -21,17 +21,12 @@ public class RNSkiaModule extends NativeSkiaModuleSpec implements LifecycleEvent
21
21
  public RNSkiaModule(ReactApplicationContext reactContext) {
22
22
  super(reactContext);
23
23
  this.weakReactContext = new WeakReference<>(reactContext);
24
- reactContext.addLifecycleEventListener(this);
25
24
  }
26
25
 
27
26
  @Override
28
27
  public void invalidate() {
29
28
  super.invalidate();
30
29
 
31
- if (getReactApplicationContext() != null) {
32
- getReactApplicationContext().removeLifecycleEventListener(this);
33
- }
34
-
35
30
  if (this.skiaManager != null) {
36
31
  this.skiaManager.invalidate();
37
32
  this.skiaManager.destroy();
@@ -69,19 +64,4 @@ public class RNSkiaModule extends NativeSkiaModuleSpec implements LifecycleEvent
69
64
  return false;
70
65
  }
71
66
  }
72
-
73
- @Override
74
- public void onHostResume() {
75
- if(skiaManager != null) skiaManager.onHostResume();
76
- }
77
-
78
- @Override
79
- public void onHostPause() {
80
- if(skiaManager != null) skiaManager.onHostPause();
81
- }
82
-
83
- @Override
84
- public void onHostDestroy() {
85
-
86
- }
87
67
  }
@@ -43,10 +43,6 @@ public class SkiaManager {
43
43
  return mPlatformContext;
44
44
  }
45
45
 
46
- public void onHostResume() { mPlatformContext.onResume(); }
47
-
48
- public void onHostPause() { mPlatformContext.onPause(); }
49
-
50
46
  // private C++ functions
51
47
  private native HybridData initHybrid(long jsContext, RuntimeExecutor runtimeExecutor,
52
48
  PlatformContext platformContext);
@@ -21,11 +21,8 @@ public class SkiaDomViewManagerDelegate<T extends View, U extends BaseViewManage
21
21
  @Override
22
22
  public void setProperty(T view, String propName, @Nullable Object value) {
23
23
  switch (propName) {
24
- case "mode":
25
- mViewManager.setMode(view, value == null ? null : (String) value);
26
- break;
27
24
  case "debug":
28
- mViewManager.setDebug(view, value == null ? false : (boolean) value);
25
+ mViewManager.setDebug(view, value != null && (boolean) value);
29
26
  break;
30
27
  default:
31
28
  super.setProperty(view, propName, value);
@@ -13,6 +13,5 @@ import android.view.View;
13
13
  import androidx.annotation.Nullable;
14
14
 
15
15
  public interface SkiaDomViewManagerInterface<T extends View> {
16
- void setMode(T view, @Nullable String value);
17
16
  void setDebug(T view, boolean value);
18
17
  }
@@ -92,7 +92,11 @@ public:
92
92
  image = DawnContext::getInstance().MakeRasterImage(image);
93
93
  #else
94
94
  if (image->isTextureBacked()) {
95
- image = image->makeNonTextureImage();
95
+ auto grContext = getContext()->getDirectContext();
96
+ image = image->makeRasterImage(grContext);
97
+ if (!image) {
98
+ return nullptr;
99
+ }
96
100
  }
97
101
  #endif
98
102
  sk_sp<SkData> data;
@@ -121,6 +125,9 @@ public:
121
125
 
122
126
  JSI_HOST_FUNCTION(encodeToBytes) {
123
127
  auto data = encodeImageData(arguments, count);
128
+ if (!data) {
129
+ return jsi::Value::null();
130
+ }
124
131
 
125
132
  auto arrayCtor =
126
133
  runtime.global().getPropertyAsFunction(runtime, "Uint8Array");
@@ -141,6 +148,9 @@ public:
141
148
 
142
149
  JSI_HOST_FUNCTION(encodeToBase64) {
143
150
  auto data = encodeImageData(arguments, count);
151
+ if (!data) {
152
+ return jsi::Value::null();
153
+ }
144
154
 
145
155
  auto len = Base64::Encode(data->bytes(), data->size(), nullptr);
146
156
  auto buffer = std::string(len, 0);
@@ -182,10 +192,15 @@ public:
182
192
  .asObject(runtime)
183
193
  .getArrayBuffer(runtime);
184
194
  auto bfrPtr = reinterpret_cast<void *>(buffer.data(runtime));
185
-
186
- if (!getObject()->readPixels(info, bfrPtr, bytesPerRow, srcX, srcY)) {
195
+ #if defined(SK_GRAPHITE)
196
+ throw std::runtime_error("Not implemented yet");
197
+ #else
198
+ auto grContext = getContext()->getDirectContext();
199
+ if (!getObject()->readPixels(grContext, info, bfrPtr, bytesPerRow, srcX,
200
+ srcY)) {
187
201
  return jsi::Value::null();
188
202
  }
203
+ #endif
189
204
  return dest;
190
205
  }
191
206
 
@@ -193,7 +208,8 @@ public:
193
208
  #if defined(SK_GRAPHITE)
194
209
  auto rasterImage = DawnContext::getInstance().MakeRasterImage(getObject());
195
210
  #else
196
- auto rasterImage = getObject()->makeNonTextureImage();
211
+ auto grContext = getContext()->getDirectContext();
212
+ auto rasterImage = getObject()->makeRasterImage(grContext);
197
213
  #endif
198
214
  return jsi::Object::createFromHostObject(
199
215
  runtime, std::make_shared<JsiSkImage>(getContext(), rasterImage));