Skip to content

Commit 0ccc35a

Browse files
authored
fix(🌄): implement ImageBitmap.close (#354)
1 parent 0ebc301 commit 0ccc35a

4 files changed

Lines changed: 68 additions & 5 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "react-native-webgpu",
2+
"name": "react-native-webgpu-monorepo",
33
"private": true,
44
"workspaces": [
55
"packages/*",

‎packages/webgpu/cpp/rnwgpu/api/ImageBitmap.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ class ImageBitmap : public NativeObject<ImageBitmap> {
2626

2727
size_t getSize() { return _imageData.data.size(); }
2828

29+
void close() {
30+
_imageData.data.clear();
31+
_imageData.data.shrink_to_fit();
32+
_imageData.width = 0;
33+
_imageData.height = 0;
34+
}
35+
2936
static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
3037
installGetter(runtime, prototype, "width", &ImageBitmap::getWidth);
3138
installGetter(runtime, prototype, "height", &ImageBitmap::getHeight);
39+
installMethod(runtime, prototype, "close", &ImageBitmap::close);
3240
}
3341

3442
size_t getMemoryPressure() override { return getSize(); }

‎packages/webgpu/src/__tests__/ImageData.spec.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,61 @@ describe("Image Bitmap", () => {
6666
expect(result.width).toBe(expected.width);
6767
expect(result.height).toBe(expected.height);
6868
});
69+
itSkipsOnWeb("ImageBitmap.close() zeroes width and height", async () => {
70+
const pngBytes = Array.from(
71+
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
72+
);
73+
const expected = decodeImage(path.join(__dirname, "./assets/Di-3d.png"));
74+
const result = await client.eval(
75+
({ pngData }) => {
76+
const bytes = new Uint8Array(pngData);
77+
return createImageBitmap(bytes.buffer).then((bmp) => {
78+
const before = { width: bmp.width, height: bmp.height };
79+
bmp.close();
80+
const after = { width: bmp.width, height: bmp.height };
81+
return { before, after };
82+
});
83+
},
84+
{ pngData: pngBytes },
85+
);
86+
expect(result.before.width).toBe(expected.width);
87+
expect(result.before.height).toBe(expected.height);
88+
expect(result.after.width).toBe(0);
89+
expect(result.after.height).toBe(0);
90+
});
91+
itSkipsOnWeb("ImageBitmap.close() is idempotent", async () => {
92+
const pngBytes = Array.from(
93+
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
94+
);
95+
const result = await client.eval(
96+
({ pngData }) => {
97+
const bytes = new Uint8Array(pngData);
98+
return createImageBitmap(bytes.buffer).then((bmp) => {
99+
bmp.close();
100+
bmp.close();
101+
return { width: bmp.width, height: bmp.height };
102+
});
103+
},
104+
{ pngData: pngBytes },
105+
);
106+
expect(result.width).toBe(0);
107+
expect(result.height).toBe(0);
108+
});
109+
itSkipsOnWeb("ImageBitmap exposes close as a function", async () => {
110+
const pngBytes = Array.from(
111+
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
112+
);
113+
const result = await client.eval(
114+
({ pngData }) => {
115+
const bytes = new Uint8Array(pngData);
116+
return createImageBitmap(bytes.buffer).then((bmp) => {
117+
return typeof bmp.close;
118+
});
119+
},
120+
{ pngData: pngBytes },
121+
);
122+
expect(result).toBe("function");
123+
});
69124
itSkipsOnWeb(
70125
"createImageBitmap from Uint8Array subarray (byteOffset/byteLength)",
71126
async () => {

‎yarn.lock‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,9 +3724,9 @@ __metadata:
37243724
languageName: node
37253725
linkType: hard
37263726

3727-
"@react-three/fiber@patch:@react-three/fiber@npm%3A9.4.0#./.yarn/patches/@react-three-fiber-npm-9.4.0-77fe8ee9bd.patch::locator=react-native-webgpu%40workspace%3A.":
3727+
"@react-three/fiber@patch:@react-three/fiber@npm%3A9.4.0#./.yarn/patches/@react-three-fiber-npm-9.4.0-77fe8ee9bd.patch::locator=react-native-webgpu-monorepo%40workspace%3A.":
37283728
version: 9.4.0
3729-
resolution: "@react-three/fiber@patch:@react-three/fiber@npm%3A9.4.0#./.yarn/patches/@react-three-fiber-npm-9.4.0-77fe8ee9bd.patch::version=9.4.0&hash=7de131&locator=react-native-webgpu%40workspace%3A."
3729+
resolution: "@react-three/fiber@patch:@react-three/fiber@npm%3A9.4.0#./.yarn/patches/@react-three-fiber-npm-9.4.0-77fe8ee9bd.patch::version=9.4.0&hash=7de131&locator=react-native-webgpu-monorepo%40workspace%3A."
37303730
dependencies:
37313731
"@babel/runtime": ^7.17.8
37323732
"@types/react-reconciler": ^0.32.0
@@ -13039,9 +13039,9 @@ __metadata:
1303913039
languageName: node
1304013040
linkType: hard
1304113041

13042-
"react-native-webgpu@workspace:.":
13042+
"react-native-webgpu-monorepo@workspace:.":
1304313043
version: 0.0.0-use.local
13044-
resolution: "react-native-webgpu@workspace:."
13044+
resolution: "react-native-webgpu-monorepo@workspace:."
1304513045
dependencies:
1304613046
"@types/react-native-web": ^0.19.2
1304713047
turbo: ^2.1.0

0 commit comments

Comments
 (0)