Skip to content

Commit

Permalink
Update useImageAsTexture implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon committed Sep 17, 2024
1 parent ac1172f commit b0031dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
27 changes: 6 additions & 21 deletions packages/skia/src/external/reanimated/textures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,12 @@ export const usePictureAsTexture = (
};

export const useImageAsTexture = (source: DataSourceParam) => {
const texture = Rea.useSharedValue<SkImage | null>(null);
const image = useImage(source);
const size = useMemo(() => {
if (image) {
return { width: image.width(), height: image.height() };
}
return { width: 0, height: 0 };
}, [image]);
const picture = useMemo(() => {
if (image) {
const recorder = Skia.PictureRecorder();
const canvas = recorder.beginRecording({
x: 0,
y: 0,
width: size.width,
height: size.height,
});
canvas.drawImage(image, 0, 0);
return recorder.finishRecordingAsPicture();
} else {
return null;
useEffect(() => {
if (image !== null) {
texture.value = Skia.Image.MakeTextureFromImage(image);
}
}, [size, image]);
return usePictureAsTexture(picture, size);
});
return texture;
};
6 changes: 5 additions & 1 deletion packages/skia/src/renderer/__tests__/e2e/Offscreen.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ describe("Offscreen Drawings", () => {
if (!r2) {
return [];
}
return [r0.isTextureBacked(), r1.isTextureBacked(), r2.isTextureBacked()];
return [
r0.isTextureBacked(),
r1.isTextureBacked(),
r2.isTextureBacked(),
];
},
{ width, height, CI }
);
Expand Down

0 comments on commit b0031dc

Please sign in to comment.