$ show rust-windowing/softbuffer
This is a Rust library, `softbuffer`, that provides a cross-platform API for software-rendering a pixel buffer to a window. The core abstraction is a `Context` (associated with a display), a `Surface` (associated with a window), and a `Buffer` (a writable pixel array). The user gets a buffer, fills it with pixels (via `&mut [Pixel]`), and presents it to the compositor. The library supports multiple platforms (Wayland, X11, Windows, macOS/iOS, WebAssembly, Android, DRM/KMS, Orbital) using a clean trait-based backend dispatch mechanism. It does not provide its own rendering primitives; instead it focuses on efficiently transferring CPU-written pixel data to the display server, aiming for zero-copy where possible. The codebase is well-structured with thorough documentation, error handling, and CI testing across many targets.
The LLM identified 3 files worth highlighting for reuse:
Uses a `make_dispatch!` macro to generate an enum `ContextDispatch`/`SurfaceDispatch`/`BufferDispatch` that statically dispatches to platform-specific implementations. Each variant holds the concrete backend type. The `ContextInterface` trait implementation tries each backend in order and returns the first that succeeds. This pattern cleanly separates platform logic while allowing the main library code to remain generic. The macro also implements common traits like `Debug` on the enums.
Defines the `Pixel` struct with fields `r`, `g`, `b`, `a` in platform-specific byte order (BGRA on most platforms, RGBA on Web/Android). The struct uses `#[repr(C)]` and `#[repr(align(4))]` to have the same in-memory representation as a `u32`. Provides safe constructors (`new_rgb`, `new_rgba`, etc.) and handles platform-dependent field ordering for ergonomic use. The documentation includes examples for premultiplying/unpremultiplying alpha and transmuting to `u32`.
Contains utility functions used across backends: `union_damage` computes the bounding rectangle of a set of damage rects; `clamp_rect` ensures a rectangle stays within given dimensions; `byte_stride` calculates the desired row stride (aligning to 64-byte boundaries in debug mode, 16-byte in release). Also includes a `PixelBuffer` wrapper that hides the full buffer contents in `Debug` output, and saturating conversion helpers for u32 to i32/i16/u16.