summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/tests/cts/checkout/src/webgpu/api/validation/gpu_external_texture_expiration.spec.ts
blob: 20ea4897e6881cd790f8bc2b0562fc8b8dd6b023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
export const description = `
GPUExternalTexture expiration mechanism validation tests.
`;

import { makeTestGroup } from '../../../common/framework/test_group.js';
import { assert } from '../../../common/util/util.js';
import {
  getVideoElement,
  startPlayingAndWaitForVideo,
  getVideoFrameFromVideoElement,
  waitForNextFrame,
  waitForNextTask,
} from '../../web_platform/util.js';

import { ValidationTest } from './validation_test.js';

class GPUExternalTextureExpireTest extends ValidationTest {
  submitCommandBuffer(bindGroup: GPUBindGroup, success: boolean): void {
    const kHeight = 16;
    const kWidth = 16;
    const kFormat = 'rgba8unorm';

    const colorAttachment = this.device.createTexture({
      format: kFormat,
      size: { width: kWidth, height: kHeight, depthOrArrayLayers: 1 },
      usage: GPUTextureUsage.COPY_SRC | GPUTextureUsage.RENDER_ATTACHMENT,
    });
    const passDescriptor = {
      colorAttachments: [
        {
          view: colorAttachment.createView(),
          clearValue: [0, 0, 0, 1],
          loadOp: 'clear',
          storeOp: 'store',
        },
      ],
    } as const;

    const commandEncoder = this.device.createCommandEncoder();
    const passEncoder = commandEncoder.beginRenderPass(passDescriptor);
    passEncoder.setBindGroup(0, bindGroup);
    passEncoder.end();
    const commandBuffer = commandEncoder.finish();
    this.expectValidationError(() => this.device.queue.submit([commandBuffer]), !success);
  }

  getDefaultVideoElementAndCheck(): HTMLVideoElement {
    const videoElement = getVideoElement(this, 'four-colors-vp9-bt601.webm');

    if (!('requestVideoFrameCallback' in videoElement)) {
      this.skip('HTMLVideoElement.requestVideoFrameCallback is not supported');
    }

    return videoElement;
  }

  getDefaultBindGroupLayout(): GPUBindGroupLayout {
    return this.device.createBindGroupLayout({
      entries: [{ binding: 0, visibility: GPUShaderStage.FRAGMENT, externalTexture: {} }],
    });
  }
}

export const g = makeTestGroup(GPUExternalTextureExpireTest);

g.test('import_multiple_times_in_same_task_scope')
  .desc(
    `
    Tests that GPUExternalTexture is valid after been imported in the task.
    Tests that in the same task scope, import twice on the same video source may return
    the same GPUExternalTexture and bindGroup doesn't need to be updated.
    `
  )
  .params(u =>
    u //
      .combine('sourceType', ['VideoElement', 'VideoFrame'] as const)
  )
  .fn(async t => {
    const sourceType = t.params.sourceType;
    const videoElement = t.getDefaultVideoElementAndCheck();

    let bindGroup: GPUBindGroup;
    let externalTexture: GPUExternalTexture;
    await startPlayingAndWaitForVideo(videoElement, async () => {
      const source =
        sourceType === 'VideoFrame'
          ? await getVideoFrameFromVideoElement(t, videoElement)
          : videoElement;
      externalTexture = t.device.importExternalTexture({ source });

      bindGroup = t.device.createBindGroup({
        layout: t.getDefaultBindGroupLayout(),
        entries: [{ binding: 0, resource: externalTexture }],
      });

      t.submitCommandBuffer(bindGroup, true);

      // Import again in the same task scope should return same object.
      const mayBeTheSameExternalTexture = t.device.importExternalTexture({ source });

      if (externalTexture === mayBeTheSameExternalTexture) {
        t.submitCommandBuffer(bindGroup, true);
      } else {
        bindGroup = t.device.createBindGroup({
          layout: t.getDefaultBindGroupLayout(),
          entries: [{ binding: 0, resource: externalTexture }],
        });

        t.submitCommandBuffer(bindGroup, true);
      }
    });
  });

g.test('import_and_use_in_different_microtask')
  .desc(
    `
    Tests that in the same task scope, imported GPUExternalTexture is valid in
    different microtasks.
    `
  )
  .params(u =>
    u //
      .combine('sourceType', ['VideoElement', 'VideoFrame'] as const)
  )
  .fn(async t => {
    const sourceType = t.params.sourceType;
    const videoElement = t.getDefaultVideoElementAndCheck();

    let bindGroup: GPUBindGroup;
    let externalTexture: GPUExternalTexture;
    await startPlayingAndWaitForVideo(videoElement, async () => {
      const source =
        sourceType === 'VideoFrame'
          ? await getVideoFrameFromVideoElement(t, videoElement)
          : videoElement;

      // Import GPUExternalTexture
      queueMicrotask(() => {
        externalTexture = t.device.importExternalTexture({ source });
      });

      // Submit GPUExternalTexture
      queueMicrotask(() => {
        bindGroup = t.device.createBindGroup({
          layout: t.getDefaultBindGroupLayout(),
          entries: [{ binding: 0, resource: externalTexture }],
        });
        t.submitCommandBuffer(bindGroup, true);
      });
    });
  });

g.test('import_and_use_in_different_task')
  .desc(
    `
    Tests that in the different task scope, previous imported GPUExternalTexture
    should be expired if it is imported from HTMLVideoElment. GPUExternalTexture
    imported from WebCodec VideoFrame is not expired.
    `
  )
  .params(u =>
    u //
      .combine('sourceType', ['VideoElement', 'VideoFrame'] as const)
  )
  .fn(async t => {
    const sourceType = t.params.sourceType;
    const videoElement = t.getDefaultVideoElementAndCheck();

    let bindGroup: GPUBindGroup;
    let externalTexture: GPUExternalTexture;
    await startPlayingAndWaitForVideo(videoElement, async () => {
      const source =
        sourceType === 'VideoFrame'
          ? await getVideoFrameFromVideoElement(t, videoElement)
          : videoElement;
      externalTexture = t.device.importExternalTexture({ source });

      bindGroup = t.device.createBindGroup({
        layout: t.getDefaultBindGroupLayout(),
        entries: [{ binding: 0, resource: externalTexture }],
      });

      t.submitCommandBuffer(bindGroup, true);
    });

    await waitForNextTask(() => {
      // Enter in another task scope. For GPUExternalTexture imported from WebCodec,
      // it shouldn't be expired because VideoFrame is not 'closed'.
      // For GPUExternalTexutre imported from HTMLVideoElement, it should be expired.
      t.submitCommandBuffer(bindGroup, sourceType === 'VideoFrame' ? true : false);
    });
  });

g.test('use_import_to_refresh')
  .desc(
    `
    Tests that in the different task scope, imported GPUExternalTexture
    again on the same HTMLVideoElement should return active GPUExternalTexture.
    `
  )
  .fn(async t => {
    const videoElement = t.getDefaultVideoElementAndCheck();

    let bindGroup: GPUBindGroup;
    let externalTexture: GPUExternalTexture;
    let source: HTMLVideoElement | VideoFrame;
    await startPlayingAndWaitForVideo(videoElement, () => {
      source = videoElement;
      externalTexture = t.device.importExternalTexture({ source });

      bindGroup = t.device.createBindGroup({
        layout: t.getDefaultBindGroupLayout(),
        entries: [{ binding: 0, resource: externalTexture }],
      });

      t.submitCommandBuffer(bindGroup, true);
    });

    await waitForNextTask(() => {
      const mayBeTheSameExternalTexture = t.device.importExternalTexture({ source });

      if (externalTexture === mayBeTheSameExternalTexture) {
        // ImportExternalTexture should refresh expired GPUExternalTexture.
        t.submitCommandBuffer(bindGroup, true);
      } else {
        bindGroup = t.device.createBindGroup({
          layout: t.getDefaultBindGroupLayout(),
          entries: [{ binding: 0, resource: externalTexture }],
        });
        t.submitCommandBuffer(bindGroup, true);
      }
    });
  });

g.test('webcodec_video_frame_close_expire_immediately')
  .desc(
    `
    Tests that in the same task scope, imported GPUExternalTexture should be expired
    immediately if webcodec VideoFrame.close() is called.
    `
  )
  .fn(async t => {
    const videoElement = t.getDefaultVideoElementAndCheck();

    let bindGroup: GPUBindGroup;
    let externalTexture: GPUExternalTexture;
    await startPlayingAndWaitForVideo(videoElement, async () => {
      const source = await getVideoFrameFromVideoElement(t, videoElement);
      externalTexture = t.device.importExternalTexture({ source });

      bindGroup = t.device.createBindGroup({
        layout: t.getDefaultBindGroupLayout(),
        entries: [{ binding: 0, resource: externalTexture }],
      });

      t.submitCommandBuffer(bindGroup, true);

      source.close();

      t.submitCommandBuffer(bindGroup, false);
    });
  });

g.test('import_from_different_video_frame')
  .desc(
    `
    Tests that imported GPUExternalTexture from different video frame should
    return different GPUExternalTexture objects.
    If the frames are from the same HTMLVideoElement source, GPUExternalTexture
    with old frame should be expired and not been refreshed again.
    `
  )
  .fn(async t => {
    const videoElement = t.getDefaultVideoElementAndCheck();

    let bindGroup: GPUBindGroup;
    let externalTexture: GPUExternalTexture;
    await startPlayingAndWaitForVideo(videoElement, () => {
      externalTexture = t.device.importExternalTexture({
        source: videoElement,
      });

      bindGroup = t.device.createBindGroup({
        layout: t.getDefaultBindGroupLayout(),
        entries: [{ binding: 0, resource: externalTexture }],
      });

      t.submitCommandBuffer(bindGroup, true);
    });

    // Update new video frame.
    await waitForNextFrame(videoElement, () => {
      // Import again for the new video frame.
      const newValidExternalTexture = t.device.importExternalTexture({
        source: videoElement,
      });
      assert(externalTexture !== newValidExternalTexture);

      // VideoFrame is updated. GPUExternalTexture imported from old frame should be expired and
      // cannot be refreshed again.
      // Using the GPUExternalTexture should result in an error.
      t.submitCommandBuffer(bindGroup, false);

      // Update bindGroup with updated GPUExternalTexture should work.
      bindGroup = t.device.createBindGroup({
        layout: t.getDefaultBindGroupLayout(),
        entries: [{ binding: 0, resource: newValidExternalTexture }],
      });
      t.submitCommandBuffer(bindGroup, true);
    });
  });