Skip to content

Commit 9d2e23d

Browse files
authored
Prepare for supporting multiple pixel formats (#359)
Make `PixelFormat` `#[non_exhaustive]`, and rename the variants from `Rgba` -> `Rgba8` and `Bgra` -> `Bgra8`.
1 parent edbbb61 commit 9d2e23d

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

src/format.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/// A pixel format that Softbuffer may use.
1+
/// The pixel format of a surface and buffer.
22
///
33
/// # Alpha
44
///
55
/// These pixel formats all include the alpha channel in their name, but formats that ignore the
6-
/// alpha channel are supported if you set [`AlphaMode::Ignored`]. This will make `RGBA` mean `RGBX`
7-
/// and `BGRA` mean `BGRX`.
6+
/// alpha channel are supported if you use [`AlphaMode::Ignored`]. This will make `Rgba8` mean
7+
/// `Rgbx8`, for example.
88
///
99
/// [`AlphaMode::Ignored`]: crate::AlphaMode::Ignored
1010
///
@@ -13,27 +13,45 @@
1313
/// The [`Default::default`] implementation returns the pixel format that Softbuffer uses for the
1414
/// current target platform.
1515
///
16-
/// Currently, this is [`BGRA`][Self::Bgra] on all platforms except WebAssembly and Android, where
17-
/// it is [`RGBA`][Self::Rgba], since the API on these platforms does not support BGRA.
16+
/// Currently, this is [`Bgra8`][Self::Bgra8] on all platforms except WebAssembly and Android, where
17+
/// it is [`Rgba8`][Self::Rgba8], since the API on these platforms does not support BGRA.
1818
///
1919
/// The format for a given platform may change in a non-breaking release if found to be more
2020
/// performant.
2121
///
2222
/// This distinction should only be relevant if you're bitcasting `Pixel` to/from a `u32`, to e.g.
2323
/// avoid unnecessary copying, see the documentation for [`Pixel`][crate::Pixel] for examples.
24+
#[non_exhaustive]
2425
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
2526
pub enum PixelFormat {
26-
/// The pixel format is `RGBA` (red, green, blue, alpha).
27+
// This uses roughly the same naming scheme as WebGPU does.
28+
/// 32-bit BGRA, `u8` per channel. Laid out in memory as `B,G,R,A`.
2729
///
28-
/// This is currently the default on macOS/iOS, KMS/DRM, Orbital, Wayland, Windows and X11.
30+
/// **This is currently the default on macOS/iOS, KMS/DRM, Orbital, Wayland, Windows and X11**.
31+
///
32+
/// ## Platform Support
33+
///
34+
/// - macOS/iOS, KMS/DRM, Orbital, Wayland, Windows and X11: Supported.
35+
/// - Android and Web: Not yet supported.
2936
#[cfg_attr(not(any(target_family = "wasm", target_os = "android")), default)]
30-
Bgra,
31-
/// The pixel format is `BGRA` (blue, green, red, alpha).
37+
#[doc(alias = "Argb8888")]
38+
#[doc(alias = "Xrgb8888")]
39+
#[doc(alias = "VK_FORMAT_B8G8R8A8_UNORM")]
40+
Bgra8,
41+
42+
/// 32-bit RGBA, `u8` per channel. Laid out in memory as `R,G,B,A`.
43+
///
44+
/// **This is currently the default on Android and Web**.
45+
///
46+
/// ## Platform Support
3247
///
33-
/// This is currently the default on Android and Web.
48+
/// - Android and Web: Supported.
49+
/// - macOS/iOS, KMS/DRM, Orbital, Wayland, Windows and X11: Not yet supported.
3450
#[cfg_attr(any(target_family = "wasm", target_os = "android"), default)]
35-
Rgba,
36-
// Intentionally exhaustive for now.
51+
#[doc(alias = "Abgr8888")]
52+
#[doc(alias = "Xbgr8888")]
53+
#[doc(alias = "VK_FORMAT_R8G8B8A8_UNORM")]
54+
Rgba8,
3755
}
3856

3957
impl PixelFormat {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl Buffer<'_> {
447447
/// let width = buffer.width().get();
448448
/// let height = buffer.height().get();
449449
///
450-
/// if PixelFormat::Rgba.is_default()
450+
/// if PixelFormat::Rgba8.is_default()
451451
/// && buffer.byte_stride().get() == width * 4
452452
/// && buffer.alpha_mode() == AlphaMode::Ignored
453453
/// {

src/pixel.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
/// let red = unsafe { core::mem::transmute::<Pixel, [u8; 4]>(red) };
4444
///
4545
/// match PixelFormat::default() {
46-
/// PixelFormat::Bgra => assert_eq!(red[2], 255),
47-
/// PixelFormat::Rgba => assert_eq!(red[0], 255),
46+
/// PixelFormat::Bgra8 => assert_eq!(red[2], 255),
47+
/// PixelFormat::Rgba8 => assert_eq!(red[0], 255),
48+
/// format => unimplemented!("unknown default pixel format: {format:?}"),
4849
/// }
4950
/// ```
5051
///
@@ -58,8 +59,9 @@
5859
/// let red = unsafe { core::mem::transmute::<Pixel, u32>(red) };
5960
///
6061
/// match PixelFormat::default() {
61-
/// PixelFormat::Bgra => assert_eq!(red, u32::from_ne_bytes([0x00, 0x00, 0xff, 0xff])),
62-
/// PixelFormat::Rgba => assert_eq!(red, u32::from_ne_bytes([0xff, 0x00, 0x00, 0xff])),
62+
/// PixelFormat::Bgra8 => assert_eq!(red, u32::from_ne_bytes([0x00, 0x00, 0xff, 0xff])),
63+
/// PixelFormat::Rgba8 => assert_eq!(red, u32::from_ne_bytes([0xff, 0x00, 0x00, 0xff])),
64+
/// format => unimplemented!("unknown default pixel format: {format:?}"),
6365
/// }
6466
/// ```
6567
#[repr(C)]

0 commit comments

Comments
 (0)