imaging_skia 0.0.1

Skia backend for the imaging command stream.
docs.rs failed to build imaging_skia-0.0.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

Imaging Skia

Skia backend for the imaging command stream.

Latest published version. Documentation build status. Apache 2.0 or MIT license.
GitHub Actions CI status.

Skia backend for imaging.

This crate provides a CPU raster renderer that consumes imaging::record::Scene or native Skia draw targets and produces an RGBA8 image buffer using Skia.

Render A Recorded Scene

Record commands into imaging::record::Scene, then hand the scene to SkiaCpuRenderer.

use imaging::{Painter, record};
use imaging_skia::SkiaCpuRenderer;
use kurbo::Rect;
use peniko::{Brush, Color};

fn main() -> Result<(), imaging_skia::Error> {
    let paint = Brush::Solid(Color::from_rgb8(0x2a, 0x6f, 0xdb));
    let mut scene = record::Scene::new();

    {
        let mut painter = Painter::new(&mut scene);
        painter.fill_rect(Rect::new(0.0, 0.0, 128.0, 128.0), &paint);
    }

    let mut renderer = SkiaCpuRenderer::new();
    let image = renderer.render_scene(&scene, 128, 128)?;
    assert_eq!(image.data.len(), 128 * 128 * 4);
    Ok(())
}

Draw Into An Existing Canvas

If you already have a Skia canvas, wrap it with SkCanvasSink and stream commands directly.

use imaging::Painter;
use imaging_skia::SkCanvasSink;
use kurbo::Rect;
use peniko::{Brush, Color};
use skia_safe::surfaces;

fn main() -> Result<(), imaging_skia::Error> {
    let paint = Brush::Solid(Color::from_rgb8(0x1d, 0x4e, 0x89));
    let mut surface = surfaces::raster_n32_premul((128, 128)).unwrap();

    {
        let mut sink = SkCanvasSink::new(surface.canvas());
        let mut painter = Painter::new(&mut sink);
        painter.fill_rect(Rect::new(0.0, 0.0, 128.0, 128.0), &paint);
        sink.finish()?;
    }

    Ok(())
}

Record A SkPicture

Use SkPictureRecorderSink when you want Skia's native retained recording format.

use imaging::Painter;
use imaging_skia::SkPictureRecorderSink;
use kurbo::Rect;
use peniko::{Brush, Color};

fn main() -> Result<(), imaging_skia::Error> {
    let paint = Brush::Solid(Color::from_rgb8(0x7c, 0x3a, 0xed));
    let mut sink = SkPictureRecorderSink::new(Rect::new(0.0, 0.0, 128.0, 128.0));

    {
        let mut painter = Painter::new(&mut sink);
        painter.fill_rect(Rect::new(16.0, 16.0, 112.0, 112.0), &paint);
    }

    let picture = sink.finish_picture()?;
    assert_eq!(picture.cull_rect().right, 128.0);
    Ok(())
}

Render A Native SkPicture

If you already have a recorded picture, hand it directly to SkiaCpuRenderer.

use imaging::Painter;
use imaging_skia::{SkPictureRecorderSink, SkiaCpuRenderer};
use kurbo::Rect;
use peniko::{Brush, Color};

fn main() -> Result<(), imaging_skia::Error> {
    let paint = Brush::Solid(Color::from_rgb8(0xd9, 0x77, 0x06));
    let mut sink = SkPictureRecorderSink::new(Rect::new(0.0, 0.0, 128.0, 128.0));

    {
        let mut painter = Painter::new(&mut sink);
        painter.fill_rect(Rect::new(16.0, 16.0, 112.0, 112.0), &paint);
    }

    let picture = sink.finish_picture()?;
    let mut renderer = SkiaCpuRenderer::new();
    let image = renderer.render_picture(&picture, 128, 128)?;
    assert_eq!(image.data.len(), 128 * 128 * 4);
    Ok(())
}

GPU Rendering

Enable the gpu feature when you want Skia Ganesh rendering through app-owned wgpu handles. SkiaRenderer reuses the current backend selected by wgpu and renders native skia_safe::Picture values into caller-owned wgpu::Texture targets while also supporting RGBA8 image output.

use imaging::{Painter, record};
use imaging_skia::SkiaRenderer;
use kurbo::Rect;
use peniko::{Brush, Color};

let mut scene = record::Scene::new();
{
    let mut painter = Painter::new(&mut scene);
    painter.fill_rect(
        Rect::new(0.0, 0.0, 128.0, 128.0),
        &Brush::Solid(Color::from_rgb8(0x2a, 0x6f, 0xdb)),
    );
}

let mut renderer = SkiaRenderer::new(adapter, device, queue)?;
let picture = renderer.encode_scene(&scene, 128, 128)?;
renderer.render_picture_to_texture(&picture, &texture)?;

Building

skia-safe / skia-bindings normally download prebuilt Skia binaries at build time. In offline or sandboxed environments, set SKIA_BINARIES_URL to a local tar.gz downloaded ahead of time:

SKIA_BINARIES_URL='file:///absolute/path/to/skia-binaries-....tar.gz' cargo build

Minimum supported Rust Version (MSRV)

This crate has been verified to compile with Rust 1.92 and later.

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contribution

Contributions are welcome by pull request. The Rust code of conduct applies. Please feel free to add your name to the AUTHORS file in any substantive pull request.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.