Skip to content

Commit 524072c

Browse files
authored
fix(🤖): remove unnecessary video frame copies on Android (#2476)
1 parent 7d391ae commit 524072c

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

‎example/src/Examples/Video/Video.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Slider from "@react-native-community/slider";
1616
const videoURL =
1717
Platform.OS === "web"
1818
? require("../../Tests/assets/BigBuckBunny.mp4").default
19-
: "https://bit.ly/skia-video-short";
19+
: "https://bit.ly/skia-video";
2020

2121
export const Video = () => {
2222
const paused = useSharedValue(false);

‎package/src/external/reanimated/useVideo.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,29 @@ interface PlaybackOptions {
2222
volume: Animated<number>;
2323
}
2424

25+
const copyFrameOnAndroid = (currentFrame: SharedValue<SkImage | null>) => {
26+
"worklet";
27+
// on android we need to copy the texture before it's invalidated
28+
if (Platform.OS === "android") {
29+
const tex = currentFrame.value;
30+
if (tex) {
31+
console.log("Copying frame on Android");
32+
currentFrame.value = tex.makeNonTextureImage();
33+
tex.dispose();
34+
}
35+
}
36+
};
37+
2538
const setFrame = (video: Video, currentFrame: SharedValue<SkImage | null>) => {
2639
"worklet";
2740
const img = video.nextImage();
2841
if (img) {
2942
if (currentFrame.value) {
3043
currentFrame.value.dispose();
3144
}
32-
if (Platform.OS === "android") {
33-
currentFrame.value = img.makeNonTextureImage();
34-
} else {
35-
currentFrame.value = img;
36-
}
45+
currentFrame.value = img;
46+
} else {
47+
copyFrameOnAndroid(currentFrame);
3748
}
3849
};
3950

@@ -111,6 +122,7 @@ export const useVideo = (
111122
() => seek.value,
112123
(value) => {
113124
if (value !== null) {
125+
copyFrameOnAndroid(currentFrame);
114126
video?.seek(value);
115127
currentTime.value = value;
116128
seek.value = null;

0 commit comments

Comments
 (0)