@shopify/react-native-skia 0.1.151 → 0.1.153

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 +5 -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} +46 -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 +3 -3
  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,113 +0,0 @@
1
- #include "JniSkiaDrawView.h"
2
- #include "RNSkLog.h"
3
-
4
- #include <memory>
5
- #include <string>
6
- #include <utility>
7
- #include <vector>
8
-
9
- #include <GLES2/gl2.h>
10
- #include <EGL/eglplatform.h>
11
- #include <android/native_window.h>
12
- #include <android/native_window_jni.h>
13
-
14
- #include <SkCanvas.h>
15
- #include <SkImageInfo.h>
16
- #include <SkSurface.h>
17
- #include <gpu/GrDirectContext.h>
18
- #include <gpu/GrBackendSurface.h>
19
- #include <gpu/gl/GrGLInterface.h>
20
- #include <gpu/gl/GrGLTypes.h>
21
- #include <gpu/GrTypes.h>
22
- #include <RNSkInfoParameter.h>
23
-
24
- namespace RNSkia
25
- {
26
- using namespace facebook;
27
- using namespace jni;
28
-
29
- using TSelf = local_ref<HybridClass<JniSkiaDrawView>::jhybriddata>;
30
-
31
- /**** DTOR ***/
32
- JniSkiaDrawView::~JniSkiaDrawView()
33
- {
34
- }
35
-
36
- /**** JNI ****/
37
-
38
- TSelf JniSkiaDrawView::initHybrid(
39
- alias_ref<HybridClass::jhybridobject> jThis,
40
- JavaSkiaManager skiaManager)
41
- {
42
- return makeCxxInstance(jThis, skiaManager);
43
- }
44
-
45
- void JniSkiaDrawView::registerNatives()
46
- {
47
- registerHybrid({makeNativeMethod("initHybrid", JniSkiaDrawView::initHybrid),
48
- makeNativeMethod("surfaceAvailable", JniSkiaDrawView::surfaceAvailable),
49
- makeNativeMethod("surfaceDestroyed", JniSkiaDrawView::surfaceDestroyed),
50
- makeNativeMethod("surfaceSizeChanged", JniSkiaDrawView::surfaceSizeChanged),
51
- makeNativeMethod("setMode", JniSkiaDrawView::setMode),
52
- makeNativeMethod("setDebugMode", JniSkiaDrawView::setDebugMode),
53
- makeNativeMethod("updateTouchPoints", JniSkiaDrawView::updateTouchPoints)});
54
- }
55
-
56
- void JniSkiaDrawView::setMode(std::string mode)
57
- {
58
- if (mode.compare("continuous") == 0)
59
- {
60
- _drawView->setDrawingMode(RNSkDrawingMode::Continuous);
61
- }
62
- else
63
- {
64
- _drawView->setDrawingMode(RNSkDrawingMode::Default);
65
- }
66
- }
67
-
68
- void JniSkiaDrawView::setDebugMode(bool show)
69
- {
70
- _drawView->setShowDebugOverlays(show);
71
- }
72
-
73
- void JniSkiaDrawView::updateTouchPoints(jni::JArrayDouble touches)
74
- {
75
- // Create touch points
76
- std::vector<RNSkia::RNSkTouchPoint> points;
77
- auto pin = touches.pin();
78
- auto scale = _drawView->getPixelDensity();
79
- points.reserve(pin.size() / 5);
80
- for (size_t i = 0; i < pin.size(); i += 5)
81
- {
82
- RNSkTouchPoint point;
83
- point.x = pin[i] / scale;
84
- point.y = pin[i + 1] / scale;
85
- point.force = pin[i + 2];
86
- point.type = (RNSkia::RNSkTouchType)pin[i + 3];
87
- point.id = pin[i + 4];
88
- points.push_back(point);
89
- }
90
- _drawView->updateTouchState(std::move(points));
91
- }
92
-
93
- void JniSkiaDrawView::surfaceAvailable(jobject surface, int width, int height)
94
- {
95
- _drawView->surfaceAvailable(ANativeWindow_fromSurface(Environment::current(), surface), width, height);
96
- }
97
-
98
- void JniSkiaDrawView::surfaceSizeChanged(int width, int height)
99
- {
100
- _drawView->surfaceSizeChanged(width, height);
101
- }
102
-
103
- void JniSkiaDrawView::surfaceDestroyed()
104
- {
105
- _drawView->surfaceDestroyed();
106
- }
107
-
108
- void JniSkiaDrawView::releaseSurface() {
109
- jni::ThreadScope ts;
110
- static auto method = javaPart_->getClass()->getMethod<void(void)>("releaseSurface");
111
- method(javaPart_.get());
112
- }
113
- } // namespace RNSkia
@@ -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