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
//! What the shader API (`crate::shader`) gives a backend, with or without the
//! renderer (feature `shader`).
use std::borrow::Cow;
/// The largest side of a frame, in pixels.
pub const MAX_SIDE: u32 = 4096;
/// The pixels a backend shows: 8 bits per channel, premultiplied alpha.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PixelFormat {
/// Red, green, blue, alpha (GDK's `R8G8B8A8_PREMULTIPLIED`, Core
/// Graphics' premultiplied last, Android's `ARGB_8888`).
#[default]
Rgba,
/// Blue, green, red, alpha (Direct2D's, GPUI's).
Bgra,
}
/// A rendered frame: `height` rows of `width` pixels, `stride` bytes apart.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct Frame {
/// The pixels, row by row.
pub pixels: Vec<u8>,
/// Width in pixels.
pub width: u32,
/// Height in pixels.
pub height: u32,
/// Bytes from one row to the next (at least `width * 4`).
pub stride: u32,
/// Counts the frames shown: a new value is a new picture.
pub serial: u64,
}
/// Windows: the texture of a view's frame, shared with the backend's
/// Direct3D 11 device (`ShaderView::surface`).
#[cfg(windows)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct SharedTexture {
/// The view's textures and fence for one frame size, unique in the
/// process: handles of another set are other objects, even when equal.
pub set: u64,
/// The texture's NT handle (`ID3D11Device1::OpenSharedResource1`), valid
/// while the view keeps frames of this size.
pub texture: windows_sys::Win32::Foundation::HANDLE,
/// The NT handle of the view's fence (`ID3D11Device5::OpenSharedFence`),
/// valid while the view keeps frames of this size.
pub fence: windows_sys::Win32::Foundation::HANDLE,
/// What to signal on the fence once the frame that draws the texture is
/// submitted: until then the view renders into none of its others.
pub value: u64,
/// Width in pixels.
pub width: u32,
/// Height in pixels.
pub height: u32,
}
impl Frame {
/// The rows, without their padding.
pub fn rows(&self) -> impl Iterator<Item = &[u8]> {
let row = self.width as usize * 4;
self.pixels
.chunks(self.stride.max(1) as usize)
.take(self.height as usize)
.map(move |r| &r[..row.min(r.len())])
}
/// The pixels with no padding between rows (`width * 4` bytes apart).
pub fn tight(&self) -> Cow<'_, [u8]> {
if self.stride == self.width * 4 {
Cow::Borrowed(&self.pixels)
} else {
Cow::Owned(self.rows().flatten().copied().collect())
}
}
}