Skip to main content

crabshell_render/
lib.rs

1//! Draws Crabshell windows on the CPU: backgrounds, rounded corners,
2//! borders and opacity with tiny-skia, glyphs from cosmic-text, and icons
3//! from SVG, PNG or raw pixels. Only what changed is repainted.
4//!
5//! The platform keeps one [`Renderer`], a [`DamageHistory`] per window and
6//! a [`BufferAge`] per buffer. Each frame it hands the renderer the window's
7//! view and damage and a [`Canvas`] over the buffer's memory, and reports
8//! the returned rectangles to the compositor:
9//!
10//! ```
11//! use crabshell_reactive::Runtime;
12//! use crabshell_render::{BufferAge, Canvas, DamageHistory, PixelFormat, Renderer};
13//! use crabshell_ui::prelude::*;
14//!
15//! let runtime = Runtime::current();
16//! let cx = Cx::new(runtime.new_owner());
17//! let bar = PanelWindow::new(&Output::new("DP-1"))
18//!     .style(Style::new().background(Color::rgb(0x1e, 0x1e, 0x2e)))
19//!     .child(Text::new("hello"));
20//! let window = Window::new(&cx, bar);
21//! let mut text = TextSystem::new();
22//! window.set_size(Size::new(200.0, 30.0));
23//! runtime.flush();
24//! window.layout(&mut text);
25//!
26//! let mut renderer = Renderer::new();
27//! let mut history = DamageHistory::new();
28//! let mut age = BufferAge::default();
29//! let scale = 1.5;
30//! let (width, height) = (300, 45); // 200 x 30 logical pixels at 1.5
31//! let mut buffer = vec![0; width * height * 4];
32//! let damage = window.take_damage();
33//! let report = renderer.draw(
34//!     &window.view(),
35//!     &damage,
36//!     &mut text,
37//!     &mut history,
38//!     Canvas {
39//!         pixels: &mut buffer,
40//!         width: width as u32,
41//!         height: height as u32,
42//!         scale,
43//!         format: PixelFormat::Argb8888,
44//!         age: &mut age,
45//!     },
46//! )?;
47//! assert_eq!(report.len(), 1); // a new buffer is drawn whole
48//! # Ok::<(), crabshell_render::CanvasError>(())
49//! ```
50//!
51//! # How it works
52//!
53//! Damage arrives in logical pixels and becomes buffer pixels, rounded
54//! outward. A buffer that skipped frames also repaints what changed in
55//! them, and one more than half dirty repaints whole. Damaged areas are
56//! drawn in fixed 64-pixel tiles, so a repaint matches a full draw. Edges
57//! snap to whole pixels, text is drawn as grayscale masks, and icons are
58//! cached by source and size. [`capture_frame`] and [`quantize_colors`]
59//! serve screencopy views and wallpaper themes. The design doc's
60//! [Rendering](https://github.com/w0x7y/crabshell/blob/main/docs/design.md#rendering)
61//! section has the full rules.
62
63#![forbid(unsafe_code)]
64#![warn(missing_docs)]
65
66mod animation;
67mod capture;
68mod colors;
69mod damage;
70mod draw;
71mod filters;
72mod icons;
73mod images;
74mod theme;
75mod worker;
76
77#[cfg(test)]
78mod tests;
79
80pub use capture::{Turn, capture_frame, frame_size};
81pub use colors::quantize_colors;
82pub use damage::{BufferAge, DamageHistory, PixelRect};
83pub use draw::{Canvas, CanvasError, PixelFormat, Renderer};
84pub use icons::DEFAULT_THEME as DEFAULT_ICON_THEME;
85pub use theme::IconLookup;