Repository navigation
Deprecation of SkiaView and <Drawing /> - #2041
Conversation
SkiaView and <Drawing />SkiaView and <Drawing />
SkiaView and <Drawing />SkiaView and <Drawing />
|
Hi @wcandillon first of all thank you for all your work on react-native-skia and other libraries. I've had a look at the The problem is the more nodes you render and the more often you have to update the node structure, the more the calculate intensive it gets. Just by running this example for a minute and dragging your finger around, you can see the fps decreasing to a sub 30 fps: Code exampleimport SkiaDomViewNativeComponent, { NativeProps } from "@shopify/react-native-skia/src/specs/SkiaDomViewNativeComponent";
import { SkiaViewNativeId } from "@shopify/react-native-skia/src/views/SkiaViewNativeId";
import { SkiaViewApi } from "@shopify/react-native-skia/src/views/api";
const Sk = new JsiSkDOM({ Skia }, true);
export default (props: Partial<NativeProps>) => {
const _nativeId = useMemo(() => SkiaViewNativeId.current++, []);
const group = Sk.Group({});
let frames = useSharedValue(0);
const SkiaDomApi = global.SkiaDomApi;
function onDraw(stopped: SharedValue<boolean>, i = 0) {
"worklet";
if (stopped.value) return;
if (i % 10 === 0) {
// benchmark: every 10th frame, restructure the list
const oldRow: RenderNode<GroupNode> = globalThis.row;
if (oldRow) group.removeChild(oldRow);
const rows = SkiaDomApi.GroupNode({});
group.addChild(rows);
for (let i = 0; i < 30; i++) {
const cx = 100 + i * 5;
const cy = 0 + i * 30;
rows.addChild(SkiaDomApi.CircleNode({ color: Skia.Color("blue"), cx, cy, r: 10 }));
rows.addChild(SkiaDomApi.TextNode({ font, text: "Hello", x: cx + 10, y: cy + 10, color: Skia.Color("black") }));
}
globalThis.rows = rows;
}
frames.value++;
requestAnimationFrame(() => {
onDraw(stopped, i + 1);
});
}
useLayoutEffect(() => {
const interval = setInterval(() => {
console.log("FPS", frames.value);
frames.value = 0;
}, 1000);
const stopped = makeMutable(false);
runOnUIImmediately(onDraw)(stopped);
return () => {
clearInterval(interval);
stopped.value = true;
};
}, []);
SkiaViewApi.setJsiProperty(_nativeId, "root", group);
const matrix = useSharedValue(Skia.Matrix().translate(0, 0).get());
const gesture = Gesture.Pan().onChange((e) => {
matrix.value[5] += e.changeY;
matrix.value[2] += e.changeX;
group.setProp("matrix", matrix.value);
SkiaViewApi.requestRedraw(_nativeId);
});
return (
<GestureDetector gesture={gesture}>
<SkiaDomViewNativeComponent collapsable={false} nativeID={`${_nativeId}`} mode={"default"} debug={true} {...props} />
</GestureDetector>
);
};So I had a look at the Picture API and was able to imperatively call paint functions and live update the canvas using continuous mode: I also looked at the Texture/Offscreen API and measured the memory of this approach and it seems like because every frame needs to be captured and saved as SkImage, the memory pressure is quite high with a peak at 1.6GB: Code exampleexport function TestSkia() {
const image = useSharedValue<SkImage | null>(null);
let frames = useSharedValue(0);
const forceUpdate = useForceUpdate();
const surfaceShared = useSharedValue<SkSurface | null>(null);
const canvasShared = useSharedValue<SkCanvas | null>(null);
const paintShared = useSharedValue<SkPaint | null>(null);
const stopped = useSharedValue(false);
function onDraw(image: SharedValue<SkImage | null>, i = 0) {
"worklet";
if (stopped.value) return;
if (!surfaceShared.value) surfaceShared.value = Skia.Surface.MakeOffscreen(200, 200)!;
if (!canvasShared.value) canvasShared.value = surfaceShared.value.getCanvas();
if (!paintShared.value) {
paintShared.value = Skia.Paint();
paintShared.value.setColor(Skia.Color("cyan"));
}
const surface = surfaceShared.value;
const canvas = canvasShared.value;
const paint = paintShared.value;
canvas.clear(Skia.Color("white"));
canvas.drawCircle(100, 100, 100 - i, paint);
i = (i + 1) % 100;
surface.flush();
image.value = surface.makeImageSnapshot();
frames.value++;
requestAnimationFrame(() => onDraw(image, i));
}
useLayoutEffect(() => {
runOnUIImmediately(onDraw)(image);
const interval = setInterval(() => {
console.log("FPS", frames.value);
frames.value = 0;
}, 1000);
return () => {
clearInterval(interval);
stopped.value = true;
console.log("stopped");
};
}, []);
return (
<SafeAreaView style={{ flex: 1 }}>
<Canvas style={{ flex: 1 }} mode="continuous">
<Image image={image} x={0} y={0} width={200} height={200} />
</Canvas>
<TouchableHighlight
style={{ flex: 0, justifyContent: "flex-start", alignItems: "center", paddingBottom: 30, paddingTop: 10 }}
onPress={() => {
let start = performance.now();
while (performance.now() - start < 1000) {}
}}
>
<Text style={{ fontSize: 25, fontWeight: "600" }}>Block</Text>
</TouchableHighlight>
</SafeAreaView>
);
}
What do you suggest to speed up the rendering pipeline? Another unrelated fixed question regarding the fps, which I'll leave here for other people: Update: I fixed the fps issue by setting the preferred fps in the DisplayLink.mm like described here: I was successful when trying to use the Update: I was able to run the onDraw callback on the UI thread via reanimated worklets and the react-native-webgpu package. Here is an example on how to use it. Thank you @wcandillon for wgpu and the example 🙏 |

This PR aims to streamline the usage of the imperative API in React Native Skia by soft deprecating two of its APIs:
SkiaViewand the<Drawing />component.Currently, the imperative API is available in the following ways:
SkiaView<Drawing />componentThe primary reasons for this deprecation are as follows:
SkiaViewand the<Drawing />component are dependent on the main JavaScript context. This dependency introduces complexity in the rendering pipeline. Specifically,SkiaView's reliance on the main JS context prevents us to have a first time to frame.<Drawing />component breaks our scenegraph contract, which ideally should be serializable and executable across various environments.Changes in this PR include:
SkiaView.<Drawing />component.