@shopify/react-native-skia 0.1.121 → 0.1.124

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. package/android/CMakeLists.txt +3 -1
  2. package/android/cpp/jni/JniSkiaDrawView.cpp +14 -71
  3. package/android/cpp/jni/JniSkiaManager.cpp +1 -1
  4. package/android/cpp/jni/include/JniSkiaDrawView.h +18 -22
  5. package/android/cpp/jni/include/JniSkiaManager.h +4 -4
  6. package/android/cpp/rnskia-android/RNSkDrawViewImpl.cpp +68 -0
  7. package/android/cpp/rnskia-android/RNSkDrawViewImpl.h +48 -0
  8. package/android/cpp/{jni/include/JniPlatformContextWrapper.h → rnskia-android/RNSkPlatformContextImpl.h} +4 -4
  9. package/android/cpp/{jni → rnskia-android}/SkiaOpenGLRenderer.cpp +39 -54
  10. package/android/cpp/{jni/include → rnskia-android}/SkiaOpenGLRenderer.h +2 -31
  11. package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaViewManager.java +1 -1
  12. package/android/src/main/java/com/shopify/reactnative/skia/SkiaDrawView.java +21 -28
  13. package/cpp/api/JsiSkApi.h +2 -0
  14. package/cpp/api/JsiSkColor.h +49 -0
  15. package/cpp/api/JsiSkPath.h +31 -2
  16. package/cpp/api/JsiSkPathFactory.h +96 -1
  17. package/cpp/api/third_party/CSSColorParser.h +324 -0
  18. package/cpp/rnskia/RNSkAnimation.h +3 -8
  19. package/cpp/rnskia/RNSkDrawView.cpp +84 -126
  20. package/cpp/rnskia/RNSkDrawView.h +7 -37
  21. package/cpp/rnskia/RNSkJsiViewApi.h +8 -5
  22. package/cpp/rnskia/RNSkManager.cpp +2 -2
  23. package/cpp/rnskia/RNSkManager.h +2 -2
  24. package/cpp/rnskia/RNSkPlatformContext.h +1 -1
  25. package/cpp/rnskia/RNSkValueApi.h +6 -2
  26. package/cpp/rnskia/values/RNSkClockValue.h +21 -13
  27. package/cpp/rnskia/values/RNSkDerivedValue.h +13 -6
  28. package/cpp/rnskia/values/RNSkReadonlyValue.h +17 -16
  29. package/cpp/rnskia/values/RNSkValue.h +8 -3
  30. package/cpp/utils/RNSkTimingInfo.h +13 -1
  31. package/ios/RNSkia-iOS/RNSkDrawViewImpl.h +8 -10
  32. package/ios/RNSkia-iOS/RNSkDrawViewImpl.mm +25 -10
  33. package/ios/RNSkia-iOS/SkiaDrawView.mm +21 -16
  34. package/lib/commonjs/renderer/Canvas.js +17 -8
  35. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  36. package/lib/commonjs/renderer/components/Paint.js +1 -1
  37. package/lib/commonjs/renderer/components/Paint.js.map +1 -1
  38. package/lib/commonjs/renderer/components/colorFilters/Lerp.js +1 -1
  39. package/lib/commonjs/renderer/components/colorFilters/Lerp.js.map +1 -1
  40. package/lib/commonjs/renderer/components/shaders/Shader.js +2 -2
  41. package/lib/commonjs/renderer/components/shaders/Shader.js.map +1 -1
  42. package/lib/commonjs/renderer/components/shapes/Path.js +10 -2
  43. package/lib/commonjs/renderer/components/shapes/Path.js.map +1 -1
  44. package/lib/commonjs/renderer/nodes/Node.js +3 -3
  45. package/lib/commonjs/renderer/nodes/Node.js.map +1 -1
  46. package/lib/commonjs/renderer/processors/Circles.js +3 -2
  47. package/lib/commonjs/renderer/processors/Circles.js.map +1 -1
  48. package/lib/commonjs/renderer/processors/Font.js +1 -1
  49. package/lib/commonjs/renderer/processors/Font.js.map +1 -1
  50. package/lib/commonjs/renderer/processors/Paint.js +6 -1
  51. package/lib/commonjs/renderer/processors/Paint.js.map +1 -1
  52. package/lib/commonjs/renderer/processors/Rects.js +6 -6
  53. package/lib/commonjs/renderer/processors/Rects.js.map +1 -1
  54. package/lib/commonjs/skia/Color.js +3 -25
  55. package/lib/commonjs/skia/Color.js.map +1 -1
  56. package/lib/commonjs/skia/Image/Image.js.map +1 -1
  57. package/lib/commonjs/skia/ImageFilter/ImageFilterFactory.js.map +1 -1
  58. package/lib/commonjs/skia/Paint/Paint.js +13 -1
  59. package/lib/commonjs/skia/Paint/Paint.js.map +1 -1
  60. package/lib/commonjs/skia/Paint/usePaint.js +2 -4
  61. package/lib/commonjs/skia/Paint/usePaint.js.map +1 -1
  62. package/lib/commonjs/skia/Path/Path.js +13 -1
  63. package/lib/commonjs/skia/Path/Path.js.map +1 -1
  64. package/lib/commonjs/skia/Shader/Shader.js.map +1 -1
  65. package/lib/commonjs/skia/Skia.js +43 -3
  66. package/lib/commonjs/skia/Skia.js.map +1 -1
  67. package/lib/commonjs/values/animation/timing/functions/getResolvedParams.js +3 -3
  68. package/lib/commonjs/values/animation/timing/functions/getResolvedParams.js.map +1 -1
  69. package/lib/module/renderer/Canvas.js +12 -6
  70. package/lib/module/renderer/Canvas.js.map +1 -1
  71. package/lib/module/renderer/components/Paint.js +2 -2
  72. package/lib/module/renderer/components/Paint.js.map +1 -1
  73. package/lib/module/renderer/components/colorFilters/Lerp.js +1 -1
  74. package/lib/module/renderer/components/colorFilters/Lerp.js.map +1 -1
  75. package/lib/module/renderer/components/shaders/Shader.js +3 -2
  76. package/lib/module/renderer/components/shaders/Shader.js.map +1 -1
  77. package/lib/module/renderer/components/shapes/Path.js +10 -3
  78. package/lib/module/renderer/components/shapes/Path.js.map +1 -1
  79. package/lib/module/renderer/nodes/Node.js +3 -3
  80. package/lib/module/renderer/nodes/Node.js.map +1 -1
  81. package/lib/module/renderer/processors/Circles.js +3 -2
  82. package/lib/module/renderer/processors/Circles.js.map +1 -1
  83. package/lib/module/renderer/processors/Font.js +1 -1
  84. package/lib/module/renderer/processors/Font.js.map +1 -1
  85. package/lib/module/renderer/processors/Paint.js +6 -1
  86. package/lib/module/renderer/processors/Paint.js.map +1 -1
  87. package/lib/module/renderer/processors/Rects.js +5 -6
  88. package/lib/module/renderer/processors/Rects.js.map +1 -1
  89. package/lib/module/skia/Color.js +2 -21
  90. package/lib/module/skia/Color.js.map +1 -1
  91. package/lib/module/skia/Image/Image.js.map +1 -1
  92. package/lib/module/skia/ImageFilter/ImageFilterFactory.js.map +1 -1
  93. package/lib/module/skia/Paint/Paint.js +6 -0
  94. package/lib/module/skia/Paint/Paint.js.map +1 -1
  95. package/lib/module/skia/Paint/usePaint.js +2 -3
  96. package/lib/module/skia/Paint/usePaint.js.map +1 -1
  97. package/lib/module/skia/Path/Path.js +11 -0
  98. package/lib/module/skia/Path/Path.js.map +1 -1
  99. package/lib/module/skia/Shader/Shader.js.map +1 -1
  100. package/lib/module/skia/Skia.js +45 -2
  101. package/lib/module/skia/Skia.js.map +1 -1
  102. package/lib/module/values/animation/timing/functions/getResolvedParams.js +3 -3
  103. package/lib/module/values/animation/timing/functions/getResolvedParams.js.map +1 -1
  104. package/lib/typescript/src/renderer/Canvas.d.ts +6 -0
  105. package/lib/typescript/src/renderer/components/shapes/Path.d.ts +3 -1
  106. package/lib/typescript/src/renderer/processors/Paint.d.ts +2 -1
  107. package/lib/typescript/src/skia/Color.d.ts +0 -1
  108. package/lib/typescript/src/skia/Image/Image.d.ts +3 -3
  109. package/lib/typescript/src/skia/ImageFilter/ImageFilterFactory.d.ts +2 -2
  110. package/lib/typescript/src/skia/Paint/Paint.d.ts +3 -2
  111. package/lib/typescript/src/skia/Path/Path.d.ts +13 -0
  112. package/lib/typescript/src/skia/Path/PathFactory.d.ts +7 -1
  113. package/lib/typescript/src/skia/Picture/Picture.d.ts +2 -2
  114. package/lib/typescript/src/skia/RuntimeEffect/RuntimeEffect.d.ts +3 -3
  115. package/lib/typescript/src/skia/Shader/Shader.d.ts +2 -2
  116. package/lib/typescript/src/skia/Shader/ShaderFactory.d.ts +9 -9
  117. package/lib/typescript/src/skia/Skia.d.ts +5 -3
  118. package/lib/typescript/src/values/animation/types.d.ts +5 -5
  119. package/package.json +1 -1
  120. package/scripts/install-npm.js +1 -1
  121. package/src/renderer/Canvas.tsx +13 -6
  122. package/src/renderer/components/Paint.tsx +2 -2
  123. package/src/renderer/components/colorFilters/Lerp.tsx +1 -1
  124. package/src/renderer/components/shaders/Shader.tsx +1 -1
  125. package/src/renderer/components/shapes/Path.tsx +12 -4
  126. package/src/renderer/nodes/Node.ts +3 -3
  127. package/src/renderer/processors/Circles.ts +2 -1
  128. package/src/renderer/processors/Font.ts +1 -1
  129. package/src/renderer/processors/Paint.ts +5 -0
  130. package/src/renderer/processors/Rects.ts +3 -2
  131. package/src/skia/Color.ts +3 -20
  132. package/src/skia/Image/Image.ts +3 -3
  133. package/src/skia/ImageFilter/ImageFilterFactory.ts +2 -2
  134. package/src/skia/Paint/Paint.ts +9 -2
  135. package/src/skia/Paint/usePaint.ts +2 -4
  136. package/src/skia/Path/Path.ts +16 -0
  137. package/src/skia/Path/PathFactory.ts +8 -1
  138. package/src/skia/Picture/Picture.ts +2 -2
  139. package/src/skia/RuntimeEffect/RuntimeEffect.ts +4 -4
  140. package/src/skia/Shader/Shader.ts +2 -2
  141. package/src/skia/Shader/ShaderFactory.ts +9 -9
  142. package/src/skia/Skia.ts +47 -3
  143. package/src/values/animation/timing/functions/getResolvedParams.ts +2 -2
  144. package/src/values/animation/types.ts +5 -5
@@ -31,7 +31,8 @@ add_library(
31
31
  "${PROJECT_SOURCE_DIR}/cpp/jni/JniSkiaManager.cpp"
32
32
  "${PROJECT_SOURCE_DIR}/cpp/jni/JniSkiaDrawView.cpp"
33
33
  "${PROJECT_SOURCE_DIR}/cpp/jni/JniPlatformContext.cpp"
34
- "${PROJECT_SOURCE_DIR}/cpp/jni/SkiaOpenGLRenderer.cpp"
34
+ "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/RNSkDrawViewImpl.cpp"
35
+ "${PROJECT_SOURCE_DIR}/cpp/rnskia-android/SkiaOpenGLRenderer.cpp"
35
36
 
36
37
  "${PROJECT_SOURCE_DIR}/cpp/jsi/JsiHostObject.cpp"
37
38
 
@@ -64,6 +65,7 @@ target_include_directories(
64
65
  cpp/api
65
66
  cpp/jsi
66
67
  cpp/jni/include
68
+ cpp/rnskia-android
67
69
  cpp/rnskia
68
70
  cpp/rnskia/values
69
71
  cpp/utils
@@ -31,16 +31,13 @@ namespace RNSkia
31
31
  /**** DTOR ***/
32
32
  JniSkiaDrawView::~JniSkiaDrawView()
33
33
  {
34
- #if LOG_ALL_DRAWING
35
- RNSkLogger::logToConsole("JniSkiaDrawView::~JniSkiaDrawView %i", getNativeId());
36
- #endif
37
34
  }
38
35
 
39
36
  /**** JNI ****/
40
37
 
41
38
  TSelf JniSkiaDrawView::initHybrid(
42
- alias_ref<HybridClass::jhybridobject> jThis,
43
- JavaSkiaManager skiaManager)
39
+ alias_ref<HybridClass::jhybridobject> jThis,
40
+ JavaSkiaManager skiaManager)
44
41
  {
45
42
  return makeCxxInstance(jThis, skiaManager);
46
43
  }
@@ -60,17 +57,17 @@ namespace RNSkia
60
57
  {
61
58
  if (mode.compare("continuous") == 0)
62
59
  {
63
- setDrawingMode(RNSkDrawingMode::Continuous);
60
+ _drawView->setDrawingMode(RNSkDrawingMode::Continuous);
64
61
  }
65
62
  else
66
63
  {
67
- setDrawingMode(RNSkDrawingMode::Default);
64
+ _drawView->setDrawingMode(RNSkDrawingMode::Default);
68
65
  }
69
66
  }
70
67
 
71
68
  void JniSkiaDrawView::setDebugMode(bool show)
72
69
  {
73
- setShowDebugOverlays(show);
70
+ _drawView->setShowDebugOverlays(show);
74
71
  }
75
72
 
76
73
  void JniSkiaDrawView::updateTouchPoints(jni::JArrayDouble touches)
@@ -78,7 +75,7 @@ namespace RNSkia
78
75
  // Create touch points
79
76
  std::vector<RNSkia::RNSkTouchPoint> points;
80
77
  auto pin = touches.pin();
81
- auto scale = getPlatformContext()->getPixelDensity();
78
+ auto scale = _drawView->getPixelDensity();
82
79
  points.reserve(pin.size() / 4);
83
80
  for (size_t i = 0; i < pin.size(); i += 4)
84
81
  {
@@ -89,81 +86,27 @@ namespace RNSkia
89
86
  point.type = (RNSkia::RNSkTouchType)pin[i + 3];
90
87
  points.push_back(point);
91
88
  }
92
- updateTouchState(std::move(points));
89
+ _drawView->updateTouchState(std::move(points));
93
90
  }
94
91
 
95
92
  void JniSkiaDrawView::surfaceAvailable(jobject surface, int width, int height)
96
93
  {
97
- #if LOG_ALL_DRAWING
98
- RNSkLogger::logToConsole("JniSkiaDrawView::surfaceAvailable %i", getNativeId());
99
- #endif
100
-
101
- _width = width;
102
- _height = height;
103
-
104
- if (_renderer == nullptr)
105
- {
106
- // Create renderer!
107
- _renderer = new SkiaOpenGLRenderer(
108
- ANativeWindow_fromSurface(Environment::current(), surface), getNativeId());
109
-
110
- // Set the draw function
111
- setNativeDrawFunc(std::bind(&JniSkiaDrawView::drawFrame, this, std::placeholders::_1));
112
-
113
- // Redraw
114
- requestRedraw();
115
- }
94
+ _drawView->surfaceAvailable(ANativeWindow_fromSurface(Environment::current(), surface), width, height);
116
95
  }
117
96
 
118
97
  void JniSkiaDrawView::surfaceSizeChanged(int width, int height)
119
98
  {
120
- #if LOG_ALL_DRAWING
121
- RNSkLogger::logToConsole("JniSkiaDrawView::surfaceSizeChanged %i", getNativeId());
122
- #endif
123
-
124
- _width = width;
125
- _height = height;
126
-
127
- // Redraw after size change
128
- requestRedraw();
99
+ _drawView->surfaceSizeChanged(width, height);
129
100
  }
130
101
 
131
102
  void JniSkiaDrawView::surfaceDestroyed()
132
103
  {
133
- #if LOG_ALL_DRAWING
134
- RNSkLogger::logToConsole("JniSkiaDrawView::surfaceDestroyed %i", getNativeId());
135
- #endif
136
- if (_renderer != nullptr)
137
- {
138
- // Turn off drawing
139
- setNativeDrawFunc(nullptr);
140
-
141
- // Start teardown
142
- _renderer->teardown();
143
-
144
- // Ask for a redraw to tear down the render pipeline. This
145
- // needs to be done on the render thread since OpenGL demands
146
- // same thread access for OpenGL contexts.
147
- getPlatformContext()->runOnRenderThread([this]()
148
- {
149
- if(_renderer != nullptr) {
150
- _renderer->run(nullptr, 0, 0);
151
- } });
152
-
153
- // Wait until the above render has finished.
154
- _renderer->waitForTeardown();
155
-
156
- // Delete renderer. All resources should be released during teardown.
157
- delete _renderer;
158
- _renderer = nullptr;
159
- }
104
+ _drawView->surfaceDestroyed();
160
105
  }
161
106
 
162
- /**** Render method ****/
163
-
164
- void JniSkiaDrawView::drawFrame(const sk_sp<SkPicture> picture)
165
- {
166
- // No need to check if the renderer is nullptr since we only get here if it is not.
167
- _renderer->run(picture, _width, _height);
107
+ void JniSkiaDrawView::releaseSurface() {
108
+ jni::ThreadScope ts;
109
+ static auto method = javaPart_->getClass()->getMethod<void(void)>("releaseSurface");
110
+ method(javaPart_.get());
168
111
  }
169
112
  } // namespace RNSkia
@@ -47,7 +47,7 @@ void JniSkiaManager::initializeRuntime() {
47
47
  }
48
48
 
49
49
  void JniSkiaManager::registerSkiaView(int viewTag, JniSkiaDrawView *skiaView) {
50
- _skManager->registerSkiaDrawView(viewTag, skiaView);
50
+ _skManager->registerSkiaDrawView(viewTag, skiaView->getDrawViewImpl());
51
51
  }
52
52
 
53
53
  void JniSkiaManager::unregisterSkiaView(int viewTag) {
@@ -5,17 +5,16 @@
5
5
  #include <thread>
6
6
  #include <string>
7
7
 
8
- #include <EGL/egl.h>
9
- #include <GLES2/gl2.h>
10
8
  #include <fbjni/fbjni.h>
11
9
  #include <jni.h>
12
10
  #include <jsi/jsi.h>
13
11
  #include <thread>
14
12
 
15
13
  #include <RNSkDrawView.h>
16
- #include "JniSkiaManager.h"
17
- #include "JniSkiaDrawView.h"
18
- #include "SkiaOpenGLRenderer.h"
14
+ #include <JniSkiaManager.h>
15
+ #include <JniSkiaDrawView.h>
16
+
17
+ #include <RNSkDrawViewImpl.h>
19
18
 
20
19
  #include <SkSurface.h>
21
20
  #include <SkRefCnt.h>
@@ -31,16 +30,15 @@ namespace RNSkia
31
30
 
32
31
  using JavaSkiaManager = jni::alias_ref<JniSkiaManager::javaobject>;
33
32
 
34
- class JniSkiaDrawView : public jni::HybridClass<JniSkiaDrawView>,
35
- public RNSkDrawView
33
+ class JniSkiaDrawView : public jni::HybridClass<JniSkiaDrawView>
36
34
  {
37
35
  public:
38
36
  static auto constexpr kJavaDescriptor = "Lcom/shopify/reactnative/skia/SkiaDrawView;";
39
37
  static auto constexpr TAG = "ReactNativeSkia";
40
38
 
41
39
  static jni::local_ref<jhybriddata> initHybrid(
42
- jni::alias_ref<jhybridobject>,
43
- JavaSkiaManager);
40
+ jni::alias_ref<jhybridobject>,
41
+ JavaSkiaManager);
44
42
 
45
43
  static void registerNatives();
46
44
 
@@ -52,30 +50,28 @@ namespace RNSkia
52
50
 
53
51
  ~JniSkiaDrawView();
54
52
 
55
- protected:
56
- int getWidth() override { return _width; }
57
- int getHeight() override { return _height; }
53
+ std::shared_ptr<RNSkDrawView> getDrawViewImpl() { return _drawView; }
54
+
55
+ void releaseSurface();
58
56
 
57
+ protected:
59
58
  void setMode(std::string mode);
60
59
  void setDebugMode(bool show);
61
60
 
62
61
  private:
63
62
  friend HybridBase;
64
63
 
65
- void drawFrame(const sk_sp<SkPicture> picture);
66
-
67
- int _width = 0;
68
- int _height = 0;
69
-
70
- SkiaOpenGLRenderer* _renderer = nullptr;
64
+ std::shared_ptr<RNSkDrawViewImpl> _drawView;
71
65
 
72
66
  jni::global_ref<JniSkiaDrawView::javaobject> javaPart_;
73
67
 
74
68
  explicit JniSkiaDrawView(
75
- jni::alias_ref<JniSkiaDrawView::jhybridobject> jThis,
76
- JavaSkiaManager skiaManager)
77
- : javaPart_(jni::make_global(jThis)),
78
- RNSkDrawView(skiaManager->cthis()->getPlatformContext()) {
69
+ jni::alias_ref<JniSkiaDrawView::jhybridobject> jThis,
70
+ JavaSkiaManager skiaManager)
71
+ : javaPart_(jni::make_global(jThis)),
72
+ _drawView(std::make_shared<RNSkDrawViewImpl>(skiaManager->cthis()->getPlatformContext(), [this]() {
73
+ releaseSurface();
74
+ })) {
79
75
  }
80
76
  };
81
77
 
@@ -7,7 +7,7 @@
7
7
  #include <memory>
8
8
 
9
9
  #include <JniPlatformContext.h>
10
- #include <JniPlatformContextWrapper.h>
10
+ #include <RNSkPlatformContextImpl.h>
11
11
 
12
12
  #include <RNSkLog.h>
13
13
 
@@ -48,14 +48,14 @@ class JniSkiaManager : public jni::HybridClass<JniSkiaManager> {
48
48
  : _javaPart(jni::make_global(jThis)),
49
49
  _jsRuntime(runtime),
50
50
  _jsCallInvoker(jsCallInvoker),
51
- _context(std::make_shared<JniPlatformContextWrapper>(platformContext, runtime, jsCallInvoker)) {
51
+ _context(std::make_shared<RNSkPlatformContextImpl>(platformContext, runtime, jsCallInvoker)) {
52
52
 
53
53
  }
54
54
 
55
55
  void registerSkiaView(int viewTag, JniSkiaDrawView *skiaView);
56
56
  void unregisterSkiaView(int viewTag);
57
57
 
58
- std::shared_ptr<JniPlatformContextWrapper> getPlatformContext() { return _context; }
58
+ std::shared_ptr<RNSkPlatformContextImpl> getPlatformContext() { return _context; }
59
59
 
60
60
  void invalidate() {
61
61
  _context->stopDrawLoop();
@@ -73,7 +73,7 @@ class JniSkiaManager : public jni::HybridClass<JniSkiaManager> {
73
73
 
74
74
  jsi::Runtime *_jsRuntime;
75
75
  std::shared_ptr<facebook::react::CallInvoker> _jsCallInvoker;
76
- std::shared_ptr<JniPlatformContextWrapper> _context;
76
+ std::shared_ptr<RNSkPlatformContextImpl> _context;
77
77
 
78
78
  void initializeRuntime();
79
79
  };
@@ -0,0 +1,68 @@
1
+ #include <RNSkDrawViewImpl.h>
2
+
3
+ #pragma clang diagnostic push
4
+ #pragma clang diagnostic ignored "-Wdocumentation"
5
+
6
+ #include <SkSurface.h>
7
+ #include <SkCanvas.h>
8
+
9
+ #pragma clang diagnostic pop
10
+
11
+ #include <RNSkLog.h>
12
+
13
+ namespace RNSkia {
14
+ RNSkDrawViewImpl::RNSkDrawViewImpl(std::shared_ptr <RNSkia::RNSkPlatformContext> context, std::function<void()> releaseSurfaceCallback) :
15
+ RNSkia::RNSkDrawView(context),
16
+ _releaseSurfaceCallback(std::move(releaseSurfaceCallback)) {}
17
+
18
+ void RNSkDrawViewImpl::surfaceAvailable(ANativeWindow* surface, int width, int height) {
19
+ _scaledWidth = width;
20
+ _scaledHeight = height;
21
+
22
+ if (_renderer == nullptr)
23
+ {
24
+ // Create renderer!
25
+ _renderer = std::make_unique<SkiaOpenGLRenderer>(surface, getNativeId());
26
+
27
+ // Redraw
28
+ requestRedraw();
29
+ }
30
+ }
31
+
32
+ void RNSkDrawViewImpl::surfaceDestroyed() {
33
+ if (_renderer != nullptr)
34
+ {
35
+ // Start teardown
36
+ _renderer->teardown();
37
+
38
+ // Teardown renderer on the render thread since OpenGL demands
39
+ // same thread access for OpenGL contexts.
40
+ getPlatformContext()->runOnRenderThread([weakSelf = weak_from_this()]() {
41
+ auto self = weakSelf.lock();
42
+ if(self) {
43
+ auto drawViewImpl = std::dynamic_pointer_cast<RNSkDrawViewImpl>(self);
44
+ if(drawViewImpl->_renderer != nullptr) {
45
+ drawViewImpl->_renderer->run(nullptr, 0, 0);
46
+ }
47
+ // Remove renderer
48
+ drawViewImpl->_renderer = nullptr;
49
+ drawViewImpl->_releaseSurfaceCallback();
50
+ }
51
+ });
52
+ }
53
+ }
54
+
55
+ void RNSkDrawViewImpl::surfaceSizeChanged(int width, int height) {
56
+ _scaledWidth = width;
57
+ _scaledHeight = height;
58
+
59
+ // Redraw after size change
60
+ requestRedraw();
61
+ }
62
+
63
+ void RNSkDrawViewImpl::drawPicture(const sk_sp <SkPicture> picture) {
64
+ if(_renderer != nullptr) {
65
+ _renderer->run(picture, _scaledWidth, _scaledHeight);
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+
3
+ #include <RNSkDrawView.h>
4
+
5
+ #include <SkiaOpenGLRenderer.h>
6
+ #include <android/native_window.h>
7
+
8
+ #pragma clang diagnostic push
9
+ #pragma clang diagnostic ignored "-Wdocumentation"
10
+
11
+ #include <SkPicture.h>
12
+ #include <SkRefCnt.h>
13
+
14
+ #pragma clang diagnostic pop
15
+
16
+ namespace RNSkia {
17
+ class RNSkDrawViewImpl : public RNSkia::RNSkDrawView {
18
+ public:
19
+ RNSkDrawViewImpl(std::shared_ptr <RNSkia::RNSkPlatformContext> context,
20
+ std::function<void()> releaseSurfaceCallback);
21
+
22
+ void surfaceAvailable(ANativeWindow* surface, int, int);
23
+ void surfaceDestroyed();
24
+ void surfaceSizeChanged(int, int);
25
+
26
+ float getPixelDensity() {
27
+ return getPlatformContext()->getPixelDensity();
28
+ }
29
+
30
+ protected:
31
+ float getScaledWidth() override { return _scaledWidth; };
32
+
33
+ float getScaledHeight() override { return _scaledHeight; };
34
+
35
+ void drawPicture(const sk_sp <SkPicture> picture) override;
36
+
37
+ private:
38
+ bool createSkiaSurface();
39
+
40
+ std::unique_ptr<SkiaOpenGLRenderer> _renderer = nullptr;
41
+
42
+ int _nativeId;
43
+ float _scaledWidth = -1;
44
+ float _scaledHeight = -1;
45
+
46
+ std::function<void()> _releaseSurfaceCallback;
47
+ };
48
+ }
@@ -11,11 +11,11 @@
11
11
  namespace RNSkia {
12
12
  using namespace facebook;
13
13
 
14
- class JniPlatformContextWrapper: public RNSkPlatformContext {
14
+ class RNSkPlatformContextImpl: public RNSkPlatformContext {
15
15
  public:
16
- JniPlatformContextWrapper(JniPlatformContext* jniPlatformContext,
17
- jsi::Runtime *runtime,
18
- std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker) :
16
+ RNSkPlatformContextImpl(JniPlatformContext* jniPlatformContext,
17
+ jsi::Runtime *runtime,
18
+ std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker) :
19
19
  RNSkPlatformContext(runtime,
20
20
  jsCallInvoker,
21
21
  jniPlatformContext->getPixelDensity()),
@@ -1,9 +1,9 @@
1
1
  #include "SkiaOpenGLRenderer.h"
2
2
 
3
3
  #include <RNSkLog.h>
4
+
4
5
  namespace RNSkia
5
6
  {
6
-
7
7
  /** Static members */
8
8
  std::shared_ptr<DrawingContext> SkiaOpenGLRenderer::getThreadDrawingContext()
9
9
  {
@@ -20,6 +20,11 @@ namespace RNSkia
20
20
  return threadContexts.at(threadId);
21
21
  }
22
22
 
23
+ SkiaOpenGLRenderer::SkiaOpenGLRenderer(ANativeWindow *surface, size_t renderId):
24
+ _surfaceTexture(surface),
25
+ _renderId(renderId) {
26
+ }
27
+
23
28
  void SkiaOpenGLRenderer::run(const sk_sp<SkPicture> picture, int width, int height)
24
29
  {
25
30
  switch (_renderState)
@@ -73,7 +78,16 @@ namespace RNSkia
73
78
  }
74
79
  case RenderState::Finishing:
75
80
  {
76
- finish();
81
+ _renderState = RenderState::Done;
82
+
83
+ if (_glSurface != EGL_NO_SURFACE && getThreadDrawingContext()->glDisplay != EGL_NO_DISPLAY)
84
+ {
85
+ eglDestroySurface(getThreadDrawingContext()->glDisplay, _glSurface);
86
+ }
87
+
88
+ _skSurface = nullptr;
89
+ _surfaceTexture = nullptr;
90
+
77
91
  break;
78
92
  }
79
93
  case RenderState::Done:
@@ -107,58 +121,11 @@ namespace RNSkia
107
121
  return true;
108
122
  }
109
123
 
110
- void SkiaOpenGLRenderer::finish()
111
- {
112
- std::lock_guard<std::mutex> lock(_lock);
113
-
114
- if (_renderState != RenderState::Finishing)
115
- {
116
- _cv.notify_all();
117
- return;
118
- }
119
-
120
- finishGL();
121
- finishSkiaSurface();
122
-
123
- _renderState = RenderState::Done;
124
-
125
- _cv.notify_one();
126
- }
127
-
128
- void SkiaOpenGLRenderer::finishGL()
129
- {
130
- if (_glSurface != EGL_NO_SURFACE && getThreadDrawingContext()->glDisplay != EGL_NO_DISPLAY)
131
- {
132
- eglDestroySurface(getThreadDrawingContext()->glDisplay, _glSurface);
133
- }
134
- }
135
-
136
- void SkiaOpenGLRenderer::finishSkiaSurface()
137
- {
138
- if (_skSurface != nullptr)
139
- {
140
- _skSurface = nullptr;
141
- }
142
-
143
- if (_nativeWindow != nullptr)
144
- {
145
- ANativeWindow_release(_nativeWindow);
146
- _nativeWindow = nullptr;
147
- }
148
- }
149
-
150
124
  void SkiaOpenGLRenderer::teardown()
151
125
  {
152
126
  _renderState = RenderState::Finishing;
153
127
  }
154
128
 
155
- void SkiaOpenGLRenderer::waitForTeardown()
156
- {
157
- std::unique_lock<std::mutex> lock(_lock);
158
- _cv.wait(lock, [this]
159
- { return (_renderState == RenderState::Done); });
160
- }
161
-
162
129
  bool SkiaOpenGLRenderer::initStaticGLContext()
163
130
  {
164
131
  if (getThreadDrawingContext()->glContext != EGL_NO_CONTEXT)
@@ -212,7 +179,12 @@ namespace RNSkia
212
179
 
213
180
  EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
214
181
 
215
- getThreadDrawingContext()->glContext = eglCreateContext(getThreadDrawingContext()->glDisplay, getThreadDrawingContext()->glConfig, NULL, contextAttribs);
182
+ getThreadDrawingContext()->glContext = eglCreateContext(
183
+ getThreadDrawingContext()->glDisplay,
184
+ getThreadDrawingContext()->glConfig,
185
+ NULL,
186
+ contextAttribs);
187
+
216
188
  if (getThreadDrawingContext()->glContext == EGL_NO_CONTEXT)
217
189
  {
218
190
  RNSkLogger::logToConsole(
@@ -244,14 +216,18 @@ namespace RNSkia
244
216
 
245
217
  bool SkiaOpenGLRenderer::initGLSurface()
246
218
  {
247
- if (_nativeWindow == nullptr)
219
+ if (_surfaceTexture == nullptr)
248
220
  {
249
221
  return false;
250
222
  }
251
223
 
252
224
  if (_glSurface != EGL_NO_SURFACE)
253
225
  {
254
- if (!eglMakeCurrent(getThreadDrawingContext()->glDisplay, _glSurface, _glSurface, getThreadDrawingContext()->glContext))
226
+ if (!eglMakeCurrent(
227
+ getThreadDrawingContext()->glDisplay,
228
+ _glSurface,
229
+ _glSurface,
230
+ getThreadDrawingContext()->glContext))
255
231
  {
256
232
  RNSkLogger::logToConsole(
257
233
  "eglMakeCurrent failed: %d\n", eglGetError());
@@ -262,7 +238,12 @@ namespace RNSkia
262
238
 
263
239
  // Create the opengl surface
264
240
  _glSurface =
265
- eglCreateWindowSurface(getThreadDrawingContext()->glDisplay, getThreadDrawingContext()->glConfig, _nativeWindow, nullptr);
241
+ eglCreateWindowSurface(
242
+ getThreadDrawingContext()->glDisplay,
243
+ getThreadDrawingContext()->glConfig,
244
+ _surfaceTexture,
245
+ nullptr);
246
+
266
247
  if (_glSurface == EGL_NO_SURFACE)
267
248
  {
268
249
  RNSkLogger::logToConsole(
@@ -270,7 +251,11 @@ namespace RNSkia
270
251
  return false;
271
252
  }
272
253
 
273
- if (!eglMakeCurrent(getThreadDrawingContext()->glDisplay, _glSurface, _glSurface, getThreadDrawingContext()->glContext))
254
+ if (!eglMakeCurrent(
255
+ getThreadDrawingContext()->glDisplay,
256
+ _glSurface,
257
+ _glSurface,
258
+ getThreadDrawingContext()->glContext))
274
259
  {
275
260
  RNSkLogger::logToConsole("eglMakeCurrent failed: %d\n", eglGetError());
276
261
  return false;
@@ -7,7 +7,6 @@
7
7
  #include "GLES2/gl2.h"
8
8
 
9
9
  #include <condition_variable>
10
- #include <mutex>
11
10
  #include <thread>
12
11
  #include <unordered_map>
13
12
 
@@ -45,9 +44,7 @@ namespace RNSkia
45
44
  class SkiaOpenGLRenderer
46
45
  {
47
46
  public:
48
- SkiaOpenGLRenderer(ANativeWindow *nativeWindow, size_t renderId) :
49
- _nativeWindow(nativeWindow),
50
- _renderId(renderId) { }
47
+ SkiaOpenGLRenderer(ANativeWindow *surface, size_t renderId);
51
48
 
52
49
  /**
53
50
  * Initializes, renders and tears down the render pipeline depending on the state of the
@@ -71,14 +68,6 @@ namespace RNSkia
71
68
  */
72
69
  void teardown();
73
70
 
74
- /**
75
- * Wait for teardown to finish. This means that we'll wait until the next
76
- * render which will handle releasing all OpenGL and Skia resources used for
77
- * this renderer. After tearing down the render will do nothing if the render
78
- * method is called again.
79
- */
80
- void waitForTeardown();
81
-
82
71
  private:
83
72
  /**
84
73
  * Initializes all required OpenGL and Skia objects
@@ -117,21 +106,6 @@ namespace RNSkia
117
106
  */
118
107
  bool ensureSkiaSurface(int width, int height);
119
108
 
120
- /**
121
- * Finalizes and releases all resources used by this renderer
122
- */
123
- void finish();
124
-
125
- /**
126
- * Destroys the underlying OpenGL surface used for this renderer
127
- */
128
- void finishGL();
129
-
130
- /**
131
- * Destroys the underlying Skia surface used for this renderer
132
- */
133
- void finishSkiaSurface();
134
-
135
109
  /**
136
110
  * To be able to use static contexts (and avoid reloading the skia context for each
137
111
  * new view, we track the OpenGL and Skia drawing context per thread.
@@ -141,7 +115,7 @@ namespace RNSkia
141
115
 
142
116
  EGLSurface _glSurface = EGL_NO_SURFACE;
143
117
 
144
- ANativeWindow *_nativeWindow = nullptr;
118
+ ANativeWindow *_surfaceTexture = nullptr;
145
119
  GrBackendRenderTarget _skRenderTarget;
146
120
  sk_sp<SkSurface> _skSurface;
147
121
 
@@ -150,9 +124,6 @@ namespace RNSkia
150
124
 
151
125
  size_t _renderId;
152
126
 
153
- std::mutex _lock;
154
- std::condition_variable _cv;
155
-
156
127
  std::atomic<RenderState> _renderState = { RenderState::Initializing };
157
128
  };
158
129
 
@@ -61,7 +61,7 @@ public class RNSkiaViewManager extends BaseViewManager<SkiaDrawView, LayoutShado
61
61
  Integer nativeId = mViewMapping.get(view);
62
62
  skiaModule.getSkiaManager().unregister(nativeId);
63
63
  mViewMapping.remove(view);
64
- view.onRemoved();
64
+ view.onViewRemoved();
65
65
  }
66
66
 
67
67
  @NonNull