@shopify/react-native-skia 0.1.151 → 0.1.152

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. package/android/CMakeLists.txt +4 -5
  2. package/android/cpp/jni/JniLoad.cpp +7 -5
  3. package/android/cpp/jni/JniSkiaManager.cpp +2 -15
  4. package/android/cpp/jni/include/JniSkiaBaseView.h +68 -0
  5. package/android/cpp/jni/include/JniSkiaDrawView.h +64 -47
  6. package/android/cpp/jni/include/JniSkiaManager.h +8 -10
  7. package/android/cpp/jni/include/JniSkiaPictureView.h +96 -0
  8. package/android/cpp/rnskia-android/{RNSkPlatformContextImpl.h → RNSkAndroidPlatformContext.h} +4 -4
  9. package/android/cpp/rnskia-android/RNSkAndroidView.h +100 -0
  10. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +91 -0
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +39 -0
  12. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.cpp +15 -6
  13. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h +5 -4
  14. package/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java +5 -11
  15. package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaPackage.java +1 -2
  16. package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +143 -0
  17. package/android/src/main/java/com/shopify/reactnative/skia/SkiaDrawView.java +17 -145
  18. package/android/src/main/java/com/shopify/reactnative/skia/{RNSkiaViewManager.java → SkiaDrawViewManager.java} +4 -15
  19. package/android/src/main/java/com/shopify/reactnative/skia/SkiaManager.java +1 -10
  20. package/android/src/main/java/com/shopify/reactnative/skia/SkiaPictureView.java +45 -0
  21. package/android/src/main/java/com/shopify/reactnative/skia/SkiaPictureViewManager.java +64 -0
  22. package/cpp/jsi/JsiValueWrapper.h +11 -0
  23. package/cpp/rnskia/RNSkInfoParameter.h +4 -14
  24. package/cpp/rnskia/RNSkJsView.cpp +211 -0
  25. package/cpp/rnskia/RNSkJsView.h +115 -0
  26. package/cpp/rnskia/RNSkJsiViewApi.h +6 -6
  27. package/cpp/rnskia/RNSkManager.cpp +7 -7
  28. package/cpp/rnskia/RNSkManager.h +7 -7
  29. package/cpp/rnskia/RNSkPictureView.h +124 -0
  30. package/cpp/rnskia/RNSkView.h +287 -0
  31. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +38 -0
  32. package/ios/RNSkia-iOS/{RNSkDrawViewImpl.mm → RNSkMetalCanvasProvider.mm} +40 -23
  33. package/ios/RNSkia-iOS/{PlatformContext.h → RNSkiOSPlatformContext.h} +4 -4
  34. package/ios/RNSkia-iOS/{PlatformContext.mm → RNSkiOSPlatformContext.mm} +5 -5
  35. package/ios/RNSkia-iOS/RNSkiOSView.h +34 -0
  36. package/ios/RNSkia-iOS/SkiaDrawViewManager.mm +19 -11
  37. package/ios/RNSkia-iOS/SkiaManager.mm +3 -3
  38. package/ios/RNSkia-iOS/SkiaPictureViewManager.h +8 -0
  39. package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +51 -0
  40. package/ios/RNSkia-iOS/SkiaUIView.h +26 -0
  41. package/ios/RNSkia-iOS/{SkiaDrawView.mm → SkiaUIView.mm} +29 -23
  42. package/lib/commonjs/views/SkiaBaseWebView.js +213 -0
  43. package/lib/commonjs/views/SkiaBaseWebView.js.map +1 -0
  44. package/lib/commonjs/views/SkiaPictureView.js +112 -0
  45. package/lib/commonjs/views/SkiaPictureView.js.map +1 -0
  46. package/lib/commonjs/views/SkiaPictureView.web.js +31 -0
  47. package/lib/commonjs/views/SkiaPictureView.web.js.map +1 -0
  48. package/lib/commonjs/views/SkiaView.js +1 -1
  49. package/lib/commonjs/views/SkiaView.js.map +1 -1
  50. package/lib/commonjs/views/SkiaView.web.js +14 -201
  51. package/lib/commonjs/views/SkiaView.web.js.map +1 -1
  52. package/lib/commonjs/views/index.js +13 -0
  53. package/lib/commonjs/views/index.js.map +1 -1
  54. package/lib/commonjs/views/types.js.map +1 -1
  55. package/lib/module/views/SkiaBaseWebView.js +198 -0
  56. package/lib/module/views/SkiaBaseWebView.js.map +1 -0
  57. package/lib/module/views/SkiaPictureView.js +94 -0
  58. package/lib/module/views/SkiaPictureView.js.map +1 -0
  59. package/lib/module/views/SkiaPictureView.web.js +19 -0
  60. package/lib/module/views/SkiaPictureView.web.js.map +1 -0
  61. package/lib/module/views/SkiaView.js +1 -1
  62. package/lib/module/views/SkiaView.js.map +1 -1
  63. package/lib/module/views/SkiaView.web.js +15 -199
  64. package/lib/module/views/SkiaView.web.js.map +1 -1
  65. package/lib/module/views/index.js +1 -0
  66. package/lib/module/views/index.js.map +1 -1
  67. package/lib/module/views/types.js.map +1 -1
  68. package/lib/typescript/src/views/SkiaBaseWebView.d.ts +58 -0
  69. package/lib/typescript/src/views/SkiaPictureView.d.ts +27 -0
  70. package/lib/typescript/src/views/SkiaPictureView.web.d.ts +7 -0
  71. package/lib/typescript/src/views/SkiaView.d.ts +4 -4
  72. package/lib/typescript/src/views/SkiaView.web.d.ts +6 -53
  73. package/lib/typescript/src/views/index.d.ts +1 -0
  74. package/lib/typescript/src/views/types.d.ts +7 -2
  75. package/package.json +1 -1
  76. package/src/views/SkiaBaseWebView.tsx +177 -0
  77. package/src/views/SkiaPictureView.tsx +93 -0
  78. package/src/views/SkiaPictureView.web.tsx +23 -0
  79. package/src/views/SkiaView.tsx +6 -7
  80. package/src/views/SkiaView.web.tsx +18 -167
  81. package/src/views/index.ts +1 -0
  82. package/src/views/types.ts +9 -2
  83. package/android/cpp/jni/JniSkiaDrawView.cpp +0 -113
  84. package/android/cpp/rnskia-android/RNSkDrawViewImpl.cpp +0 -73
  85. package/android/cpp/rnskia-android/RNSkDrawViewImpl.h +0 -48
  86. package/cpp/rnskia/RNSkDrawView.cpp +0 -315
  87. package/cpp/rnskia/RNSkDrawView.h +0 -226
  88. package/ios/RNSkia-iOS/RNSkDrawViewImpl.h +0 -52
  89. package/ios/RNSkia-iOS/SkiaDrawView.h +0 -23
@@ -0,0 +1,287 @@
1
+
2
+ #pragma once
3
+
4
+ #include <memory>
5
+
6
+ #include <RNSkPlatformContext.h>
7
+
8
+ #include <JsiValueWrapper.h>
9
+
10
+ #include <JsiSkRect.h>
11
+ #include <JsiSkImage.h>
12
+
13
+ #pragma clang diagnostic push
14
+ #pragma clang diagnostic ignored "-Wdocumentation"
15
+
16
+ #include <SkSurface.h>
17
+ #include <SkCanvas.h>
18
+
19
+ #pragma clang diagnostic pop
20
+
21
+ namespace RNSkia {
22
+
23
+ using namespace facebook;
24
+
25
+ class RNSkCanvasProvider {
26
+ public:
27
+ RNSkCanvasProvider(std::function<void()> requestRedraw): _requestRedraw(requestRedraw) {}
28
+
29
+ /**
30
+ Returns the scaled width of the view
31
+ */
32
+ virtual float getScaledWidth() = 0;
33
+
34
+ /**
35
+ Returns the scaled height of the view
36
+ */
37
+ virtual float getScaledHeight() = 0;
38
+
39
+ /**
40
+ Render to a canvas
41
+ */
42
+ virtual void renderToCanvas(const std::function<void(SkCanvas*)>&) = 0;
43
+ protected:
44
+ std::function<void()> _requestRedraw;
45
+ };
46
+
47
+ class RNSkRenderer {
48
+ public:
49
+ RNSkRenderer(std::function<void()> requestRedraw): _requestRedraw(requestRedraw) {}
50
+
51
+ /**
52
+ Tries to render the current set of drawing operations. If we're busy we'll return false so that the calling RNSkBaseDrawView
53
+ can request a new render next frame. The tryRender method is typically called on each frame if there are any redraw
54
+ requests. The method will be called from the main thread, so the implementor must make sure any thread requirements are
55
+ met before rendering. This method will also allow the rendering to be dispatched to another thread.
56
+ */
57
+ virtual bool tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
58
+
59
+ /**
60
+ Renders directly to the canvas in the canvas provider. This method is called from a Javascript call to render a
61
+ snapshot of the SkiaView to an image, and can therefore run outside the tryRender loop and directly in the
62
+ javascript thread.
63
+ */
64
+ virtual void renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
65
+
66
+ void setShowDebugOverlays(bool showDebugOverlays) { _showDebugOverlays = showDebugOverlays; }
67
+ bool getShowDebugOverlays() { return _showDebugOverlays; }
68
+
69
+ protected:
70
+ std::function<void()> _requestRedraw;
71
+ bool _showDebugOverlays;
72
+ };
73
+
74
+ class RNSkImageCanvasProvider: public RNSkCanvasProvider {
75
+ public:
76
+ RNSkImageCanvasProvider(std::function<void()> requestRedraw,
77
+ float width,
78
+ float height):
79
+ RNSkCanvasProvider(requestRedraw),
80
+ _width(width),
81
+ _height(height) {
82
+ _surface = SkSurface::MakeRasterN32Premul(_width, _height);
83
+ }
84
+
85
+ /**
86
+ Returns a snapshot of the current surface/canvas
87
+ */
88
+ sk_sp<SkImage> makeSnapshot(std::shared_ptr<SkRect> bounds) {
89
+ if(bounds != nullptr) {
90
+ SkIRect b = SkIRect::MakeXYWH(bounds->x(), bounds->y(), bounds->width(), bounds->height());
91
+ return _surface->makeImageSnapshot(b);
92
+ } else {
93
+ return _surface->makeImageSnapshot();
94
+ }
95
+ }
96
+
97
+ /**
98
+ Returns the scaled width of the view
99
+ */
100
+ float getScaledWidth() override { return _width; };
101
+
102
+ /**
103
+ Returns the scaled height of the view
104
+ */
105
+ float getScaledHeight() override { return _height; };
106
+
107
+ /**
108
+ Render to a canvas
109
+ */
110
+ void renderToCanvas(const std::function<void(SkCanvas*)>& cb) override {
111
+ cb(_surface->getCanvas());
112
+ };
113
+
114
+ private:
115
+ float _width;
116
+ float _height;
117
+ sk_sp<SkSurface> _surface;
118
+ };
119
+
120
+ enum RNSkDrawingMode { Default, Continuous };
121
+
122
+ using RNSkTouchInfo = struct {
123
+ enum TouchType { Start, Active, End, Cancelled };
124
+ double x;
125
+ double y;
126
+ double force;
127
+ TouchType type;
128
+ size_t id;
129
+ long timestamp;
130
+ };
131
+
132
+ class RNSkView: public std::enable_shared_from_this<RNSkView> {
133
+ public:
134
+ /**
135
+ * Constructor
136
+ */
137
+ RNSkView(std::shared_ptr<RNSkPlatformContext> context,
138
+ std::shared_ptr<RNSkCanvasProvider> canvasProvider,
139
+ std::shared_ptr<RNSkRenderer> renderer):
140
+ _platformContext(context),
141
+ _canvasProvider(canvasProvider),
142
+ _renderer(renderer) {}
143
+
144
+ /**
145
+ Destructor
146
+ */
147
+ virtual ~RNSkView() {
148
+ endDrawingLoop();
149
+ }
150
+
151
+ /**
152
+ Sets custom properties. Custom properties are properties that are set directly from Javascript without having
153
+ to go through the async bridge.
154
+ */
155
+ virtual void setJsiProperties(std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) {
156
+ throw std::runtime_error("The base Skia View does not support any custom properties.");
157
+ };
158
+
159
+ /**
160
+ Calls a custom action.
161
+ */
162
+ virtual jsi::Value callJsiMethod(jsi::Runtime& runtime,
163
+ const std::string& name,
164
+ const jsi::Value *arguments,
165
+ size_t count) {
166
+ throw std::runtime_error("The base Skia View does not support any commands. Command " + name + " not found.");
167
+ };
168
+
169
+ /**
170
+ * Repaints the Skia view using the underlying context and the drawcallback.
171
+ * This method schedules a draw request that will be run on the correct
172
+ * thread and js runtime.
173
+ */
174
+ void requestRedraw() {
175
+ _redrawRequestCounter++;
176
+ }
177
+
178
+ /**
179
+ Sets the native id of the view
180
+ */
181
+ virtual void setNativeId(size_t nativeId) {
182
+ _nativeId = nativeId;
183
+ beginDrawingLoop();
184
+ }
185
+
186
+ /**
187
+ Returns the native id
188
+ */
189
+ size_t getNativeId() { return _nativeId; }
190
+
191
+ /**
192
+ Sets the drawing mode for the view
193
+ */
194
+ void setDrawingMode(RNSkDrawingMode mode) {
195
+ _drawingMode = mode;
196
+ requestRedraw();
197
+ }
198
+
199
+ /**
200
+ * Set to true to show the debug overlays on render
201
+ */
202
+ void setShowDebugOverlays(bool show) {
203
+ _renderer->setShowDebugOverlays(show);
204
+ requestRedraw();
205
+ }
206
+
207
+ /**
208
+ Update touch state with new touch points
209
+ */
210
+ virtual void updateTouchState(std::vector<RNSkTouchInfo>&) {
211
+ requestRedraw();
212
+ }
213
+
214
+ /**
215
+ Renders the view into an SkImage instead of the screen.
216
+ */
217
+ sk_sp<SkImage> makeImageSnapshot(std::shared_ptr<SkRect> bounds) {
218
+ auto provider = std::make_shared<RNSkImageCanvasProvider>(std::bind(&RNSkView::requestRedraw, this),
219
+ _canvasProvider->getScaledWidth(),
220
+ _canvasProvider->getScaledHeight());
221
+
222
+ _renderer->renderImmediate(provider);
223
+ return provider->makeSnapshot(bounds);
224
+ }
225
+
226
+ protected:
227
+ std::shared_ptr<RNSkPlatformContext> getPlatformContext() { return _platformContext; }
228
+ std::shared_ptr<RNSkCanvasProvider> getCanvasProvider() { return _canvasProvider; }
229
+ std::shared_ptr<RNSkRenderer> getRenderer() { return _renderer; }
230
+
231
+ /**
232
+ Ends an ongoing beginDrawCallback loop for this view. This method is made protected if
233
+ the drawing loop should be stopped before reaching the destructor (like we do for Android
234
+ views)
235
+ */
236
+ void endDrawingLoop() {
237
+ if(_drawingLoopId != 0) {
238
+ _drawingLoopId = 0;
239
+ _platformContext->endDrawLoop(_nativeId);
240
+ }
241
+ }
242
+
243
+ private:
244
+ /**
245
+ Starts beginDrawCallback loop if the drawing mode is continuous
246
+ */
247
+ void beginDrawingLoop() {
248
+ if (_drawingLoopId != 0 || _nativeId == 0) {
249
+ return;
250
+ }
251
+ // Set to zero to avoid calling beginDrawLoop before we return
252
+ _drawingLoopId = _platformContext->beginDrawLoop(_nativeId,
253
+ [weakSelf = weak_from_this()](bool invalidated) {
254
+ auto self = weakSelf.lock();
255
+ if(self) {
256
+ self->drawLoopCallback(invalidated);
257
+ }
258
+ });
259
+ }
260
+
261
+ /**
262
+ Draw loop callback
263
+ */
264
+ void drawLoopCallback(bool invalidated) {
265
+ if(_redrawRequestCounter > 0 || _drawingMode == RNSkDrawingMode::Continuous) {
266
+ _redrawRequestCounter = 0;
267
+
268
+ if(!_renderer->tryRender(_canvasProvider)) {
269
+ // The renderer could not render cause it was busy, just schedule redrawing
270
+ // on the next frame.
271
+ requestRedraw();
272
+ }
273
+ }
274
+ }
275
+
276
+ std::shared_ptr<RNSkPlatformContext> _platformContext;
277
+ std::shared_ptr<RNSkCanvasProvider> _canvasProvider;
278
+ std::shared_ptr<RNSkRenderer> _renderer;
279
+
280
+ RNSkDrawingMode _drawingMode;
281
+ size_t _nativeId;
282
+
283
+ size_t _drawingLoopId = 0;
284
+ std::atomic<int> _redrawRequestCounter = { 1 };
285
+ };
286
+
287
+ } // namespace RNSkia
@@ -0,0 +1,38 @@
1
+ #pragma once
2
+
3
+ #import "RNSkPlatformContext.h"
4
+ #import "RNSkView.h"
5
+
6
+ #import <MetalKit/MetalKit.h>
7
+ #import <QuartzCore/CAMetalLayer.h>
8
+
9
+ class RNSkMetalCanvasProvider: public RNSkia::RNSkCanvasProvider {
10
+ public:
11
+ RNSkMetalCanvasProvider(std::function<void()> requestRedraw,
12
+ std::shared_ptr<RNSkia::RNSkPlatformContext> context);
13
+
14
+ ~RNSkMetalCanvasProvider();
15
+
16
+ float getScaledWidth() override;
17
+ float getScaledHeight() override;
18
+
19
+ void renderToCanvas(const std::function<void(SkCanvas*)>& cb) override;
20
+
21
+ void setSize(int width, int height);
22
+
23
+ CALayer* getLayer();
24
+
25
+ private:
26
+ std::shared_ptr<RNSkia::RNSkPlatformContext> _context;
27
+ float _width = -1;
28
+ float _height = -1;
29
+ #pragma clang diagnostic push
30
+ #pragma clang diagnostic ignored "-Wunguarded-availability-new"
31
+ CAMetalLayer *_layer;
32
+ #pragma clang diagnostic pop
33
+
34
+ static id<MTLCommandQueue> _commandQueue;
35
+ static id<MTLDevice> _device;
36
+ static sk_sp<GrDirectContext> _skContext;
37
+
38
+ };
@@ -1,4 +1,5 @@
1
- #import <RNSkDrawViewImpl.h>
1
+ #import <RNSkMetalCanvasProvider.h>
2
+ #import <RNSkLog.h>
2
3
 
3
4
  #pragma clang diagnostic push
4
5
  #pragma clang diagnostic ignored "-Wdocumentation"
@@ -7,23 +8,24 @@
7
8
  #import <SkSurface.h>
8
9
  #import <SkCanvas.h>
9
10
 
10
- #pragma clang diagnostic pop
11
+ #import <include/gpu/GrDirectContext.h>
11
12
 
12
- #import <SkiaDrawView.h>
13
- #import <RNSkLog.h>
13
+ #pragma clang diagnostic pop
14
14
 
15
15
  // These static class members are used by all Skia Views
16
- id<MTLDevice> RNSkDrawViewImpl::_device = MTLCreateSystemDefaultDevice();
17
- id<MTLCommandQueue> RNSkDrawViewImpl::_commandQueue = id<MTLCommandQueue>(CFRetain((GrMTLHandle)[_device newCommandQueue]));
16
+ id<MTLDevice> RNSkMetalCanvasProvider::_device = MTLCreateSystemDefaultDevice();
17
+ id<MTLCommandQueue> RNSkMetalCanvasProvider::_commandQueue = id<MTLCommandQueue>(CFRetain((GrMTLHandle)[_device newCommandQueue]));
18
18
 
19
- sk_sp<GrDirectContext> RNSkDrawViewImpl::_skContext = nullptr;
19
+ sk_sp<GrDirectContext> RNSkMetalCanvasProvider::_skContext = nullptr;
20
20
 
21
- RNSkDrawViewImpl::RNSkDrawViewImpl(std::shared_ptr<RNSkia::RNSkPlatformContext> context):
22
- _context(context), RNSkia::RNSkDrawView(context) {
23
- #pragma clang diagnostic push
24
- #pragma clang diagnostic ignored "-Wunguarded-availability-new"
21
+ RNSkMetalCanvasProvider::RNSkMetalCanvasProvider(std::function<void()> requestRedraw,
22
+ std::shared_ptr<RNSkia::RNSkPlatformContext> context):
23
+ RNSkCanvasProvider(requestRedraw),
24
+ _context(context) {
25
+ #pragma clang diagnostic push
26
+ #pragma clang diagnostic ignored "-Wunguarded-availability-new"
25
27
  _layer = [CAMetalLayer layer];
26
- #pragma clang diagnostic pop
28
+ #pragma clang diagnostic pop
27
29
 
28
30
  _layer.framebufferOnly = NO;
29
31
  _layer.device = _device;
@@ -32,7 +34,7 @@ RNSkDrawViewImpl::RNSkDrawViewImpl(std::shared_ptr<RNSkia::RNSkPlatformContext>
32
34
  _layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
33
35
  }
34
36
 
35
- RNSkDrawViewImpl::~RNSkDrawViewImpl() {
37
+ RNSkMetalCanvasProvider::~RNSkMetalCanvasProvider() {
36
38
  if([[NSThread currentThread] isMainThread]) {
37
39
  _layer = NULL;
38
40
  } else {
@@ -50,17 +52,20 @@ RNSkDrawViewImpl::~RNSkDrawViewImpl() {
50
52
  }
51
53
  }
52
54
 
53
- void RNSkDrawViewImpl::setSize(int width, int height) {
54
- _width = width;
55
- _height = height;
56
- _layer.frame = CGRectMake(0, 0, width, height);
57
- _layer.drawableSize = CGSizeMake(width * _context->getPixelDensity(),
58
- height* _context->getPixelDensity());
59
-
60
- requestRedraw();
61
- }
55
+ /**
56
+ Returns the scaled width of the view
57
+ */
58
+ float RNSkMetalCanvasProvider::getScaledWidth() { return _width * _context->getPixelDensity(); };
59
+
60
+ /**
61
+ Returns the scaled height of the view
62
+ */
63
+ float RNSkMetalCanvasProvider::getScaledHeight() { return _height * _context->getPixelDensity(); };
62
64
 
63
- void RNSkDrawViewImpl::renderToSkiaCanvas(const std::function<void(SkCanvas*)>& cb) {
65
+ /**
66
+ Render to a canvas
67
+ */
68
+ void RNSkMetalCanvasProvider::renderToCanvas(const std::function<void(SkCanvas*)>& cb) {
64
69
  if(_width == -1 && _height == -1) {
65
70
  return;
66
71
  }
@@ -109,4 +114,16 @@ void RNSkDrawViewImpl::renderToSkiaCanvas(const std::function<void(SkCanvas*)>&
109
114
  [commandBuffer presentDrawable:currentDrawable];
110
115
  [commandBuffer commit];
111
116
  }
117
+ };
118
+
119
+ void RNSkMetalCanvasProvider::setSize(int width, int height) {
120
+ _width = width;
121
+ _height = height;
122
+ _layer.frame = CGRectMake(0, 0, width, height);
123
+ _layer.drawableSize = CGSizeMake(width * _context->getPixelDensity(),
124
+ height* _context->getPixelDensity());
125
+
126
+ _requestRedraw();
112
127
  }
128
+
129
+ CALayer* RNSkMetalCanvasProvider::getLayer() { return _layer; }
@@ -30,9 +30,9 @@ using namespace facebook;
30
30
 
31
31
  static void handleNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo);
32
32
 
33
- class PlatformContext : public RNSkPlatformContext {
33
+ class RNSkiOSPlatformContext : public RNSkPlatformContext {
34
34
  public:
35
- PlatformContext(jsi::Runtime *runtime,
35
+ RNSkiOSPlatformContext(jsi::Runtime *runtime,
36
36
  std::shared_ptr<react::CallInvoker> callInvoker)
37
37
  : RNSkPlatformContext(runtime, callInvoker, [[UIScreen mainScreen] scale]) {
38
38
  // We need to make sure we invalidate when modules are freed
@@ -46,7 +46,7 @@ public:
46
46
  );
47
47
  }
48
48
 
49
- ~PlatformContext() {
49
+ ~RNSkiOSPlatformContext() {
50
50
  CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetLocalCenter(), this);
51
51
  }
52
52
 
@@ -70,7 +70,7 @@ private:
70
70
 
71
71
  static void handleNotification(CFNotificationCenterRef center, void *observer, CFStringRef name,
72
72
  const void *object, CFDictionaryRef userInfo) {
73
- (static_cast<PlatformContext*>(observer))->willInvalidateModules();
73
+ (static_cast<RNSkiOSPlatformContext*>(observer))->willInvalidateModules();
74
74
  }
75
75
 
76
76
  } // namespace RNSkia
@@ -1,4 +1,4 @@
1
- #include "PlatformContext.h"
1
+ #include "RNSkiOSPlatformContext.h"
2
2
 
3
3
  #import <React/RCTUtils.h>
4
4
  #include <thread>
@@ -8,7 +8,7 @@
8
8
 
9
9
  namespace RNSkia {
10
10
 
11
- void PlatformContext::performStreamOperation(const std::string &sourceUri,
11
+ void RNSkiOSPlatformContext::performStreamOperation(const std::string &sourceUri,
12
12
  const std::function<void(std::unique_ptr<SkStreamAsset>)> &op) {
13
13
 
14
14
  RNSkMeasureTime("PlatformContext::performStreamOperation");
@@ -28,11 +28,11 @@ namespace RNSkia {
28
28
  std::thread(loader).detach();
29
29
  }
30
30
 
31
- void PlatformContext::raiseError(const std::exception &err) {
31
+ void RNSkiOSPlatformContext::raiseError(const std::exception &err) {
32
32
  RCTFatal(RCTErrorWithMessage([NSString stringWithUTF8String:err.what()]));
33
33
  }
34
34
 
35
- void PlatformContext::startDrawLoop() {
35
+ void RNSkiOSPlatformContext::startDrawLoop() {
36
36
  if(_displayLink == nullptr) {
37
37
  _displayLink = [[DisplayLink alloc] init];
38
38
  [_displayLink start:^(double time) {
@@ -41,7 +41,7 @@ void PlatformContext::startDrawLoop() {
41
41
  }
42
42
  }
43
43
 
44
- void PlatformContext::stopDrawLoop() {
44
+ void RNSkiOSPlatformContext::stopDrawLoop() {
45
45
  if(_displayLink != nullptr) {
46
46
  [_displayLink stop];
47
47
  _displayLink = nullptr;
@@ -0,0 +1,34 @@
1
+ #pragma once
2
+
3
+ #import <memory>
4
+
5
+ #import "RNSkView.h"
6
+ #import "RNSkiOSPlatformContext.h"
7
+ #import "RNSkMetalCanvasProvider.h"
8
+
9
+ class RNSkBaseiOSView {
10
+ public:
11
+ virtual CALayer* getLayer() = 0;
12
+ virtual void setSize(int width, int height) = 0;
13
+ virtual std::shared_ptr<RNSkia::RNSkView> getDrawView() = 0;
14
+ };
15
+
16
+ template <class T>
17
+ class RNSkiOSView: public RNSkBaseiOSView, public T {
18
+ public:
19
+ RNSkiOSView(std::shared_ptr<RNSkia::RNSkPlatformContext> context):
20
+ T(context, std::make_shared<RNSkMetalCanvasProvider>(std::bind(&RNSkia::RNSkView::requestRedraw, this), context)) {}
21
+
22
+ CALayer* getLayer() override {
23
+ return std::static_pointer_cast<RNSkMetalCanvasProvider>(this->getCanvasProvider())->getLayer();
24
+ }
25
+
26
+ void setSize(int width, int height) override {
27
+ std::static_pointer_cast<RNSkMetalCanvasProvider>(this->getCanvasProvider())->setSize(width, height);
28
+ }
29
+
30
+ std::shared_ptr<RNSkia::RNSkView> getDrawView() override {
31
+ return this->shared_from_this();
32
+ }
33
+
34
+ };
@@ -1,42 +1,50 @@
1
1
  #include <SkiaDrawViewManager.h>
2
2
  #include <React/RCTBridge+Private.h>
3
3
 
4
+ #include <RNSkPlatformContext.h>
5
+ #include <RNSkJsView.h>
6
+ #include <RNSkIOSView.h>
7
+
4
8
  #include <SkiaManager.h>
5
9
  #include <RNSkiaModule.h>
6
- #include <RNSkDrawViewImpl.h>
7
- #include <SkiaDrawView.h>
10
+ #include <SkiaUIView.h>
11
+
12
+
8
13
 
9
14
  @implementation SkiaDrawViewManager
10
15
 
16
+ RCT_EXPORT_MODULE(SkiaDrawView)
17
+
11
18
  - (SkiaManager*) skiaManager {
12
19
  auto bridge = [RCTBridge currentBridge];
13
20
  auto skiaModule = (RNSkiaModule*)[bridge moduleForName:@"RNSkia"];
14
21
  return [skiaModule manager];
15
22
  }
16
23
 
17
- RCT_CUSTOM_VIEW_PROPERTY(nativeID, NSNumber, SkiaDrawView) {
24
+ RCT_CUSTOM_VIEW_PROPERTY(nativeID, NSNumber, SkiaUIView) {
18
25
  // Get parameter
19
26
  int nativeId = [[RCTConvert NSString:json] intValue];
20
- [(SkiaDrawView*)view setNativeId:nativeId];
27
+ [(SkiaUIView*)view setNativeId:nativeId];
21
28
  }
22
29
 
23
- RCT_CUSTOM_VIEW_PROPERTY(mode, NSString, SkiaDrawView) {
30
+ RCT_CUSTOM_VIEW_PROPERTY(mode, NSString, SkiaUIView) {
24
31
  std::string mode = json != NULL ? [[RCTConvert NSString:json] UTF8String] : "default";
25
- [(SkiaDrawView*)view setDrawingMode: mode];
32
+ [(SkiaUIView*)view setDrawingMode: mode];
26
33
  }
27
34
 
28
- RCT_CUSTOM_VIEW_PROPERTY(debug, BOOL, SkiaDrawView) {
35
+ RCT_CUSTOM_VIEW_PROPERTY(debug, BOOL, SkiaUIView) {
29
36
  bool debug = json != NULL ? [RCTConvert BOOL:json] : false;
30
- [(SkiaDrawView*)view setDebugMode: debug];
37
+ [(SkiaUIView*)view setDebugMode: debug];
31
38
  }
32
39
 
33
- RCT_EXPORT_MODULE(ReactNativeSkiaView)
34
-
35
40
  - (UIView *)view
36
41
  {
37
42
  auto skManager = [[self skiaManager] skManager];
38
43
  // Pass SkManager as a raw pointer to avoid circular dependenciesr
39
- return [[SkiaDrawView alloc] initWithManager:skManager.get()];
44
+ return [[SkiaUIView alloc] initWithManager: skManager.get()
45
+ factory: [](std::shared_ptr<RNSkia::RNSkPlatformContext> context) {
46
+ return std::make_shared<RNSkiOSView<RNSkia::RNSkJsView>>(context);
47
+ }];
40
48
  }
41
49
 
42
50
  @end
@@ -7,11 +7,11 @@
7
7
 
8
8
  #import <ReactCommon/RCTTurboModule.h>
9
9
 
10
- #import "PlatformContext.h"
10
+ #import "RNSkiOSPlatformContext.h"
11
11
 
12
12
  @implementation SkiaManager {
13
13
  std::shared_ptr<RNSkia::RNSkManager> _skManager;
14
- std::shared_ptr<RNSkia::PlatformContext> _platformContext;
14
+ std::shared_ptr<RNSkia::RNSkiOSPlatformContext> _platformContext;
15
15
  __weak RCTBridge* weakBridge;
16
16
  }
17
17
 
@@ -37,7 +37,7 @@
37
37
  facebook::jsi::Runtime* jsRuntime = (facebook::jsi::Runtime*)cxxBridge.runtime;
38
38
 
39
39
  // Create platform context
40
- _platformContext = std::make_shared<RNSkia::PlatformContext>(jsRuntime, callInvoker);
40
+ _platformContext = std::make_shared<RNSkia::RNSkiOSPlatformContext>(jsRuntime, callInvoker);
41
41
 
42
42
  // Create the RNSkiaManager (cross platform)
43
43
  _skManager = std::make_shared<RNSkia::RNSkManager>(jsRuntime, callInvoker, _platformContext);
@@ -0,0 +1,8 @@
1
+
2
+ #pragma once
3
+
4
+ #import <React/RCTViewManager.h>
5
+
6
+ @interface SkiaPictureViewManager : RCTViewManager
7
+
8
+ @end
@@ -0,0 +1,51 @@
1
+
2
+ #include <SkiaPictureViewManager.h>
3
+ #include <React/RCTBridge+Private.h>
4
+
5
+ #include <RNSkPlatformContext.h>
6
+ #include <RNSkPictureView.h>
7
+ #include <RNSkIOSView.h>
8
+
9
+ #include <SkiaManager.h>
10
+ #include <RNSkiaModule.h>
11
+ #include <SkiaUIView.h>
12
+
13
+
14
+
15
+ @implementation SkiaPictureViewManager
16
+
17
+ RCT_EXPORT_MODULE(SkiaPictureView)
18
+
19
+ - (SkiaManager*) skiaManager {
20
+ auto bridge = [RCTBridge currentBridge];
21
+ auto skiaModule = (RNSkiaModule*)[bridge moduleForName:@"RNSkia"];
22
+ return [skiaModule manager];
23
+ }
24
+
25
+ RCT_CUSTOM_VIEW_PROPERTY(nativeID, NSNumber, SkiaUIView) {
26
+ // Get parameter
27
+ int nativeId = [[RCTConvert NSString:json] intValue];
28
+ [(SkiaUIView*)view setNativeId:nativeId];
29
+ }
30
+
31
+ RCT_CUSTOM_VIEW_PROPERTY(mode, NSString, SkiaUIView) {
32
+ std::string mode = json != NULL ? [[RCTConvert NSString:json] UTF8String] : "default";
33
+ [(SkiaUIView*)view setDrawingMode: mode];
34
+ }
35
+
36
+ RCT_CUSTOM_VIEW_PROPERTY(debug, BOOL, SkiaUIView) {
37
+ bool debug = json != NULL ? [RCTConvert BOOL:json] : false;
38
+ [(SkiaUIView*)view setDebugMode: debug];
39
+ }
40
+
41
+ - (UIView *)view
42
+ {
43
+ auto skManager = [[self skiaManager] skManager];
44
+ // Pass SkManager as a raw pointer to avoid circular dependenciesr
45
+ return [[SkiaUIView alloc] initWithManager: skManager.get()
46
+ factory: [](std::shared_ptr<RNSkia::RNSkPlatformContext> context) {
47
+ return std::make_shared<RNSkiOSView<RNSkia::RNSkPictureView>>(context);
48
+ }];
49
+ }
50
+
51
+ @end