redjs-webgpu 0.8.0

Native WebGPU for redjs over wgpu. A crate of its own so that redjs and wgpu compile in parallel.
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Immutable video-frame storage shared by WebGPU imports and queue copies.
use super::*;
use redjs::webapi::image::external_pixels;
use std::sync::Weak;
pub use wgpu;

/// A decoder snapshot, independently retained across playback, seek and source changes.
/// Return a single-sample BGRA/RGBA unorm texture with TEXTURE_BINDING usage.
pub trait NativeFrame: Send + Sync {
    fn texture(&self, context: &backend::Context) -> Result<wgpu::Texture, String>;
}

#[derive(Clone)]
enum Storage {
    Pixels(Arc<[u8]>),
    Native(Arc<dyn NativeFrame>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PixelFormat {
    Rgba,
    Rgbx,
    Bgra,
    Bgrx,
}

impl PixelFormat {
    fn name(self) -> &'static str {
        match self {
            Self::Rgba => "RGBA",
            Self::Rgbx => "RGBX",
            Self::Bgra => "BGRA",
            Self::Bgrx => "BGRX",
        }
    }
}

#[derive(Clone)]
pub struct Frame {
    pub width: u32,
    pub height: u32,
    pub timestamp: i64,
    format: PixelFormat,
    storage: Storage,
}

impl Frame {
    pub fn packed(
        width: u32,
        height: u32,
        timestamp: i64,
        mut bytes: Vec<u8>,
        format: PixelFormat,
    ) -> Option<Self> {
        let length = (width as usize)
            .checked_mul(height as usize)?
            .checked_mul(4)?;
        if length == 0 || bytes.len() != length {
            return None;
        }
        if matches!(format, PixelFormat::Rgbx | PixelFormat::Bgrx) {
            for pixel in bytes.chunks_exact_mut(4) {
                pixel[3] = 255;
            }
        }
        Some(Self {
            width,
            height,
            timestamp,
            format,
            storage: Storage::Pixels(bytes.into()),
        })
    }

    pub fn native(
        width: u32,
        height: u32,
        timestamp: i64,
        format: PixelFormat,
        source: Arc<dyn NativeFrame>,
    ) -> Self {
        Self {
            width,
            height,
            timestamp,
            storage: Storage::Native(source),
            format,
        }
    }

    pub fn texture(&self, context: &backend::Context) -> Result<wgpu::Texture, String> {
        let bytes = match &self.storage {
            Storage::Pixels(bytes) => bytes,
            Storage::Native(native) => return native.texture(context),
        };
        let size = wgpu::Extent3d {
            width: self.width,
            height: self.height,
            depth_or_array_layers: 1,
        };
        let texture = context.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("VideoFrame pixels"),
            size,
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: if matches!(self.format, PixelFormat::Bgra | PixelFormat::Bgrx) {
                wgpu::TextureFormat::Bgra8Unorm
            } else {
                wgpu::TextureFormat::Rgba8Unorm
            },
            usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });
        context.queue.write_texture(
            texture.as_image_copy(),
            bytes,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(self.width * 4),
                rows_per_image: Some(self.height),
            },
            size,
        );
        Ok(texture)
    }
}

pub enum SourceError {
    NoFrame,
    Origin,
    Unavailable(String),
}
/// None identifies an object other than a host video element.
pub type ElementSource = unsafe fn(*mut c_void, u64) -> Option<Result<Frame, SourceError>>;

pub fn set_element_source(g: *mut c_void, source: ElementSource) {
    states()
        .lock()
        .unwrap()
        .entry(g as usize)
        .or_default()
        .video_source = Some(source);
}

pub(super) struct Imported {
    pub raw: wgpu::ExternalTexture,
    pub expired: AtomicBool,
    _plane: wgpu::Texture,
}
impl Imported {
    fn expire(&self) {
        if !self.expired.swap(true, Ordering::AcqRel) {
            self.raw.destroy();
        }
    }
}
pub(super) struct FrameHandle {
    data: Mutex<Option<Frame>>,
    imports: Mutex<Vec<Weak<Imported>>>,
    timestamp: i64,
    duration: Option<u64>,
}
fn expire(list: Vec<Weak<Imported>>) {
    for value in list.into_iter().filter_map(|v| v.upgrade()) {
        value.expire();
    }
}
pub(super) unsafe fn expire_elements(g: *mut c_void) {
    let list = states()
        .lock()
        .unwrap()
        .get_mut(&(g as usize))
        .map(|s| std::mem::take(&mut s.video_imports));
    if let Some(list) = list {
        expire(list);
    }
}
unsafe fn fail<T>(g: *mut c_void, name: &str, msg: &str) -> Result<T, ()> {
    redjs::webapi::domexception::throw(g, name, msg);
    Err(())
}

pub(super) struct Source {
    pub frame: Frame,
    handle: Option<Arc<FrameHandle>>,
}
pub(super) unsafe fn source(g: *mut c_void, object: u64) -> Result<Option<Source>, ()> {
    if let Some(h) = try_handle(g, object, Kind::VideoFrame) {
        let Resource::VideoFrame(handle) = h.resource else {
            unreachable!()
        };
        let data = handle.data.lock().unwrap().clone();
        let Some(frame) = data else {
            return fail(g, "InvalidStateError", "VideoFrame is closed");
        };
        return Ok(Some(Source {
            frame,
            handle: Some(handle),
        }));
    }
    let resolver = states()
        .lock()
        .unwrap()
        .get(&(g as usize))
        .and_then(|s| s.video_source);
    match resolver.and_then(|resolve| resolve(g, object)) {
        None => Ok(None),
        Some(Ok(frame)) => Ok(Some(Source {
            frame,
            handle: None,
        })),
        Some(Err(SourceError::NoFrame)) => {
            fail(g, "InvalidStateError", "The video has no current frame")
        }
        Some(Err(SourceError::Origin)) => fail(g, "SecurityError", "The video is not origin-clean"),
        Some(Err(SourceError::Unavailable(e))) => fail(g, "OperationError", &e),
    }
}

unsafe fn frame_handle(g: *mut c_void, cf: *mut c_void) -> Result<Arc<FrameHandle>, ()> {
    let h = this(g, cf, Kind::VideoFrame)?;
    let Resource::VideoFrame(handle) = h.resource else {
        unreachable!()
    };
    Ok(handle)
}
unsafe fn wrap_frame(g: *mut c_void, frame: Frame, duration: Option<u64>) -> u64 {
    let handle = FrameHandle {
        timestamp: frame.timestamp,
        data: Mutex::new(Some(frame)),
        imports: Mutex::default(),
        duration,
    };
    wrap(
        g,
        Kind::VideoFrame,
        None,
        Resource::VideoFrame(Arc::new(handle)),
    )
}
unsafe fn packed_input(
    g: *mut c_void,
    source: u64,
    init: u64,
    format: PixelFormat,
    timestamp: i64,
) -> Result<Frame, ()> {
    let width = uint(g, field(g, init, "codedWidth")?.v, 0)?;
    let height = uint(g, field(g, init, "codedHeight")?.v, 0)?;
    let row = u64::from(width) * 4;
    if width == 0
        || height == 0
        || !row
            .checked_mul(u64::from(height))
            .is_some_and(|n| n <= 256 * 1024 * 1024)
    {
        return fail(g, "TypeError", "Invalid or oversized VideoFrame dimensions");
    }
    let layout = seq(g, field(g, init, "layout")?.v)?;
    let (offset, stride) = match layout.as_slice() {
        [] => (0, row),
        [plane] => (
            num(g, field(g, plane.v, "offset")?.v, 0)?,
            num(g, field(g, plane.v, "stride")?.v, row)?,
        ),
        _ => return fail(g, "TypeError", "A packed VideoFrame has one plane"),
    };
    let end = stride
        .checked_mul(u64::from(height - 1))
        .and_then(|n| n.checked_add(offset))
        .and_then(|n| n.checked_add(row));
    let mut ta = redjs::ffi::RjsTaInfo::default();
    let mut ab = redjs::ffi::RjsAbInfo::default();
    let (ptr, len, detached, shared) = if glue::rjs_typedarray_info(g, source, &mut ta) == 0 {
        (ta.data, u64::from(ta.byte_length), ta.detached, ta.shared)
    } else if glue::rjs_arraybuffer_info(g, source, &mut ab) == 0 {
        (ab.data, ab.byte_length, ab.detached, ab.shared)
    } else {
        return fail(g, "TypeError", "Expected a VideoFrame BufferSource");
    };
    if shared != 0 {
        return fail(
            g,
            "NotSupportedError",
            "Shared VideoFrame buffers are not supported",
        );
    }
    if detached != 0
        || ptr.is_null()
        || stride < row
        || !end.is_some_and(|e| e <= len && e <= isize::MAX as u64)
    {
        return fail(g, "TypeError", "VideoFrame plane exceeds its buffer");
    }
    // All dictionary conversions have finished. No JS can detach the input during this copy.
    let mut bytes = Vec::with_capacity((row * u64::from(height)) as usize);
    for y in 0..height {
        let start = (offset + u64::from(y) * stride) as usize;
        bytes.extend_from_slice(std::slice::from_raw_parts(
            ptr.cast::<u8>().add(start),
            row as usize,
        ));
    }
    Ok(Frame::packed(width, height, timestamp, bytes, format).unwrap())
}

#[redjs::host_fn]
pub(super) unsafe fn construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let object = Val::new(g, cf_arg(cf, 0));
        let init = Val::new(g, cf_arg(cf, 1));
        let ts = field(g, init.v, "timestamp")?;
        let timestamp = if ts.v == UNDEF {
            None
        } else {
            let t = to_number(g, ts.v)?;
            if !t.is_finite() || t < i64::MIN as f64 || t >= -(i64::MIN as f64) {
                return fail(g, "TypeError", "Invalid VideoFrame timestamp");
            }
            Some(t as i64)
        };
        let dur = field(g, init.v, "duration")?;
        let mut duration = if dur.v == UNDEF {
            None
        } else {
            Some(num(g, dur.v, 0)?)
        };
        let format = field(g, init.v, "format")?;
        let mut frame = if format.v != UNDEF {
            let format = match text(g, format.v, "")?.as_str() {
                "RGBA" => PixelFormat::Rgba,
                "BGRA" => PixelFormat::Bgra,
                "RGBX" => PixelFormat::Rgbx,
                "BGRX" => PixelFormat::Bgrx,
                _ => {
                    return fail(
                        g,
                        "NotSupportedError",
                        "Expected a packed RGBA, RGBX, BGRA or BGRX frame",
                    )
                }
            };
            let Some(t) = timestamp else {
                return fail(g, "TypeError", "VideoFrame requires a timestamp");
            };
            packed_input(g, object.v, init.v, format, t)?
        } else if let Some(s) = source(g, object.v)? {
            duration = duration.or_else(|| s.handle.as_ref()?.duration);
            s.frame
        } else {
            let Some(t) = timestamp else {
                return fail(g, "TypeError", "VideoFrame requires a timestamp");
            };
            let mut pixels = external_pixels(g, object.v, 0)?;
            if !pixels.origin_clean {
                return fail(g, "SecurityError", "VideoFrame source is not origin-clean");
            }
            if pixels.premultiplied {
                for p in pixels.rgba.chunks_exact_mut(4) {
                    let a = u32::from(p[3]);
                    for v in &mut p[..3] {
                        *v = if a == 0 {
                            0
                        } else {
                            ((u32::from(*v) * 255 + a / 2) / a).min(255) as u8
                        };
                    }
                }
            }
            let Some(frame) = Frame::packed(
                pixels.width,
                pixels.height,
                t,
                pixels.rgba,
                PixelFormat::Rgba,
            ) else {
                return fail(g, "InvalidStateError", "Empty video frame");
            };
            frame
        };
        if let Some(t) = timestamp {
            frame.timestamp = t;
        }
        Ok(wrap_frame(g, frame, duration))
    })
}
#[redjs::host_fn]
unsafe fn clone_frame(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let handle = frame_handle(g, cf)?;
        let frame = handle.data.lock().unwrap().clone();
        let Some(frame) = frame else {
            return fail(g, "InvalidStateError", "VideoFrame is closed");
        };
        Ok(wrap_frame(g, frame, handle.duration))
    })
}
#[redjs::host_fn]
unsafe fn close(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let handle = frame_handle(g, cf)?;
        handle.data.lock().unwrap().take();
        expire(std::mem::take(&mut *handle.imports.lock().unwrap()));
        Ok(UNDEF)
    })
}
macro_rules! frame_getter {
    ($name:ident, $f:expr) => {
        #[redjs::host_fn]
        unsafe fn $name(g: *mut c_void, cf: *mut c_void) -> u64 {
            run(g, || {
                let h = frame_handle(g, cf)?;
                Ok(($f)(g, &h))
            })
        }
    };
}
frame_getter!(width, |_, h: &FrameHandle| enc_f64(
    h.data.lock().unwrap().as_ref().map_or(0, |f| f.width) as f64
));
frame_getter!(height, |_, h: &FrameHandle| enc_f64(
    h.data.lock().unwrap().as_ref().map_or(0, |f| f.height) as f64
));
frame_getter!(timestamp, |_, h: &FrameHandle| enc_f64(h.timestamp as f64));
frame_getter!(duration, |_, h: &FrameHandle| h

    .duration
    .map_or(NULL, |v| enc_f64(v as f64)));
frame_getter!(format, |g, h: &FrameHandle| {
    let format = h.data.lock().unwrap().as_ref().map(|f| f.format);
    format.map_or(NULL, |f| js_string(g, f.name()))
});

pub(super) unsafe fn install(g: *mut c_void) {
    class(g, Kind::ExternalTexture, "GPUExternalTexture", &[], &[]);
    class(
        g,
        Kind::VideoFrame,
        "VideoFrame",
        &[("close", 0, close), ("clone", 0, clone_frame)],
        &[
            ("codedWidth", width),
            ("codedHeight", height),
            ("displayWidth", width),
            ("displayHeight", height),
            ("timestamp", timestamp),
            ("duration", duration),
            ("format", format),
        ],
    );
}

#[redjs::host_fn]
pub(super) unsafe fn import(g: *mut c_void, cf: *mut c_void) -> u64 {
    run(g, || {
        let h = this(g, cf, Kind::Device)?;
        let c = client(g, &h)?;
        let descriptor = Val::new(g, cf_arg(cf, 0));
        let label = text(g, field(g, descriptor.v, "label")?.v, "")?;
        let color = text(g, field(g, descriptor.v, "colorSpace")?.v, "srgb")?;
        let object = field(g, descriptor.v, "source")?;
        let Some(source) = source(g, object.v)? else {
            return fail(g, "TypeError", "Expected a video element or VideoFrame");
        };
        if color != "srgb" {
            return fail(
                g,
                if color == "display-p3" {
                    "NotSupportedError"
                } else {
                    "TypeError"
                },
                "Video imports support sRGB",
            );
        }
        if !c
            .context
            .device
            .features()
            .contains(Features::EXTERNAL_TEXTURE)
        {
            return fail(
                g,
                "NotSupportedError",
                "This backend requires copyExternalImageToTexture for video",
            );
        }
        if let Err(e) = c.check() {
            return fail(g, "InvalidStateError", &e);
        }
        let plane = checked(g, &c, || source.frame.texture(&c.context)).map_err(|e| {
            redjs::webapi::domexception::throw(g, "OperationError", &e);
        })?;
        let raw = checked(g, &c, || {
            c.context.device.create_external_texture(
                &wgpu::ExternalTextureDescriptor {
                    label: Some(&label),
                    format: wgpu::ExternalTextureFormat::Rgba,
                    width: source.frame.width,
                    height: source.frame.height,
                    yuv_conversion_matrix: [0.; 16],
                    gamut_conversion_matrix: [1., 0., 0., 0., 1., 0., 0., 0., 1.],
                    src_transfer_function: Default::default(),
                    dst_transfer_function: Default::default(),
                    sample_transform: [1., 0., 0., 1., 0., 0.],
                    load_transform: [1., 0., 0., 1., 0., 0.],
                },
                &[&plane.create_view(&Default::default())],
            )
        });
        let imported = Arc::new(Imported {
            raw,
            expired: AtomicBool::new(false),
            _plane: plane,
        });
        if let Some(handle) = source.handle {
            let mut list = handle.imports.lock().unwrap();
            list.retain(|t| t.strong_count() != 0);
            list.push(Arc::downgrade(&imported));
        } else {
            states()
                .lock()
                .unwrap()
                .get_mut(&(g as usize))
                .unwrap()
                .video_imports
                .push(Arc::downgrade(&imported));
        }
        Ok(wrap_labeled(
            g,
            Kind::ExternalTexture,
            Some(c),
            Resource::ExternalTexture(imported),
            &label,
        ))
    })
}