Skip to main content

waft_embedder/
system_gpu.rs

1//! A GPU of the system's, as a host hands it to the app: what the system makes for that GPU, its
2//! video decoders and its hardware buffer allocator, as Direct3D makes a video device on one
3//! adapter, VA-API and GBM open one render node, and FFmpeg makes a hardware device before a
4//! decoder on it. Memory these make opens on that GPU: a picture a decoder writes, or a buffer the
5//! allocator makes, is drawn and shown with no copy across GPUs. Software decoders are among a
6//! GPU's too, as the GPU their pictures are copied into. Apple's systems and Android name no GPU,
7//! their memory opening on any of the machine's, so they hand out one.
8//!
9//! The invariant a host keeps: what a GPU hands out makes its memory on that GPU, never on another.
10
11use std::rc::Rc;
12use std::sync::Arc;
13
14use crate::GpuId;
15use crate::HardwareBufferAllocator;
16use crate::video_decoders::VideoDecoders;
17
18/// One of the system's GPUs ([`Platform::gpu`](crate::Platform::gpu)).
19pub trait SystemGpu {
20    /// The GPU as its system names it; `None` where the system names none.
21    fn id(&self) -> Option<GpuId>;
22
23    /// Its video decoders, on its video hardware and on the processor, whose pictures are in its
24    /// memory; `None` where the system decodes no video for the app.
25    fn video_decoders(&self) -> Option<Rc<dyn VideoDecoders>>;
26
27    /// What allocates its hardware buffers, from any thread; `None` where the system allocates
28    /// none.
29    fn hardware_buffer_allocator(&self) -> Option<Arc<dyn HardwareBufferAllocator>>;
30}
31
32/// A GPU as a host gathers it: its name and what the system made for it, which every host hands
33/// out as its [`SystemGpu`].
34pub struct HostGpu {
35    pub id: Option<GpuId>,
36    pub video_decoders: Option<Rc<dyn VideoDecoders>>,
37    pub hardware_buffer_allocator: Option<Arc<dyn HardwareBufferAllocator>>,
38}
39
40impl SystemGpu for HostGpu {
41    fn id(&self) -> Option<GpuId> {
42        self.id
43    }
44
45    fn video_decoders(&self) -> Option<Rc<dyn VideoDecoders>> {
46        self.video_decoders.clone()
47    }
48
49    fn hardware_buffer_allocator(&self) -> Option<Arc<dyn HardwareBufferAllocator>> {
50        self.hardware_buffer_allocator.clone()
51    }
52}