@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.
- package/android/CMakeLists.txt +4 -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} +40 -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 +1 -1
- 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,177 @@
|
|
1
|
+
/* global HTMLCanvasElement */
|
2
|
+
import React from "react";
|
3
|
+
import type { PointerEvent } from "react";
|
4
|
+
import type { LayoutChangeEvent } from "react-native";
|
5
|
+
import { PixelRatio, View } from "react-native";
|
6
|
+
|
7
|
+
import type { SkRect, SkCanvas } from "../skia/types";
|
8
|
+
import type { SkiaValue } from "../values";
|
9
|
+
import { JsiSkSurface } from "../skia/web/JsiSkSurface";
|
10
|
+
|
11
|
+
import type { DrawMode, SkiaBaseViewProps, TouchInfo } from "./types";
|
12
|
+
import { TouchType } from "./types";
|
13
|
+
|
14
|
+
const pd = PixelRatio.get();
|
15
|
+
|
16
|
+
export abstract class SkiaBaseWebView<
|
17
|
+
TProps extends SkiaBaseViewProps
|
18
|
+
> extends React.Component<TProps> {
|
19
|
+
constructor(props: TProps) {
|
20
|
+
super(props);
|
21
|
+
this._mode = props.mode ?? "default";
|
22
|
+
}
|
23
|
+
|
24
|
+
private _surface: JsiSkSurface | null = null;
|
25
|
+
private _unsubscriptions: Array<() => void> = [];
|
26
|
+
private _touches: Array<TouchInfo> = [];
|
27
|
+
private _canvas: SkCanvas | null = null;
|
28
|
+
private _canvasRef = React.createRef<HTMLCanvasElement>();
|
29
|
+
private _mode: DrawMode;
|
30
|
+
private _redrawRequests = 0;
|
31
|
+
private requestId = 0;
|
32
|
+
|
33
|
+
protected width = 0;
|
34
|
+
protected height = 0;
|
35
|
+
|
36
|
+
private unsubscribeAll() {
|
37
|
+
this._unsubscriptions.forEach((u) => u());
|
38
|
+
this._unsubscriptions = [];
|
39
|
+
}
|
40
|
+
|
41
|
+
private onLayout(evt: LayoutChangeEvent) {
|
42
|
+
const { CanvasKit } = global;
|
43
|
+
const { width, height } = evt.nativeEvent.layout;
|
44
|
+
this.width = width;
|
45
|
+
this.height = height;
|
46
|
+
// Reset canvas / surface on layout change
|
47
|
+
if (this._canvasRef.current) {
|
48
|
+
const canvas = this._canvasRef.current;
|
49
|
+
canvas.width = canvas.clientWidth * pd;
|
50
|
+
canvas.height = canvas.clientHeight * pd;
|
51
|
+
const surface = CanvasKit.MakeWebGLCanvasSurface(this._canvasRef.current);
|
52
|
+
if (!surface) {
|
53
|
+
throw new Error("Could not create surface");
|
54
|
+
}
|
55
|
+
this._surface = new JsiSkSurface(CanvasKit, surface);
|
56
|
+
this._canvas = this._surface.getCanvas();
|
57
|
+
this.redraw();
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
componentDidMount() {
|
62
|
+
// Start render loop
|
63
|
+
this.tick();
|
64
|
+
}
|
65
|
+
|
66
|
+
componentDidUpdate() {
|
67
|
+
this.redraw();
|
68
|
+
}
|
69
|
+
|
70
|
+
componentWillUnmount() {
|
71
|
+
this.unsubscribeAll();
|
72
|
+
cancelAnimationFrame(this.requestId);
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Creates a snapshot from the canvas in the surface
|
77
|
+
* @param rect Rect to use as bounds. Optional.
|
78
|
+
* @returns An Image object.
|
79
|
+
*/
|
80
|
+
public makeImageSnapshot(rect?: SkRect) {
|
81
|
+
return this._surface?.makeImageSnapshot(rect);
|
82
|
+
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Override to render
|
86
|
+
*/
|
87
|
+
protected abstract renderInCanvas(
|
88
|
+
canvas: SkCanvas,
|
89
|
+
touches: TouchInfo[]
|
90
|
+
): void;
|
91
|
+
|
92
|
+
/**
|
93
|
+
* Sends a redraw request to the native SkiaView.
|
94
|
+
*/
|
95
|
+
private tick() {
|
96
|
+
if (this._mode === "continuous" || this._redrawRequests > 0) {
|
97
|
+
this._redrawRequests = 0;
|
98
|
+
if (this._canvas) {
|
99
|
+
const touches = [...this._touches];
|
100
|
+
this._touches = [];
|
101
|
+
this.renderInCanvas(this._canvas!, touches);
|
102
|
+
this._surface?.ref.flush();
|
103
|
+
}
|
104
|
+
}
|
105
|
+
this.requestId = requestAnimationFrame(this.tick.bind(this));
|
106
|
+
}
|
107
|
+
|
108
|
+
public redraw() {
|
109
|
+
this._redrawRequests++;
|
110
|
+
}
|
111
|
+
|
112
|
+
/**
|
113
|
+
* Updates the drawing mode for the skia view. This is the same
|
114
|
+
* as declaratively setting the mode property on the SkiaView.
|
115
|
+
* There are two drawing modes, "continuous" and "default",
|
116
|
+
* where the continuous mode will continuously redraw the view and
|
117
|
+
* the default mode will only redraw when any of the regular react
|
118
|
+
* properties are changed like size and margins.
|
119
|
+
* @param mode Drawing mode to use.
|
120
|
+
*/
|
121
|
+
public setDrawMode(mode: DrawMode) {
|
122
|
+
this._mode = mode;
|
123
|
+
this.tick();
|
124
|
+
}
|
125
|
+
|
126
|
+
/**
|
127
|
+
* Registers one or move values as a dependant value of the Skia View. The view will
|
128
|
+
* The view will redraw itself when any of the values change.
|
129
|
+
* @param values Values to register
|
130
|
+
*/
|
131
|
+
public registerValues(_values: SkiaValue<unknown>[]) {
|
132
|
+
// Unsubscribe from dependency values
|
133
|
+
this.unsubscribeAll();
|
134
|
+
// Register redraw dependencies on values
|
135
|
+
_values.forEach((v) => {
|
136
|
+
this._unsubscriptions.push(
|
137
|
+
v.addListener(() => {
|
138
|
+
this.redraw();
|
139
|
+
})
|
140
|
+
);
|
141
|
+
});
|
142
|
+
}
|
143
|
+
|
144
|
+
private handleTouchEvent(evt: PointerEvent, touchType: TouchType) {
|
145
|
+
this._touches.push({
|
146
|
+
id: evt.pointerId,
|
147
|
+
x: evt.clientX - evt.currentTarget.getClientRects()[0].left,
|
148
|
+
y: evt.clientY - evt.currentTarget.getClientRects()[0].top,
|
149
|
+
force: evt.pressure,
|
150
|
+
type: touchType,
|
151
|
+
timestamp: Date.now(),
|
152
|
+
});
|
153
|
+
this.redraw();
|
154
|
+
}
|
155
|
+
|
156
|
+
createTouchHandler(touchType: TouchType) {
|
157
|
+
return (evt: PointerEvent) => this.handleTouchEvent(evt, touchType);
|
158
|
+
}
|
159
|
+
|
160
|
+
render() {
|
161
|
+
const { mode, debug = false, ...viewProps } = this.props;
|
162
|
+
return (
|
163
|
+
<View {...viewProps} onLayout={this.onLayout.bind(this)}>
|
164
|
+
<canvas
|
165
|
+
ref={this._canvasRef}
|
166
|
+
style={{ display: "flex", flex: 1 }}
|
167
|
+
onPointerDown={this.createTouchHandler(TouchType.Start)}
|
168
|
+
onPointerMove={this.createTouchHandler(TouchType.Active)}
|
169
|
+
onPointerUp={this.createTouchHandler(TouchType.End)}
|
170
|
+
onPointerCancel={this.createTouchHandler(TouchType.Cancelled)}
|
171
|
+
onPointerLeave={this.createTouchHandler(TouchType.End)}
|
172
|
+
onPointerOut={this.createTouchHandler(TouchType.End)}
|
173
|
+
/>
|
174
|
+
</View>
|
175
|
+
);
|
176
|
+
}
|
177
|
+
}
|
@@ -0,0 +1,93 @@
|
|
1
|
+
import React from "react";
|
2
|
+
import { requireNativeComponent } from "react-native";
|
3
|
+
|
4
|
+
import type { SkRect } from "../skia/types";
|
5
|
+
import type { SkiaValue } from "../values";
|
6
|
+
|
7
|
+
import { SkiaViewApi } from "./api";
|
8
|
+
import type { NativeSkiaViewProps, SkiaPictureViewProps } from "./types";
|
9
|
+
|
10
|
+
let SkiaViewNativeId = 1000;
|
11
|
+
|
12
|
+
const NativeSkiaPictureView =
|
13
|
+
requireNativeComponent<NativeSkiaViewProps>("SkiaPictureView");
|
14
|
+
|
15
|
+
export class SkiaPictureView extends React.Component<SkiaPictureViewProps> {
|
16
|
+
constructor(props: SkiaPictureViewProps) {
|
17
|
+
super(props);
|
18
|
+
this._nativeId = SkiaViewNativeId++;
|
19
|
+
const { picture } = props;
|
20
|
+
if (picture) {
|
21
|
+
assertSkiaViewApi();
|
22
|
+
SkiaViewApi.setJsiProperty(this._nativeId, "picture", picture);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
private _nativeId: number;
|
27
|
+
|
28
|
+
public get nativeId() {
|
29
|
+
return this._nativeId;
|
30
|
+
}
|
31
|
+
|
32
|
+
componentDidUpdate(prevProps: SkiaPictureViewProps) {
|
33
|
+
const { picture } = this.props;
|
34
|
+
if (picture !== prevProps.picture) {
|
35
|
+
assertSkiaViewApi();
|
36
|
+
SkiaViewApi.setJsiProperty(this._nativeId, "picture", picture);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
/**
|
41
|
+
* Creates a snapshot from the canvas in the surface
|
42
|
+
* @param rect Rect to use as bounds. Optional.
|
43
|
+
* @returns An Image object.
|
44
|
+
*/
|
45
|
+
public makeImageSnapshot(rect?: SkRect) {
|
46
|
+
assertSkiaViewApi();
|
47
|
+
return SkiaViewApi.makeImageSnapshot(this._nativeId, rect);
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Sends a redraw request to the native SkiaView.
|
52
|
+
*/
|
53
|
+
public redraw() {
|
54
|
+
assertSkiaViewApi();
|
55
|
+
SkiaViewApi.requestRedraw(this._nativeId);
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
* Registers one or move values as a dependant value of the Skia View. The view will
|
60
|
+
* The view will redraw itself when any of the values change.
|
61
|
+
* @param values Values to register
|
62
|
+
*/
|
63
|
+
public registerValues(values: SkiaValue<unknown>[]): () => void {
|
64
|
+
assertSkiaViewApi();
|
65
|
+
return SkiaViewApi.registerValuesInView(this._nativeId, values);
|
66
|
+
}
|
67
|
+
|
68
|
+
render() {
|
69
|
+
const { mode, debug = false, ...viewProps } = this.props;
|
70
|
+
return (
|
71
|
+
<NativeSkiaPictureView
|
72
|
+
collapsable={false}
|
73
|
+
nativeID={`${this._nativeId}`}
|
74
|
+
mode={mode}
|
75
|
+
debug={debug}
|
76
|
+
{...viewProps}
|
77
|
+
/>
|
78
|
+
);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
const assertSkiaViewApi = () => {
|
83
|
+
if (
|
84
|
+
SkiaViewApi === null ||
|
85
|
+
SkiaViewApi.setJsiProperty === null ||
|
86
|
+
SkiaViewApi.callJsiMethod === null ||
|
87
|
+
SkiaViewApi.registerValuesInView === null ||
|
88
|
+
SkiaViewApi.requestRedraw === null ||
|
89
|
+
SkiaViewApi.makeImageSnapshot === null
|
90
|
+
) {
|
91
|
+
throw Error("Skia View Api was not found.");
|
92
|
+
}
|
93
|
+
};
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { PixelRatio } from "react-native";
|
2
|
+
|
3
|
+
import type { SkCanvas } from "../skia/types";
|
4
|
+
|
5
|
+
import type { SkiaPictureViewProps } from "./types";
|
6
|
+
import { SkiaBaseWebView } from "./SkiaBaseWebView";
|
7
|
+
|
8
|
+
const pd = PixelRatio.get();
|
9
|
+
|
10
|
+
export class SkiaPictureView extends SkiaBaseWebView<SkiaPictureViewProps> {
|
11
|
+
constructor(props: SkiaPictureViewProps) {
|
12
|
+
super(props);
|
13
|
+
}
|
14
|
+
|
15
|
+
protected renderInCanvas(canvas: SkCanvas): void {
|
16
|
+
if (this.props.picture) {
|
17
|
+
canvas.save();
|
18
|
+
canvas.scale(pd, pd);
|
19
|
+
canvas.drawPicture(this.props.picture);
|
20
|
+
canvas.restore();
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
package/src/views/SkiaView.tsx
CHANGED
@@ -5,16 +5,15 @@ import type { SkRect } from "../skia/types";
|
|
5
5
|
import type { SkiaValue } from "../values";
|
6
6
|
|
7
7
|
import { SkiaViewApi } from "./api";
|
8
|
-
import type { NativeSkiaViewProps,
|
8
|
+
import type { NativeSkiaViewProps, SkiaDrawViewProps } from "./types";
|
9
9
|
|
10
10
|
let SkiaViewNativeId = 1000;
|
11
11
|
|
12
|
-
const NativeSkiaView =
|
13
|
-
"
|
14
|
-
);
|
12
|
+
const NativeSkiaView =
|
13
|
+
requireNativeComponent<NativeSkiaViewProps>("SkiaDrawView");
|
15
14
|
|
16
|
-
export class SkiaView extends React.Component<
|
17
|
-
constructor(props:
|
15
|
+
export class SkiaView extends React.Component<SkiaDrawViewProps> {
|
16
|
+
constructor(props: SkiaDrawViewProps) {
|
18
17
|
super(props);
|
19
18
|
this._nativeId = SkiaViewNativeId++;
|
20
19
|
const { onDraw } = props;
|
@@ -30,7 +29,7 @@ export class SkiaView extends React.Component<SkiaViewProps> {
|
|
30
29
|
return this._nativeId;
|
31
30
|
}
|
32
31
|
|
33
|
-
componentDidUpdate(prevProps:
|
32
|
+
componentDidUpdate(prevProps: SkiaDrawViewProps) {
|
34
33
|
const { onDraw } = this.props;
|
35
34
|
if (onDraw !== prevProps.onDraw) {
|
36
35
|
assertSkiaViewApi();
|
@@ -1,178 +1,29 @@
|
|
1
|
-
|
2
|
-
import React from "react";
|
3
|
-
import type { PointerEvent } from "react";
|
4
|
-
import type { LayoutChangeEvent } from "react-native";
|
5
|
-
import { PixelRatio, View } from "react-native";
|
1
|
+
import { PixelRatio } from "react-native";
|
6
2
|
|
7
|
-
import type {
|
8
|
-
import type { SkiaValue } from "../values";
|
9
|
-
import { JsiSkSurface } from "../skia/web/JsiSkSurface";
|
3
|
+
import type { SkCanvas } from "../skia/types";
|
10
4
|
|
11
|
-
import type { DrawingInfo,
|
12
|
-
import {
|
5
|
+
import type { DrawingInfo, SkiaDrawViewProps, TouchInfo } from "./types";
|
6
|
+
import { SkiaBaseWebView } from "./SkiaBaseWebView";
|
13
7
|
|
14
8
|
const pd = PixelRatio.get();
|
15
9
|
|
16
|
-
export class SkiaView extends
|
17
|
-
constructor(props:
|
10
|
+
export class SkiaView extends SkiaBaseWebView<SkiaDrawViewProps> {
|
11
|
+
constructor(props: SkiaDrawViewProps) {
|
18
12
|
super(props);
|
19
|
-
this._mode = props.mode ?? "default";
|
20
13
|
}
|
21
14
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
this._unsubscriptions.forEach((u) => u());
|
35
|
-
this._unsubscriptions = [];
|
36
|
-
}
|
37
|
-
|
38
|
-
private onLayout(evt: LayoutChangeEvent) {
|
39
|
-
const { CanvasKit } = global;
|
40
|
-
const { width, height } = evt.nativeEvent.layout;
|
41
|
-
this.width = width;
|
42
|
-
this.height = height;
|
43
|
-
// Reset canvas / surface on layout change
|
44
|
-
if (this._canvasRef.current) {
|
45
|
-
const canvas = this._canvasRef.current;
|
46
|
-
canvas.width = canvas.clientWidth * pd;
|
47
|
-
canvas.height = canvas.clientHeight * pd;
|
48
|
-
const surface = CanvasKit.MakeWebGLCanvasSurface(this._canvasRef.current);
|
49
|
-
if (!surface) {
|
50
|
-
throw new Error("Could not create surface");
|
51
|
-
}
|
52
|
-
this._surface = new JsiSkSurface(CanvasKit, surface);
|
53
|
-
this._canvas = this._surface.getCanvas();
|
54
|
-
this.redraw();
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
componentDidMount() {
|
59
|
-
// Start render loop
|
60
|
-
this.tick();
|
61
|
-
}
|
62
|
-
|
63
|
-
componentDidUpdate() {
|
64
|
-
this.redraw();
|
65
|
-
}
|
66
|
-
|
67
|
-
componentWillUnmount() {
|
68
|
-
this.unsubscribeAll();
|
69
|
-
cancelAnimationFrame(this.requestId);
|
70
|
-
}
|
71
|
-
|
72
|
-
/**
|
73
|
-
* Creates a snapshot from the canvas in the surface
|
74
|
-
* @param rect Rect to use as bounds. Optional.
|
75
|
-
* @returns An Image object.
|
76
|
-
*/
|
77
|
-
public makeImageSnapshot(rect?: SkRect) {
|
78
|
-
return this._surface?.makeImageSnapshot(rect);
|
79
|
-
}
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Sends a redraw request to the native SkiaView.
|
83
|
-
*/
|
84
|
-
private tick() {
|
85
|
-
if (this._mode === "continuous" || this._redrawRequests > 0) {
|
86
|
-
this._redrawRequests = 0;
|
87
|
-
if (this._canvas && this.props.onDraw) {
|
88
|
-
const touches = [...this._touches];
|
89
|
-
this._touches = [];
|
90
|
-
const info: DrawingInfo = {
|
91
|
-
height: this.height,
|
92
|
-
width: this.width,
|
93
|
-
timestamp: Date.now(),
|
94
|
-
touches: touches.map((t) => [t]),
|
95
|
-
};
|
96
|
-
if (this.props.onDraw) {
|
97
|
-
const canvas = this._canvas!;
|
98
|
-
canvas.save();
|
99
|
-
canvas.scale(pd, pd);
|
100
|
-
this.props.onDraw(canvas, info);
|
101
|
-
canvas.restore();
|
102
|
-
}
|
103
|
-
this._surface?.ref.flush();
|
104
|
-
}
|
15
|
+
protected renderInCanvas(canvas: SkCanvas, touches: TouchInfo[]): void {
|
16
|
+
if (this.props.onDraw) {
|
17
|
+
const info: DrawingInfo = {
|
18
|
+
height: this.height,
|
19
|
+
width: this.width,
|
20
|
+
timestamp: Date.now(),
|
21
|
+
touches: touches.map((t) => [t]),
|
22
|
+
};
|
23
|
+
canvas.save();
|
24
|
+
canvas.scale(pd, pd);
|
25
|
+
this.props.onDraw(canvas, info);
|
26
|
+
canvas.restore();
|
105
27
|
}
|
106
|
-
this.requestId = requestAnimationFrame(this.tick.bind(this));
|
107
|
-
}
|
108
|
-
|
109
|
-
public redraw() {
|
110
|
-
this._redrawRequests++;
|
111
|
-
}
|
112
|
-
|
113
|
-
/**
|
114
|
-
* Updates the drawing mode for the skia view. This is the same
|
115
|
-
* as declaratively setting the mode property on the SkiaView.
|
116
|
-
* There are two drawing modes, "continuous" and "default",
|
117
|
-
* where the continuous mode will continuously redraw the view and
|
118
|
-
* the default mode will only redraw when any of the regular react
|
119
|
-
* properties are changed like size and margins.
|
120
|
-
* @param mode Drawing mode to use.
|
121
|
-
*/
|
122
|
-
public setDrawMode(mode: DrawMode) {
|
123
|
-
this._mode = mode;
|
124
|
-
this.tick();
|
125
|
-
}
|
126
|
-
|
127
|
-
/**
|
128
|
-
* Registers one or move values as a dependant value of the Skia View. The view will
|
129
|
-
* The view will redraw itself when any of the values change.
|
130
|
-
* @param values Values to register
|
131
|
-
*/
|
132
|
-
public registerValues(_values: SkiaValue<unknown>[]) {
|
133
|
-
// Unsubscribe from dependency values
|
134
|
-
this.unsubscribeAll();
|
135
|
-
// Register redraw dependencies on values
|
136
|
-
_values.forEach((v) => {
|
137
|
-
this._unsubscriptions.push(
|
138
|
-
v.addListener(() => {
|
139
|
-
this.redraw();
|
140
|
-
})
|
141
|
-
);
|
142
|
-
});
|
143
|
-
}
|
144
|
-
|
145
|
-
private handleTouchEvent(evt: PointerEvent, touchType: TouchType) {
|
146
|
-
this._touches.push({
|
147
|
-
id: evt.pointerId,
|
148
|
-
x: evt.clientX - evt.currentTarget.getClientRects()[0].left,
|
149
|
-
y: evt.clientY - evt.currentTarget.getClientRects()[0].top,
|
150
|
-
force: evt.pressure,
|
151
|
-
type: touchType,
|
152
|
-
timestamp: Date.now(),
|
153
|
-
});
|
154
|
-
this.redraw();
|
155
|
-
}
|
156
|
-
|
157
|
-
createTouchHandler(touchType: TouchType) {
|
158
|
-
return (evt: PointerEvent) => this.handleTouchEvent(evt, touchType);
|
159
|
-
}
|
160
|
-
|
161
|
-
render() {
|
162
|
-
const { mode, debug = false, ...viewProps } = this.props;
|
163
|
-
return (
|
164
|
-
<View {...viewProps} onLayout={this.onLayout.bind(this)}>
|
165
|
-
<canvas
|
166
|
-
ref={this._canvasRef}
|
167
|
-
style={{ display: "flex", flex: 1 }}
|
168
|
-
onPointerDown={this.createTouchHandler(TouchType.Start)}
|
169
|
-
onPointerMove={this.createTouchHandler(TouchType.Active)}
|
170
|
-
onPointerUp={this.createTouchHandler(TouchType.End)}
|
171
|
-
onPointerCancel={this.createTouchHandler(TouchType.Cancelled)}
|
172
|
-
onPointerLeave={this.createTouchHandler(TouchType.End)}
|
173
|
-
onPointerOut={this.createTouchHandler(TouchType.End)}
|
174
|
-
/>
|
175
|
-
</View>
|
176
|
-
);
|
177
28
|
}
|
178
29
|
}
|
package/src/views/index.ts
CHANGED
package/src/views/types.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { ViewProps } from "react-native";
|
2
2
|
|
3
|
-
import type { SkCanvas, SkImage, SkRect } from "../skia/types";
|
3
|
+
import type { SkCanvas, SkImage, SkPicture, SkRect } from "../skia/types";
|
4
4
|
import type { SkiaValue } from "../values";
|
5
5
|
|
6
6
|
export type DrawMode = "continuous" | "default";
|
@@ -72,7 +72,7 @@ export interface ISkiaViewApi {
|
|
72
72
|
makeImageSnapshot: (nativeId: number, rect?: SkRect) => SkImage;
|
73
73
|
}
|
74
74
|
|
75
|
-
export interface
|
75
|
+
export interface SkiaBaseViewProps extends ViewProps {
|
76
76
|
/**
|
77
77
|
* Sets the drawing mode for the skia view. There are two drawing
|
78
78
|
* modes, "continuous" and "default", where the continuous mode will
|
@@ -86,6 +86,9 @@ export interface SkiaViewProps extends ViewProps {
|
|
86
86
|
* average time it takes to render.
|
87
87
|
*/
|
88
88
|
debug?: boolean;
|
89
|
+
}
|
90
|
+
|
91
|
+
export interface SkiaDrawViewProps extends SkiaBaseViewProps {
|
89
92
|
/**
|
90
93
|
* Draw callback. Will be called whenever the view is invalidated and
|
91
94
|
* needs to redraw. This is either caused by a change in a react
|
@@ -95,3 +98,7 @@ export interface SkiaViewProps extends ViewProps {
|
|
95
98
|
*/
|
96
99
|
onDraw?: RNSkiaDrawCallback;
|
97
100
|
}
|
101
|
+
|
102
|
+
export interface SkiaPictureViewProps extends SkiaBaseViewProps {
|
103
|
+
picture?: SkPicture;
|
104
|
+
}
|
@@ -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
|