6 releases (3 breaking)
|0.4.2||Jan 23, 2021|
|0.4.1||Jan 19, 2021|
|0.3.0||Dec 19, 2020|
|0.2.0||Nov 16, 2020|
|0.1.0||Jul 4, 2020|
#6 in Rendering
2,067 downloads per month
Used in 15 crates (3 directly)
tiny-skia is a tiny Skia subset ported to Rust.
The goal is to provide an absolute minimal, CPU only, 2D rendering library for the Rust ecosystem, with a focus on a rendering quality, speed and binary size.
tiny-skia is definitely tiny, it support all the common 2D operations
like: filling and stroking a shape with a solid color, gradient or pattern;
stroke dashing; clipping; images blending; PNG load/save.
The main missing feature is text rendering
Note: this is not a Skia replacement and never will be. It's more of a research project.
The main motivation behind this library is to have a small, high-quality 2D rendering library that can be used by resvg. And the choice is rather limited. You basically have to choose between cairo, Qt and Skia. And all of them are relatively bloated, hard to compile and distribute. Not to mention that none of them is written in Rust.
But if we ignore those problems and focus only on quality and speed alone,
Skia is by far the best one.
However, the main problem with Skia is that it's huge. Really huge.
It supports CPU and GPU rendering, multiple input and output formats (including SVG and PDF),
various filters, color spaces, color types and text rendering.
It consists of 370 KLOC without dependencies (around 7 MLOC with dependencies)
and requires around 4-8 GiB of disk space to be built from sources.
And the final binary is 3-8 MiB big, depending on enabled features.
Not to mention that it requires
clang and no other compiler,
uses an obscure build system (
gn) which still uses Python2.
tiny-skia tries to be small, simple and easy to build.
Currently, it has around 14 KLOC, compiles in less than 5s on a modern CPU
and adds around 200KiB to your binary.
The heart of Skia's CPU rendering is SkRasterPipeline. And this is an extremely optimized piece of code. But to be a bit pedantic, it's not really a C++ code. It relies on clang's non-standard vector extensions, which means that it works only with clang. You can actually build it with gcc/msvc, but it will simply ignore all the optimizations and become 15-30 times slower! Which makes it kinda useless.
Skia also supports ARM NEON instructions, which are unavailable in a stable Rust at the moment. Therefore a fallback scalar implementation will be used instead on ARM and other non-x86 targets. So if you're targeting ARM, you better stick with Skia.
Also note, that neither Skia or
tiny-skia are supporting dynamic CPU detection,
so by enabling newer instructions you're making the resulting binary non-portable.
Essentially, you will get a decent performance on x86 targets by default.
But if you are looking for an even better performance, you should compile your application
RUSTFLAGS="-Ctarget-cpu=haswell" env variables to enable AVX instructions.
You can find more information in benches/README.md.
Unless there is a bug,
tiny-skia must produce exactly the same results as Skia.
The library does not rely on unsafe code and all pixels access is checked.
It does have a single
to mark a type as bytemuck::Pod,
but this is perfectly safe. And all the dangerous casts are handled by
The API is a bit unconventional. It doesn't look like cairo, QPainter (Qt), HTML Canvas or even Skia.
The core ideas are that almost everything is stateless, immutable and valid.
Canvasprovides a fairly spartan and low-level API. We don't have a
draw_pathmethod. Instead, there are
Canvascontains just two states: world transform and clip path. We don't have save/restore functionality.
- The only truly mutable type is
Pixmap, which is our raster image.
Pathcannot be modified after creation. It can be transformed, but this function consumes the object.
- All geometry types are always valid and immutable.
You cannot create a negative
Size. And so on.
- All types that store
f32guarantee that it is finite.
Skia is a huge library and we support only a tiny part of. And more importantly, we do not plan to support many feature at all.
- GPU rendering.
- Text rendering (maybe someday).
- PDF generation.
- Non-RGBA8888 images.
- Non-PNG image formats.
- Advanced Bézier path operations.
- Conic path segments.
- Path effects (except dashing).
- Any kind of resource caching.
- ICC profiles.
Despite being a port, we still have a lot of changes even in the supported subset.
- No global alpha.
Unlike Skia, only
Patternis allowed to have opacity. In all other cases you should adjust colors opacity manually.
- No bilinear + mipmap down-scaling support.
tiny-skiauses just a simple alpha mask for clipping, while Skia has a very complicated, but way faster algorithm.
tiny-skia should be viewed as a Rust 2D rendering library that uses Skia algorithms internally.
We have a completely different public API. The internals are also extremely simplified.
But all the core logic and math is borrowed from Skia. Hence the name.
As for the porting process itself, Skia uses goto, inheritance, virtual methods, linked lists, const generics and templates specialization a lot, and all of this features are unavailable in Rust. There are also a lot of pointers magic, implicit mutations and caches. Therefore we have to compromise or even rewrite some parts from scratch.
Right now, the only pure Rust alternative is raqote.
- It doesn't support high-quality antialiasing (hairline stroking in particular).
- It's very slow (see benchmarks).
- There are some rendering issues (like gradient transparency).
- Raqote has a very rudimentary text rendering support, while tiny-skia has none.