@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
@@ -1,73 +0,0 @@
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
- if(width == 0 && height == 0) {
57
- // Setting width/height to zero is nothing we need to care about when
58
- // it comes to invalidating the surface.
59
- return;
60
- }
61
- _scaledWidth = width;
62
- _scaledHeight = height;
63
-
64
- // Redraw after size change
65
- requestRedraw();
66
- }
67
-
68
- void RNSkDrawViewImpl::renderToSkiaCanvas(const std::function<void(SkCanvas*)>& cb) {
69
- if(_renderer != nullptr) {
70
- _renderer->run(cb, _scaledWidth, _scaledHeight);
71
- }
72
- }
73
- }
@@ -1,48 +0,0 @@
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 renderToSkiaCanvas(const std::function<void(SkCanvas*)>&) 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
- }
@@ -1,315 +0,0 @@
1
- //
2
- // Created by Christian Falch on 23/08/2021.
3
- //
4
-
5
- #include "RNSkDrawView.h"
6
-
7
- #include <chrono>
8
- #include <functional>
9
- #include <sstream>
10
- #include <string>
11
- #include <memory>
12
- #include <vector>
13
- #include <utility>
14
-
15
- #include <JsiSkCanvas.h>
16
- #include <RNSkLog.h>
17
- #include <RNSkPlatformContext.h>
18
-
19
- #pragma clang diagnostic push
20
- #pragma clang diagnostic ignored "-Wdocumentation"
21
-
22
- #include <SkBBHFactory.h>
23
- #include <SkCanvas.h>
24
- #include <SkFont.h>
25
- #include <SkFontTypes.h>
26
- #include <SkGraphics.h>
27
- #include <SkPaint.h>
28
- #include <SkPictureRecorder.h>
29
- #include <SkSurface.h>
30
- #include <SkRect.h>
31
-
32
- #pragma clang diagnostic pop
33
-
34
- namespace RNSkia {
35
-
36
- using namespace std::chrono;
37
-
38
- RNSkDrawView::RNSkDrawView(std::shared_ptr<RNSkPlatformContext> context)
39
- : _jsiCanvas(std::make_shared<JsiSkCanvas>(context)),
40
- _platformContext(std::move(context)),
41
- _infoObject(std::make_shared<RNSkInfoObject>()),
42
- _jsDrawingLock(std::make_shared<std::timed_mutex>()),
43
- _gpuDrawingLock(std::make_shared<std::timed_mutex>()),
44
- _jsTimingInfo("SKIA/JS"),
45
- _gpuTimingInfo("SKIA/GPU")
46
- {}
47
-
48
- RNSkDrawView::~RNSkDrawView() {
49
- endDrawingLoop();
50
- }
51
-
52
- void RNSkDrawView::setJsiProperties(std::unordered_map<std::string, JsiValueWrapper> &props) {
53
- for(auto& prop: props) {
54
- if(prop.first == "drawCallback") {
55
- if(prop.second.isUndefinedOrNull()) {
56
- // Clear drawcallback
57
- _drawCallback = nullptr;
58
- // We can just reset everything - this is a signal that we're done.
59
- endDrawingLoop();
60
- return;
61
- } else if (prop.second.getType() != JsiWrapperValueType::Function) {
62
- // We expect a function for the draw callback custom property
63
- throw std::runtime_error("Expected a function for the drawCallback custom property.");
64
- }
65
-
66
- // Save callback
67
- _drawCallback = prop.second.getAsFunction();
68
-
69
- // Request redraw
70
- requestRedraw();
71
-
72
- } else {
73
- throw std::runtime_error("Property " + prop.first + " not found.");
74
- }
75
- }
76
- }
77
-
78
- jsi::Value RNSkDrawView::callJsiMethod(jsi::Runtime& runtime,
79
- const std::string& name,
80
- const jsi::Value *arguments,
81
- size_t count) {
82
-
83
- // This subclass doesn't support any jsi methods at the moment, but
84
- // new subclasses created in the future will.
85
- throw std::runtime_error("RNSkDrawView does not support any JSI methods. Method " + name + "() not found.");
86
- return jsi::Value::undefined();
87
- }
88
-
89
- void RNSkDrawView::setNativeId(size_t nativeId) {
90
- _nativeId = nativeId;
91
- beginDrawingLoop();
92
- }
93
-
94
- void RNSkDrawView::callJsDrawCallback(std::shared_ptr<JsiSkCanvas> canvas,
95
- int width,
96
- int height,
97
- double timestamp) {
98
- if(_drawCallback == nullptr) {
99
- return;
100
- }
101
-
102
- // Reset timing info
103
- _jsTimingInfo.reset();
104
- _gpuTimingInfo.reset();
105
-
106
- auto runtime = getPlatformContext()->getJsRuntime();
107
-
108
- // Update info parameter
109
- _infoObject->beginDrawOperation(width, height, timestamp);
110
-
111
- // Set up arguments array
112
- std::vector<jsi::Value> args(2);
113
- args[0] = jsi::Object::createFromHostObject(*runtime, canvas);
114
- args[1] = jsi::Object::createFromHostObject(*runtime, _infoObject);
115
-
116
- // To be able to call the drawing function we'll wrap it once again
117
- _drawCallback->call(*runtime,
118
- static_cast<const jsi::Value *>(args.data()),
119
- (size_t)2);
120
-
121
- // Reset touches
122
- _infoObject->endDrawOperation();
123
-
124
- // Draw debug overlays
125
- if (_showDebugOverlay) {
126
-
127
- // Display average rendering timer
128
- auto jsAvg = _jsTimingInfo.getAverage();
129
- //auto jsFps = _jsTimingInfo.getFps();
130
-
131
- auto gpuAvg = _gpuTimingInfo.getAverage();
132
- //auto gpuFps = _gpuTimingInfo.getFps();
133
-
134
- auto total = jsAvg + gpuAvg;
135
-
136
- // Build string
137
- std::ostringstream stream;
138
- stream << "js: " << jsAvg << "ms gpu: " << gpuAvg << "ms " << " total: " << total << "ms";
139
-
140
- std::string debugString = stream.str();
141
-
142
- // Set up debug font/paints
143
- auto font = SkFont();
144
- font.setSize(14);
145
- auto paint = SkPaint();
146
- paint.setColor(SkColors::kRed);
147
- canvas->getCanvas()->drawSimpleText(
148
- debugString.c_str(), debugString.size(), SkTextEncoding::kUTF8, 8,
149
- 18, font, paint);
150
- }
151
- }
152
-
153
- void RNSkDrawView::drawInCanvas(std::shared_ptr<JsiSkCanvas> canvas,
154
- int width,
155
- int height,
156
- double time) {
157
-
158
- // Call the draw drawCallback and perform js based drawing
159
- auto skCanvas = canvas->getCanvas();
160
- if (_drawCallback != nullptr && skCanvas != nullptr) {
161
- // Make sure to scale correctly
162
- auto pd = _platformContext->getPixelDensity();
163
- skCanvas->save();
164
- skCanvas->scale(pd, pd);
165
-
166
- // Call draw function.
167
- callJsDrawCallback(canvas, width / pd, height / pd, time);
168
-
169
- // Restore and flush canvas
170
- skCanvas->restore();
171
- skCanvas->flush();
172
- }
173
- }
174
-
175
- sk_sp<SkImage> RNSkDrawView::makeImageSnapshot(std::shared_ptr<SkRect> bounds) {
176
- // Assert width/height
177
- auto surface = SkSurface::MakeRasterN32Premul(getScaledWidth(), getScaledHeight());
178
- auto canvas = surface->getCanvas();
179
- auto jsiCanvas = std::make_shared<JsiSkCanvas>(_platformContext);
180
- jsiCanvas->setCanvas(canvas);
181
-
182
- milliseconds ms = duration_cast<milliseconds>(
183
- system_clock::now().time_since_epoch());
184
-
185
- drawInCanvas(jsiCanvas, getScaledWidth(), getScaledHeight(), ms.count() / 1000);
186
-
187
- if(bounds != nullptr) {
188
- SkIRect b = SkIRect::MakeXYWH(bounds->x(), bounds->y(), bounds->width(), bounds->height());
189
- return surface->makeImageSnapshot(b);
190
- } else {
191
- return surface->makeImageSnapshot();
192
- }
193
- }
194
-
195
- void RNSkDrawView::updateTouchState(std::vector<RNSkTouchPoint>&& points) {
196
- _infoObject->updateTouches(std::move(points));
197
- requestRedraw();
198
- }
199
-
200
- void RNSkDrawView::performDraw() {
201
- // Start timing
202
- _jsTimingInfo.beginTiming();
203
-
204
- // Record the drawing operations on the JS thread so that we can
205
- // move the actual drawing onto the render thread later
206
- SkPictureRecorder recorder;
207
- SkRTreeFactory factory;
208
- SkCanvas* canvas = recorder.beginRecording(getScaledWidth(), getScaledHeight(), &factory);
209
- _jsiCanvas->setCanvas(canvas);
210
-
211
- // Get current milliseconds
212
- milliseconds ms = duration_cast<milliseconds>(
213
- system_clock::now().time_since_epoch());
214
-
215
- try {
216
- // Perform the javascript drawing
217
- drawInCanvas(_jsiCanvas, getScaledWidth(), getScaledHeight(), ms.count() / 1000.0);
218
- } catch(...) {
219
- _jsTimingInfo.stopTiming();
220
- _jsDrawingLock->unlock();
221
- throw;
222
- }
223
-
224
- // Finish drawing operations
225
- auto p = recorder.finishRecordingAsPicture();
226
-
227
- _jsiCanvas->setCanvas(nullptr);
228
-
229
- // Calculate duration
230
- _jsTimingInfo.stopTiming();
231
-
232
- if(_gpuDrawingLock->try_lock()) {
233
-
234
- // Post drawing message to the render thread where the picture recorded
235
- // will be sent to the GPU/backend for rendering to screen.
236
- auto gpuLock = _gpuDrawingLock;
237
- _platformContext->runOnRenderThread([weakSelf = weak_from_this(), p = std::move(p), gpuLock]() {
238
- auto self = weakSelf.lock();
239
- if (self) {
240
- // Draw the picture recorded on the real GPU canvas
241
- self->_gpuTimingInfo.beginTiming();
242
- self->renderToSkiaCanvas([p = std::move(p)](SkCanvas* canvas) {
243
- canvas->drawPicture(p);
244
- });
245
- self->_gpuTimingInfo.stopTiming();
246
- }
247
- // Unlock GPU drawing
248
- gpuLock->unlock();
249
- });
250
- } else {
251
- #ifdef DEBUG
252
- _gpuTimingInfo.markSkipped();
253
- #endif
254
- // Request a new redraw since the last frame was skipped.
255
- requestRedraw();
256
- }
257
-
258
- // Unlock JS drawing
259
- _jsDrawingLock->unlock();
260
- }
261
-
262
- void RNSkDrawView::requestRedraw() {
263
- _redrawRequestCounter++;
264
- }
265
-
266
- void RNSkDrawView::beginDrawingLoop() {
267
- if (_drawingLoopId != 0 || _nativeId == 0) {
268
- return;
269
- }
270
- // Set to zero to avoid calling beginDrawLoop before we return
271
- _drawingLoopId = _platformContext->beginDrawLoop(_nativeId,
272
- [weakSelf = weak_from_this()](bool invalidated) {
273
- auto self = weakSelf.lock();
274
- if(self) {
275
- self->drawLoopCallback(invalidated);
276
- }
277
- });
278
- }
279
-
280
- void RNSkDrawView::drawLoopCallback(bool invalidated) {
281
- if(_redrawRequestCounter > 0 || _drawingMode == RNSkDrawingMode::Continuous) {
282
- _redrawRequestCounter = 0;
283
-
284
- // We render on the javascript thread.
285
- if(_jsDrawingLock->try_lock()) {
286
- _platformContext->runOnJavascriptThread([weakSelf = weak_from_this()](){
287
- auto self = weakSelf.lock();
288
- if(self) {
289
- self->performDraw();
290
- }
291
- });
292
- } else {
293
- #ifdef DEBUG
294
- _jsTimingInfo.markSkipped();
295
- #endif
296
- requestRedraw();
297
- }
298
- }
299
- }
300
-
301
- void RNSkDrawView::endDrawingLoop() {
302
- if(_drawingLoopId != 0) {
303
- _drawingLoopId = 0;
304
- _platformContext->endDrawLoop(_nativeId);
305
- }
306
- }
307
-
308
- void RNSkDrawView::setDrawingMode(RNSkDrawingMode mode) {
309
- if(mode == _drawingMode || _nativeId == 0) {
310
- return;
311
- }
312
- _drawingMode = mode;
313
- }
314
-
315
- } // namespace RNSkia
@@ -1,226 +0,0 @@
1
- #pragma once
2
-
3
- #include <functional>
4
- #include <memory>
5
- #include <mutex>
6
- #include <vector>
7
- #include <string>
8
-
9
- #include <jsi/jsi.h>
10
-
11
- #include <JsiValueWrapper.h>
12
-
13
- #include <RNSkInfoParameter.h>
14
- #include <RNSkPlatformContext.h>
15
- #include <RNSkTimingInfo.h>
16
- #include <RNSkLog.h>
17
-
18
- #pragma clang diagnostic push
19
- #pragma clang diagnostic ignored "-Wdocumentation"
20
-
21
- #include <SkCanvas.h>
22
-
23
- #pragma clang diagnostic pop
24
-
25
- class SkPicture;
26
- class SkRect;
27
- class SkImage;
28
-
29
- namespace RNSkia {
30
- class JsiSkCanvas;
31
- using namespace facebook;
32
-
33
- enum RNSkDrawingMode { Default, Continuous };
34
-
35
- class RNSkDrawView: public std::enable_shared_from_this<RNSkDrawView> {
36
- public:
37
- /**
38
- * Constructor
39
- */
40
- RNSkDrawView(std::shared_ptr<RNSkPlatformContext> context);
41
-
42
- /**
43
- Destructor
44
- */
45
- virtual ~RNSkDrawView();
46
-
47
- /**
48
- Sets custom properties. Custom properties are properties that are set directly from Javascript without having
49
- to go through the async bridge.
50
- */
51
- void setJsiProperties(std::unordered_map<std::string, JsiValueWrapper> &props);
52
-
53
- /**
54
- Calls a custom action.
55
- */
56
- jsi::Value callJsiMethod(jsi::Runtime& runtime,
57
- const std::string& name,
58
- const jsi::Value *arguments,
59
- size_t count);
60
-
61
- /**
62
- * Repaints the Skia view using the underlying context and the drawcallback.
63
- * This method schedules a draw request that will be run on the correct
64
- * thread and js runtime.
65
- */
66
- void requestRedraw();
67
-
68
- /**
69
- Calls the drawing callback on the javascript thread
70
- */
71
- void performDraw();
72
-
73
- /**
74
- Sets the native id of the view
75
- */
76
- void setNativeId(size_t nativeId);
77
-
78
- /**
79
- Returns the native id
80
- */
81
- size_t getNativeId() { return _nativeId; }
82
-
83
- /**
84
- Sets the drawing mode for the view
85
- */
86
- void setDrawingMode(RNSkDrawingMode mode);
87
-
88
- /**
89
- * Set to true to show the debug overlays on render
90
- */
91
- void setShowDebugOverlays(bool show) { _showDebugOverlay = show; }
92
-
93
- /**
94
- Update touch state with new touch points
95
- */
96
- void updateTouchState(std::vector<RNSkTouchPoint>&& points);
97
-
98
- /**
99
- Draws the view's surface into an image
100
- return an SkImage
101
- */
102
- sk_sp<SkImage> makeImageSnapshot(std::shared_ptr<SkRect> bounds);
103
-
104
- protected:
105
- /**
106
- Returns the scaled width of the view
107
- */
108
- virtual float getScaledWidth() = 0;
109
-
110
- /**
111
- Returns the scaled height of the view
112
- */
113
- virtual float getScaledHeight() = 0;
114
-
115
- /**
116
- Override to render to a canvas
117
- */
118
- virtual void renderToSkiaCanvas(const std::function<void(SkCanvas*)>&) = 0;
119
-
120
- /**
121
- * @return The platformcontext
122
- */
123
- std::shared_ptr<RNSkPlatformContext> getPlatformContext() {
124
- return _platformContext;
125
- }
126
-
127
- private:
128
- /**
129
- * Calls the JS draw callback. This is the JS function that does the actual drawing so this
130
- * method must be called from the JS thread.
131
- */
132
- void callJsDrawCallback(std::shared_ptr<JsiSkCanvas> canvas, int width, int height, double timestamp);
133
-
134
- /**
135
- Starts beginDrawCallback loop if the drawing mode is continuous
136
- */
137
- void beginDrawingLoop();
138
-
139
- /**
140
- Ends an ongoing beginDrawCallback loop for this view
141
- */
142
- void endDrawingLoop();
143
-
144
- /**
145
- Draw loop callback
146
- */
147
- void drawLoopCallback(bool invalidated);
148
-
149
- /**
150
- Draw in canvas
151
- */
152
- void drawInCanvas(std::shared_ptr<JsiSkCanvas> canvas,
153
- int width,
154
- int height,
155
- double time);
156
-
157
- /**
158
- * Stores the draw drawCallback
159
- */
160
- std::shared_ptr<jsi::Function> _drawCallback;
161
-
162
- /**
163
- * Stores a pointer to the jsi wrapper for the canvas. The reason for
164
- * storing this pointer and not recreate it is that it creates a set of
165
- * functions that we don't want to recreate on each render
166
- */
167
- std::shared_ptr<JsiSkCanvas> _jsiCanvas;
168
-
169
- /**
170
- * JS Drawing mutex
171
- */
172
- std::shared_ptr<std::timed_mutex> _jsDrawingLock;
173
-
174
- /**
175
- * SKIA Drawing mutex
176
- */
177
- std::shared_ptr<std::timed_mutex> _gpuDrawingLock;
178
-
179
- /**
180
- * Pointer to the platform context
181
- */
182
- std::shared_ptr<RNSkPlatformContext> _platformContext;
183
-
184
- /**
185
- Drawing mode
186
- */
187
- RNSkDrawingMode _drawingMode;
188
-
189
- /**
190
- * Show debug overlays
191
- */
192
- bool _showDebugOverlay = false;
193
-
194
- /**
195
- * True if the drawing loop has been requested
196
- */
197
- size_t _drawingLoopId = 0;
198
-
199
- /**
200
- * Info object parameter
201
- */
202
- std::shared_ptr<RNSkInfoObject> _infoObject;
203
-
204
- /**
205
- Timing information for javascript drawing
206
- */
207
- RNSkTimingInfo _jsTimingInfo;
208
-
209
- /**
210
- Timing information for GPU rendering
211
- */
212
- RNSkTimingInfo _gpuTimingInfo;
213
-
214
- /**
215
- Redraw queue counter
216
- */
217
- std::atomic<int> _redrawRequestCounter = { 1 };
218
-
219
- /**
220
- * Native id
221
- */
222
- size_t _nativeId;
223
-
224
- };
225
-
226
- } // namespace RNSkia
@@ -1,52 +0,0 @@
1
- #pragma once
2
-
3
- #import "PlatformContext.h"
4
- #import "RNSKDrawView.h"
5
- #import <CoreFoundation/CoreFoundation.h>
6
- #import <UIKit/UIKit.h>
7
-
8
- #import <GrMtlBackendContext.h>
9
- #import <MetalKit/MetalKit.h>
10
- #import <QuartzCore/CAMetalLayer.h>
11
-
12
- #pragma clang diagnostic push
13
- #pragma clang diagnostic ignored "-Wdocumentation"
14
-
15
- #import <SkPicture.h>
16
- #import <SkRefCnt.h>
17
- #import <include/gpu/GrDirectContext.h>
18
-
19
- #pragma clang diagnostic pop
20
-
21
- class RNSkDrawViewImpl : public RNSkia::RNSkDrawView {
22
- public:
23
- RNSkDrawViewImpl(std::shared_ptr<RNSkia::RNSkPlatformContext> context);
24
- ~RNSkDrawViewImpl();
25
-
26
- CALayer* getLayer() { return _layer; }
27
-
28
- void setSize(int width, int height);
29
-
30
- protected:
31
- float getScaledWidth() override { return _width * _context->getPixelDensity(); };
32
- float getScaledHeight() override { return _height * _context->getPixelDensity(); };
33
-
34
- private:
35
- void renderToSkiaCanvas(const std::function<void(SkCanvas*)>& cb) override;
36
- bool createSkiaSurface();
37
-
38
- int _nativeId;
39
- float _width = -1;
40
- float _height = -1;
41
-
42
- #pragma clang diagnostic push
43
- #pragma clang diagnostic ignored "-Wunguarded-availability-new"
44
- CAMetalLayer *_layer;
45
- #pragma clang diagnostic pop
46
-
47
- static id<MTLCommandQueue> _commandQueue;
48
- static id<MTLDevice> _device;
49
- static sk_sp<GrDirectContext> _skContext;
50
-
51
- std::shared_ptr<RNSkia::RNSkPlatformContext> _context;
52
- };