@shopify/react-native-skia 0.1.151 → 0.1.153
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/CMakeLists.txt +5 -5
- package/android/cpp/jni/JniLoad.cpp +7 -5
- package/android/cpp/jni/JniSkiaManager.cpp +2 -15
- package/android/cpp/jni/include/JniSkiaBaseView.h +68 -0
- package/android/cpp/jni/include/JniSkiaDrawView.h +64 -47
- package/android/cpp/jni/include/JniSkiaManager.h +8 -10
- package/android/cpp/jni/include/JniSkiaPictureView.h +96 -0
- package/android/cpp/rnskia-android/{RNSkPlatformContextImpl.h → RNSkAndroidPlatformContext.h} +4 -4
- package/android/cpp/rnskia-android/RNSkAndroidView.h +100 -0
- package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +91 -0
- package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +39 -0
- package/android/cpp/rnskia-android/SkiaOpenGLRenderer.cpp +15 -6
- package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h +5 -4
- package/android/src/main/java/com/shopify/reactnative/skia/PlatformContext.java +5 -11
- package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaPackage.java +1 -2
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaBaseView.java +143 -0
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaDrawView.java +17 -145
- package/android/src/main/java/com/shopify/reactnative/skia/{RNSkiaViewManager.java → SkiaDrawViewManager.java} +4 -15
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaManager.java +1 -10
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaPictureView.java +45 -0
- package/android/src/main/java/com/shopify/reactnative/skia/SkiaPictureViewManager.java +64 -0
- package/cpp/jsi/JsiValueWrapper.h +11 -0
- package/cpp/rnskia/RNSkInfoParameter.h +4 -14
- package/cpp/rnskia/RNSkJsView.cpp +211 -0
- package/cpp/rnskia/RNSkJsView.h +115 -0
- package/cpp/rnskia/RNSkJsiViewApi.h +6 -6
- package/cpp/rnskia/RNSkManager.cpp +7 -7
- package/cpp/rnskia/RNSkManager.h +7 -7
- package/cpp/rnskia/RNSkPictureView.h +124 -0
- package/cpp/rnskia/RNSkView.h +287 -0
- package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +38 -0
- package/ios/RNSkia-iOS/{RNSkDrawViewImpl.mm → RNSkMetalCanvasProvider.mm} +46 -23
- package/ios/RNSkia-iOS/{PlatformContext.h → RNSkiOSPlatformContext.h} +4 -4
- package/ios/RNSkia-iOS/{PlatformContext.mm → RNSkiOSPlatformContext.mm} +5 -5
- package/ios/RNSkia-iOS/RNSkiOSView.h +34 -0
- package/ios/RNSkia-iOS/SkiaDrawViewManager.mm +19 -11
- package/ios/RNSkia-iOS/SkiaManager.mm +3 -3
- package/ios/RNSkia-iOS/SkiaPictureViewManager.h +8 -0
- package/ios/RNSkia-iOS/SkiaPictureViewManager.mm +51 -0
- package/ios/RNSkia-iOS/SkiaUIView.h +26 -0
- package/ios/RNSkia-iOS/{SkiaDrawView.mm → SkiaUIView.mm} +29 -23
- package/lib/commonjs/views/SkiaBaseWebView.js +213 -0
- package/lib/commonjs/views/SkiaBaseWebView.js.map +1 -0
- package/lib/commonjs/views/SkiaPictureView.js +112 -0
- package/lib/commonjs/views/SkiaPictureView.js.map +1 -0
- package/lib/commonjs/views/SkiaPictureView.web.js +31 -0
- package/lib/commonjs/views/SkiaPictureView.web.js.map +1 -0
- package/lib/commonjs/views/SkiaView.js +1 -1
- package/lib/commonjs/views/SkiaView.js.map +1 -1
- package/lib/commonjs/views/SkiaView.web.js +14 -201
- package/lib/commonjs/views/SkiaView.web.js.map +1 -1
- package/lib/commonjs/views/index.js +13 -0
- package/lib/commonjs/views/index.js.map +1 -1
- package/lib/commonjs/views/types.js.map +1 -1
- package/lib/module/views/SkiaBaseWebView.js +198 -0
- package/lib/module/views/SkiaBaseWebView.js.map +1 -0
- package/lib/module/views/SkiaPictureView.js +94 -0
- package/lib/module/views/SkiaPictureView.js.map +1 -0
- package/lib/module/views/SkiaPictureView.web.js +19 -0
- package/lib/module/views/SkiaPictureView.web.js.map +1 -0
- package/lib/module/views/SkiaView.js +1 -1
- package/lib/module/views/SkiaView.js.map +1 -1
- package/lib/module/views/SkiaView.web.js +15 -199
- package/lib/module/views/SkiaView.web.js.map +1 -1
- package/lib/module/views/index.js +1 -0
- package/lib/module/views/index.js.map +1 -1
- package/lib/module/views/types.js.map +1 -1
- package/lib/typescript/src/views/SkiaBaseWebView.d.ts +58 -0
- package/lib/typescript/src/views/SkiaPictureView.d.ts +27 -0
- package/lib/typescript/src/views/SkiaPictureView.web.d.ts +7 -0
- package/lib/typescript/src/views/SkiaView.d.ts +4 -4
- package/lib/typescript/src/views/SkiaView.web.d.ts +6 -53
- package/lib/typescript/src/views/index.d.ts +1 -0
- package/lib/typescript/src/views/types.d.ts +7 -2
- package/package.json +3 -3
- package/src/views/SkiaBaseWebView.tsx +177 -0
- package/src/views/SkiaPictureView.tsx +93 -0
- package/src/views/SkiaPictureView.web.tsx +23 -0
- package/src/views/SkiaView.tsx +6 -7
- package/src/views/SkiaView.web.tsx +18 -167
- package/src/views/index.ts +1 -0
- package/src/views/types.ts +9 -2
- package/android/cpp/jni/JniSkiaDrawView.cpp +0 -113
- package/android/cpp/rnskia-android/RNSkDrawViewImpl.cpp +0 -73
- package/android/cpp/rnskia-android/RNSkDrawViewImpl.h +0 -48
- package/cpp/rnskia/RNSkDrawView.cpp +0 -315
- package/cpp/rnskia/RNSkDrawView.h +0 -226
- package/ios/RNSkia-iOS/RNSkDrawViewImpl.h +0 -52
- package/ios/RNSkia-iOS/SkiaDrawView.h +0 -23
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
#include <RNSkView.h>
|
|
13
|
+
|
|
14
|
+
#include <RNSkInfoParameter.h>
|
|
15
|
+
#include <RNSkPlatformContext.h>
|
|
16
|
+
#include <RNSkTimingInfo.h>
|
|
17
|
+
#include <RNSkLog.h>
|
|
18
|
+
#include <JsiSkPicture.h>
|
|
19
|
+
|
|
20
|
+
#pragma clang diagnostic push
|
|
21
|
+
#pragma clang diagnostic ignored "-Wdocumentation"
|
|
22
|
+
|
|
23
|
+
#include <SkCanvas.h>
|
|
24
|
+
#include <SkBBHFactory.h>
|
|
25
|
+
#include <SkPictureRecorder.h>
|
|
26
|
+
|
|
27
|
+
#pragma clang diagnostic pop
|
|
28
|
+
|
|
29
|
+
class SkPicture;
|
|
30
|
+
class SkRect;
|
|
31
|
+
class SkImage;
|
|
32
|
+
|
|
33
|
+
namespace RNSkia {
|
|
34
|
+
|
|
35
|
+
using namespace facebook;
|
|
36
|
+
|
|
37
|
+
class RNSkPictureRenderer:
|
|
38
|
+
public RNSkRenderer,
|
|
39
|
+
public std::enable_shared_from_this<RNSkPictureRenderer> {
|
|
40
|
+
public:
|
|
41
|
+
RNSkPictureRenderer(std::function<void()> requestRedraw,
|
|
42
|
+
std::shared_ptr<RNSkPlatformContext> context):
|
|
43
|
+
RNSkRenderer(requestRedraw),
|
|
44
|
+
_platformContext(context) {}
|
|
45
|
+
|
|
46
|
+
bool tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
|
|
47
|
+
performDraw(canvasProvider);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
void renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) override {
|
|
52
|
+
performDraw(canvasProvider);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void setPicture(std::shared_ptr<jsi::HostObject> picture) {
|
|
56
|
+
if(picture == nullptr) {
|
|
57
|
+
_picture = nullptr;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_picture = std::dynamic_pointer_cast<JsiSkPicture>(picture);
|
|
62
|
+
_requestRedraw();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private:
|
|
66
|
+
void performDraw(std::shared_ptr<RNSkCanvasProvider> canvasProvider) {
|
|
67
|
+
if(_picture == nullptr) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
canvasProvider->renderToCanvas([=](SkCanvas* canvas){
|
|
72
|
+
// Make sure to scale correctly
|
|
73
|
+
auto pd = _platformContext->getPixelDensity();
|
|
74
|
+
canvas->save();
|
|
75
|
+
canvas->scale(pd, pd);
|
|
76
|
+
|
|
77
|
+
canvas->drawPicture(_picture->getObject());
|
|
78
|
+
|
|
79
|
+
// Restore and flush canvas
|
|
80
|
+
canvas->restore();
|
|
81
|
+
canvas->flush();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
std::shared_ptr<RNSkPlatformContext> _platformContext;
|
|
86
|
+
std::shared_ptr<JsiSkPicture> _picture;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
class RNSkPictureView: public RNSkView {
|
|
90
|
+
public:
|
|
91
|
+
/**
|
|
92
|
+
* Constructor
|
|
93
|
+
*/
|
|
94
|
+
RNSkPictureView(std::shared_ptr<RNSkPlatformContext> context,
|
|
95
|
+
std::shared_ptr<RNSkCanvasProvider> canvasProvider):
|
|
96
|
+
RNSkView(context,
|
|
97
|
+
canvasProvider,
|
|
98
|
+
std::make_shared<RNSkPictureRenderer>(std::bind(&RNSkPictureView::requestRedraw, this), context)) {}
|
|
99
|
+
|
|
100
|
+
void setJsiProperties(std::unordered_map<std::string, JsiValueWrapper> &props) override {
|
|
101
|
+
for(auto& prop: props) {
|
|
102
|
+
if(prop.first == "picture") {
|
|
103
|
+
if(prop.second.isUndefinedOrNull()) {
|
|
104
|
+
// Clear picture
|
|
105
|
+
std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())->setPicture(nullptr);
|
|
106
|
+
return;
|
|
107
|
+
} else if (prop.second.getType() != JsiWrapperValueType::HostObject) {
|
|
108
|
+
// We expect a function for the picture custom property
|
|
109
|
+
throw std::runtime_error("Expected an object for the picture custom property.");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Save picture
|
|
113
|
+
std::static_pointer_cast<RNSkPictureRenderer>(getRenderer())->setPicture(prop.second.getAsHostObject());
|
|
114
|
+
|
|
115
|
+
// Request redraw
|
|
116
|
+
requestRedraw();
|
|
117
|
+
|
|
118
|
+
} else {
|
|
119
|
+
RNSkView::setJsiProperties(props);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
} // namespace RNSkia
|
|
@@ -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 <
|
|
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,30 @@
|
|
|
7
8
|
#import <SkSurface.h>
|
|
8
9
|
#import <SkCanvas.h>
|
|
9
10
|
|
|
10
|
-
#
|
|
11
|
+
#import <include/gpu/GrDirectContext.h>
|
|
11
12
|
|
|
12
|
-
#
|
|
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>
|
|
17
|
-
id<MTLCommandQueue>
|
|
16
|
+
id<MTLDevice> RNSkMetalCanvasProvider::_device = nullptr;
|
|
17
|
+
id<MTLCommandQueue> RNSkMetalCanvasProvider::_commandQueue = nullptr;
|
|
18
|
+
sk_sp<GrDirectContext> RNSkMetalCanvasProvider::_skContext = nullptr;
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
RNSkMetalCanvasProvider::RNSkMetalCanvasProvider(std::function<void()> requestRedraw,
|
|
21
|
+
std::shared_ptr<RNSkia::RNSkPlatformContext> context):
|
|
22
|
+
RNSkCanvasProvider(requestRedraw),
|
|
23
|
+
_context(context) {
|
|
24
|
+
if (!_device) {
|
|
25
|
+
_device = MTLCreateSystemDefaultDevice();
|
|
26
|
+
}
|
|
27
|
+
if (!_commandQueue) {
|
|
28
|
+
_commandQueue = id<MTLCommandQueue>(CFRetain((GrMTLHandle)[_device newCommandQueue]));
|
|
29
|
+
}
|
|
20
30
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
#pragma clang diagnostic push
|
|
24
|
-
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
|
|
31
|
+
#pragma clang diagnostic push
|
|
32
|
+
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
|
|
25
33
|
_layer = [CAMetalLayer layer];
|
|
26
|
-
#pragma clang diagnostic pop
|
|
34
|
+
#pragma clang diagnostic pop
|
|
27
35
|
|
|
28
36
|
_layer.framebufferOnly = NO;
|
|
29
37
|
_layer.device = _device;
|
|
@@ -32,7 +40,7 @@ RNSkDrawViewImpl::RNSkDrawViewImpl(std::shared_ptr<RNSkia::RNSkPlatformContext>
|
|
|
32
40
|
_layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
|
33
41
|
}
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
RNSkMetalCanvasProvider::~RNSkMetalCanvasProvider() {
|
|
36
44
|
if([[NSThread currentThread] isMainThread]) {
|
|
37
45
|
_layer = NULL;
|
|
38
46
|
} else {
|
|
@@ -50,17 +58,20 @@ RNSkDrawViewImpl::~RNSkDrawViewImpl() {
|
|
|
50
58
|
}
|
|
51
59
|
}
|
|
52
60
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
_layer.drawableSize = CGSizeMake(width * _context->getPixelDensity(),
|
|
58
|
-
height* _context->getPixelDensity());
|
|
59
|
-
|
|
60
|
-
requestRedraw();
|
|
61
|
-
}
|
|
61
|
+
/**
|
|
62
|
+
Returns the scaled width of the view
|
|
63
|
+
*/
|
|
64
|
+
float RNSkMetalCanvasProvider::getScaledWidth() { return _width * _context->getPixelDensity(); };
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
/**
|
|
67
|
+
Returns the scaled height of the view
|
|
68
|
+
*/
|
|
69
|
+
float RNSkMetalCanvasProvider::getScaledHeight() { return _height * _context->getPixelDensity(); };
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
Render to a canvas
|
|
73
|
+
*/
|
|
74
|
+
void RNSkMetalCanvasProvider::renderToCanvas(const std::function<void(SkCanvas*)>& cb) {
|
|
64
75
|
if(_width == -1 && _height == -1) {
|
|
65
76
|
return;
|
|
66
77
|
}
|
|
@@ -109,4 +120,16 @@ void RNSkDrawViewImpl::renderToSkiaCanvas(const std::function<void(SkCanvas*)>&
|
|
|
109
120
|
[commandBuffer presentDrawable:currentDrawable];
|
|
110
121
|
[commandBuffer commit];
|
|
111
122
|
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
void RNSkMetalCanvasProvider::setSize(int width, int height) {
|
|
126
|
+
_width = width;
|
|
127
|
+
_height = height;
|
|
128
|
+
_layer.frame = CGRectMake(0, 0, width, height);
|
|
129
|
+
_layer.drawableSize = CGSizeMake(width * _context->getPixelDensity(),
|
|
130
|
+
height* _context->getPixelDensity());
|
|
131
|
+
|
|
132
|
+
_requestRedraw();
|
|
112
133
|
}
|
|
134
|
+
|
|
135
|
+
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
|
|
33
|
+
class RNSkiOSPlatformContext : public RNSkPlatformContext {
|
|
34
34
|
public:
|
|
35
|
-
|
|
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
|
-
~
|
|
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<
|
|
73
|
+
(static_cast<RNSkiOSPlatformContext*>(observer))->willInvalidateModules();
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
} // namespace RNSkia
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#include "
|
|
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
|
|
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
|
|
31
|
+
void RNSkiOSPlatformContext::raiseError(const std::exception &err) {
|
|
32
32
|
RCTFatal(RCTErrorWithMessage([NSString stringWithUTF8String:err.what()]));
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
void
|
|
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
|
|
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
|
+
};
|