zenith_render/backend.rs
1//! The `RasterBackend` trait — the swappable seam between scene and pixels.
2//!
3//! No backend-specific types (e.g. tiny-skia) appear anywhere in this module.
4
5use zenith_core::{AssetProvider, FontProvider};
6use zenith_scene::Scene;
7
8use crate::error::RenderError;
9
10/// A rasterized image in straight-alpha RGBA8 format (row-major).
11///
12/// Pixels are stored as `[r, g, b, a, r, g, b, a, …]` with row stride
13/// `width * 4`. Alpha is **straight** (un-premultiplied), matching the
14/// `Color` type in `zenith-scene`.
15pub struct RasterImage {
16 /// Image width in pixels.
17 pub width: u32,
18 /// Image height in pixels.
19 pub height: u32,
20 /// Raw RGBA8 bytes (`width * height * 4` bytes).
21 pub rgba: Vec<u8>,
22}
23
24/// Trait that abstracts over different CPU rasterization backends.
25///
26/// The associated methods take and return only types from this crate or the
27/// standard library — no backend-specific types cross the boundary.
28pub trait RasterBackend {
29 /// Rasterize a scene to straight-alpha RGBA8 pixels plus dimensions.
30 ///
31 /// The `fonts` parameter is used to resolve font bytes for glyph runs.
32 /// The `assets` parameter is used to resolve raster image bytes for
33 /// `DrawImage` commands. Runs/images whose id cannot be resolved are
34 /// silently skipped — they do not cause an error.
35 fn rasterize(
36 &self,
37 scene: &Scene,
38 fonts: &dyn FontProvider,
39 assets: &dyn AssetProvider,
40 ) -> Result<RasterImage, RenderError>;
41
42 /// Encode a [`RasterImage`] as deterministic PNG bytes.
43 fn encode_png(&self, image: &RasterImage) -> Result<Vec<u8>, RenderError>;
44}