vello_cpu/lib.rs
1// Copyright 2025 the Vello Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4// After you edit the crate's doc comment, run this command, then check README.md for any missing links
5// cargo rdme --workspace-project=vello_cpu
6
7//! Vello CPU is a 2D graphics rendering engine written in Rust, for devices with no or underpowered GPUs.
8//!
9//! We also develop [Vello](https://crates.io/crates/vello), which makes use of the GPU for 2D rendering and has higher performance than Vello CPU.
10//! Vello CPU is being developed as part of work to address shortcomings in Vello.
11//!
12//! # Usage
13//!
14//! To use Vello CPU, you need to:
15//!
16//! - Create a [`RenderContext`][], a 2D drawing context for a fixed-size target area.
17//! - For each object in your scene:
18//! - Set how the object will be painted, using [`set_paint`][RenderContext::set_paint].
19//! - Set the shape to be drawn for that object, using methods like [`fill_path`][RenderContext::fill_path],
20//! [`stroke_path`][RenderContext::stroke_path], or [`glyph_run`][RenderContext::glyph_run].
21//! - Render it to an image using [`RenderContext::render_to_pixmap`][].
22//!
23//! ```rust
24//! use vello_cpu::{RenderContext, Pixmap, RenderMode};
25//! use vello_cpu::{color::{palette::css, PremulRgba8}, kurbo::Rect};
26//! let width = 10;
27//! let height = 5;
28//! let mut context = RenderContext::new(width, height);
29//! context.set_paint(css::MAGENTA);
30//! context.fill_rect(&Rect::from_points((3., 1.), (7., 4.)));
31//!
32//! let mut target = Pixmap::new(width, height);
33//! // While calling `flush` is only strictly necessary if you are rendering using
34//! // multiple threads, it is recommended to always do this.
35//! context.flush();
36//! context.render_to_pixmap(&mut target);
37//!
38//! let expected_render = b"\
39//! 0000000000\
40//! 0001111000\
41//! 0001111000\
42//! 0001111000\
43//! 0000000000";
44//! let magenta = css::MAGENTA.premultiply().to_rgba8();
45//! let transparent = PremulRgba8 {r: 0, g: 0, b: 0, a: 0};
46//! let mut result = Vec::new();
47//! for pixel in target.data() {
48//! if *pixel == magenta {
49//! result.push(b'1');
50//! } else if *pixel == transparent {
51//! result.push(b'0');
52//! } else {
53//! panic!("Got unexpected pixel value {pixel:?}");
54//! }
55//! }
56//! assert_eq!(&result, expected_render);
57//! ```
58//!
59//! Feel free to take a look at some further
60//! [examples](https://github.com/linebender/vello/tree/main/sparse_strips/vello_cpu/examples)
61//! to better understand how to interact with Vello CPU's API,
62//!
63//! # Features
64//!
65//! - `std` (enabled by default): Get floating point functions from the standard library
66//! (likely using your target's libc).
67//! - `libm`: Use floating point implementations from [libm][].
68//! - `png`(enabled by default): Allow loading [`Pixmap`]s from PNG images.
69//! Also required for rendering glyphs with an embedded PNG. Implies `std`.
70//! - `multithreading`: Enable multi-threaded rendering. Implies `std`.
71//! - `text` (enabled by default): Enables glyph rendering ([`glyph_run`][RenderContext::glyph_run]).
72//! - `u8_pipeline` (enabled by default): Enable the u8 pipeline, for speed focused rendering using u8 math.
73//! The `u8` pipeline will be used for [`OptimizeSpeed`][RenderMode::OptimizeSpeed], if both pipelines are enabled.
74//! If you're using Vello CPU for application rendering, you should prefer this pipeline.
75//! - `f32_pipeline`: Enable the `f32` pipeline, which is slower but has more accurate
76//! results. This is espectially useful for rendering test snapshots.
77//! The `f32` pipeline will be used for [`OptimizeQuality`][RenderMode::OptimizeQuality], if both pipelines are enabled.
78//!
79//! At least one of `std` and `libm` is required; `std` overrides `libm`.
80//! At least one of `u8_pipeline` and `f32_pipeline` must be enabled.
81//! You might choose to disable one of these pipelines if your application
82//! won't use it, so as to reduce binary size.
83//!
84//! # Caveats
85//!
86//! Overall, Vello CPU is already very feature-rich and should be ready for
87//! production use cases. The main caveat at the moment is that the API is
88//! still likely to change and not stable yet. For example, we have
89//! known plans to change the API around how image resources are used.
90//!
91//! Additionally, there are certain APIs that are still very much experimental,
92//! including for example support for filters. This will be reflected in the
93//! documentation of those APIs.
94//!
95//! Another caveat is that multi-threading with large thread counts
96//! (more than 4) might give diminishing returns, especially when
97//! making heavy use of layers and clip paths.
98//!
99//! # Performance
100//!
101//! Performance benchmarks can be found [here](https://laurenzv.github.io/vello_chart/),
102//! As can be seen, Vello CPU achieves compelling performance on both,
103//! aarch64 and x86 platforms. We also have SIMD optimizations for WASM SIMD,
104//! meaning that you can expect good performance there as well.
105//!
106//! # Implementation
107//!
108//! If you want to gain a better understanding of Vello CPU and the
109//! sparse strips paradigm, you can take a look at the [accompanying
110//! master's thesis](https://ethz.ch/content/dam/ethz/special-interest/infk/inst-pls/plf-dam/documents/StudentProjects/MasterTheses/2025-Laurenz-Thesis.pdf)
111//! that was written on the topic. Note that parts of the descriptions might
112//! become outdated as the implementation changes, but it should give a good
113//! overview nevertheless.
114//!
115//! <!-- We can't directly link to the libm crate built locally, because our feature is only a pass-through -->
116//! [libm]: https://crates.io/crates/libm
117// LINEBENDER LINT SET - lib.rs - v3
118// See https://linebender.org/wiki/canonical-lints/
119// These lints shouldn't apply to examples or tests.
120#![cfg_attr(not(test), warn(unused_crate_dependencies))]
121// These lints shouldn't apply to examples.
122#![warn(clippy::print_stdout, clippy::print_stderr)]
123// Targeting e.g. 32-bit means structs containing usize can give false positives for 64-bit.
124#![cfg_attr(target_pointer_width = "64", warn(clippy::trivially_copy_pass_by_ref))]
125// END LINEBENDER LINT SET
126#![cfg_attr(docsrs, feature(doc_cfg))]
127#![forbid(unsafe_code)]
128#![expect(
129 clippy::cast_possible_truncation,
130 reason = "We cast u16s to u8 in various places where we know for sure that it's < 256"
131)]
132#![no_std]
133
134extern crate alloc;
135extern crate core;
136#[cfg(feature = "std")]
137extern crate std;
138
139#[cfg(all(not(feature = "u8_pipeline"), not(feature = "f32_pipeline")))]
140compile_error!("vello_cpu must have at least one of the u8 or f32 pipelines enabled");
141
142mod render;
143
144mod dispatch;
145mod filter;
146#[doc(hidden)]
147pub mod fine;
148#[doc(hidden)]
149pub mod layer_manager;
150#[doc(hidden)]
151pub mod region;
152mod util;
153
154pub use render::{RenderContext, RenderSettings};
155pub use vello_common::fearless_simd::Level;
156#[cfg(feature = "text")]
157pub use vello_common::glyph::Glyph;
158pub use vello_common::mask::Mask;
159pub use vello_common::paint::{Image, ImageSource, Paint, PaintType};
160pub use vello_common::pixmap::Pixmap;
161pub use vello_common::{color, kurbo, peniko};
162
163/// The selected rendering mode.
164/// For using [`RenderMode::OptimizeQuality`] you also need to enable `f32_pipeline` feature.
165#[derive(Copy, Clone, Debug, Default)]
166pub enum RenderMode {
167 /// Optimize speed (by performing calculations with u8/16).
168 #[default]
169 OptimizeSpeed,
170 /// Optimize quality (by performing calculations with f32).
171 OptimizeQuality,
172}