Skip to content

Commit

Permalink
Consistently clean up VideoFrame objects (#3244)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x authored Jan 6, 2024
1 parent 61e30b5 commit 7fc4a3e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 36 deletions.
35 changes: 7 additions & 28 deletions src/webgpu/api/validation/gpu_external_texture_expiration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ g.test('import_multiple_times_in_same_task_scope')
sourceType === 'VideoFrame'
? await getVideoFrameFromVideoElement(t, videoElement)
: videoElement;
externalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
externalTexture = t.device.importExternalTexture({ source });

bindGroup = t.device.createBindGroup({
layout: t.getDefaultBindGroupLayout(),
Expand All @@ -99,10 +96,7 @@ g.test('import_multiple_times_in_same_task_scope')
t.submitCommandBuffer(bindGroup, true);

// Import again in the same task scope should return same object.
const mayBeTheSameExternalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
const mayBeTheSameExternalTexture = t.device.importExternalTexture({ source });

if (externalTexture === mayBeTheSameExternalTexture) {
t.submitCommandBuffer(bindGroup, true);
Expand Down Expand Up @@ -142,10 +136,7 @@ g.test('import_and_use_in_different_microtask')

// Import GPUExternalTexture
queueMicrotask(() => {
externalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
externalTexture = t.device.importExternalTexture({ source });
});

// Submit GPUExternalTexture
Expand Down Expand Up @@ -182,10 +173,7 @@ g.test('import_and_use_in_different_task')
sourceType === 'VideoFrame'
? await getVideoFrameFromVideoElement(t, videoElement)
: videoElement;
externalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
externalTexture = t.device.importExternalTexture({ source });

bindGroup = t.device.createBindGroup({
layout: t.getDefaultBindGroupLayout(),
Expand Down Expand Up @@ -218,10 +206,7 @@ g.test('use_import_to_refresh')
let source: HTMLVideoElement | VideoFrame;
await startPlayingAndWaitForVideo(videoElement, () => {
source = videoElement;
externalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
externalTexture = t.device.importExternalTexture({ source });

bindGroup = t.device.createBindGroup({
layout: t.getDefaultBindGroupLayout(),
Expand All @@ -232,10 +217,7 @@ g.test('use_import_to_refresh')
});

await waitForNextTask(() => {
const mayBeTheSameExternalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
const mayBeTheSameExternalTexture = t.device.importExternalTexture({ source });

if (externalTexture === mayBeTheSameExternalTexture) {
// ImportExternalTexture should refresh expired GPUExternalTexture.
Expand Down Expand Up @@ -264,10 +246,7 @@ g.test('webcodec_video_frame_close_expire_immediately')
let externalTexture: GPUExternalTexture;
await startPlayingAndWaitForVideo(videoElement, async () => {
const source = await getVideoFrameFromVideoElement(t, videoElement);
externalTexture = t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
});
externalTexture = t.device.importExternalTexture({ source });

bindGroup = t.device.createBindGroup({
layout: t.getDefaultBindGroupLayout(),
Expand Down
5 changes: 1 addition & 4 deletions src/webgpu/api/validation/state/device_lost/destroy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,7 @@ Tests import external texture on destroyed device. Tests valid combinations of:
entries: [
{
binding: 0,
resource: t.device.importExternalTexture({
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
source: source as any,
}),
resource: t.device.importExternalTexture({ source }),
},
],
});
Expand Down
4 changes: 0 additions & 4 deletions src/webgpu/web_platform/external_texture/video.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ for several combinations of video format, video color spaces and dst color space
exp: convertToUnorm8(expect.bottomRightColor),
},
]);

if (sourceType === 'VideoFrame') (source as VideoFrame).close();
});
});

Expand Down Expand Up @@ -437,7 +435,5 @@ compute shader, for several combinations of video format, video color spaces and
// Bottom-right.
{ coord: { x: 1, y: 1 }, exp: convertToUnorm8(expect.bottomRightColor) },
]);

if (sourceType === 'VideoFrame') (source as VideoFrame).close();
});
});
1 change: 1 addition & 0 deletions src/webgpu/web_platform/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export async function getVideoFrameFromVideoElement(
const transformer: TransformStream = new TransformStream({
transform(videoFrame, _controller) {
videoTrack.stop();
test.trackForCleanup(videoFrame);
resolve(videoFrame);
},
flush(controller) {
Expand Down

0 comments on commit 7fc4a3e

Please sign in to comment.