skia-safe 0.14.1

Safe Skia Bindings for Rust
docs.rs failed to build skia-safe-0.14.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.
Visit the last successful build: skia-safe-0.17.0

Safe Rust bindings to the Skia Graphics Library.

Build Status

Skia Submodule Status: chrome/m76 (pending changes).

Goals

This project attempts to provide up to date safe bindings that bridge idiomatic Rust with Skia's C++ API on all major desktop, mobile, and WebAssembly platforms, including GPU rendering support for Vulkan, Metal, and OpenGL.

Status

Crate

An official crate is not yet available on crates.io but every update to the release branch uploads new crates to the releases tab and there is also a Milestone that tracks the progress.

Platforms & Build Targets

  • Windows
  • Linux Ubuntu 16 (18 should work, too).
  • macOS
  • Android (macOS | Linux -> aarch64, contributed by @DenisKolodin)
  • iOS
  • WebAssembly: #42 (help wanted).

Bindings & Wrappers

We wrapped most part of the public Skia C++ APIs. To see what's missing, take a look at the API Complete Milestone.

  • Vector Geometry: Matrix, Rect, Point, Size, etc.
  • Most drawing related classes and functions: Surface, Canvas, Paint, Path.
  • Almost all Effects and Shaders.
  • Utility classes we think are useful.
  • PDF & SVG rendering
  • Skia Modules
  • GPU Backends
    • Vulkan
    • OpenGL
    • Metal

Wrappers for functions that take callbacks and virtual classes are not supported right now. While we think they should be wrapped, the use cases related seem to be rather special, so we postponed that for now.

Building

Note that the information in this section is preliminary. Please open an issue for any build problem.

Prerequisites

This project requires LLVM, Python 2, and git to build.

To see which version of LLVM/Clang is available, use clang --version.

We recommend version 8, but also had successes to build Skia with 6.0.1 and 7.0.1, and - on macOS - Apple LLVM version 10. So it's probably best to use the preinstalled version or install version 8 if LLVM is not available on your platform by default.

Python version 2.7 should be available. The build script probes for python --version and python2 --version and uses the first one that looks like a version 2 executable.

macOS

  • Install the XCode command line developer tools with

    xcode-select --install
    
  • macOS Mojave Version 10.14: install the SDK headers:

    sudo open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
    

    otherwise the Skia build may fail to build SkJpegUtility.cpp and the binding generation will fail with 'TargetConditionals.h' file not found . Also note that the command line developer tools and SDK headers should be reinstalled after an update of XCode.

  • As an alternative to Apple LLVM 10, install LLVM via brew install llvm or brew install llvm@7 and then set PATH, CPPFLAGS, and LDFLAGS like instructed.

Windows

  • Be sure the git command line tool is installed.
  • Install the official LLVM 8 distribution.
  • msys:
    • Install one of the Python2 packages, for example mingw-w64-x86_64-python2.
    • LLVM is always picked up from C:/Program Files/LLVM, so be sure it's available from there.
  • without msys:
    • Download and install Python version 2 from python.org.

Linux

  • LLVM should be installed out of the box, if not, install version 8.

Then use:

cargo build -vv

On Linux, OpenGL libraries may be missing, if that is the case, install OpenGL drivers for you graphics card, or install a mesa OpenGL package like libgl1-mesa-dev.

Please share your build experience so that we can try to automate the build and get to the point where cargo build is sufficient to build the bindings including Skia, and if that is not possible, clearly prompts to what's missing.

Android

Cross compilation to Android is supported for targeting 64 bit ARM and Intel x86 architectures (aarch64 and x86_64):

For example, to compile for aarch64:

  1. Install the rust target: rustup target install aarch64-linux-android.
  2. Download the r18b NDK from: https://developer.android.com/ndk/downloads/older_releases.html
  3. Create a toolchain for the compilation: build/tools/make_standalone_toolchain.py --arch arm64 --install-dir /tmp/ndk
  4. Compile your package for the aarch64-linux-android target:

On macOS & Linux:

ANDROID_NDK=~/path/to/android-ndk-r18b PATH=$PATH:/tmp/ndk/bin cargo build --target aarch64-linux-android -vv

On Windows it's a bit more complicated, because the Android NDK clang executable must be invoked through .cmd scripts:

ANDROID_NDK=~/path/to/android-ndk-r18b PATH=$PATH:/tmp/ndk/bin CC_aarch64_linux_android=aarch64-linux-android-clang.cmd CXX_aarch64_linux_android=aarch64-linux-android-clang++.cmd CARGO_TARGET_aarch64_linux_android_LINKER=aarch64-linux-android-clang.cmd cargo build --target aarch64-linux-android -vv

Notes:

  • It doesn't work for the latest NDK, because Skia doesn't support it yet.
  • Rebuilding skia-bindings with a different target may cause linker errors, in that case touch skia-bindings/build.rs will force a rebuild (#10).

iOS

Compilation to iOS is supported on macOS targeting the iOS simulator (--target x86_64-apple-ios) and 64 bit ARM devices (--target aarch64-apple-ios).

Skia

For situations in which Skia does not build or needs to be configured differently, we support some customization support in skia-bindings/build.rs. For more details about how to customize Skia builds, take a look at the README of the skia-bindings package.

Note that crate packages will try to download prebuilt binaries from skia-binaries if the platform matches with one of the binaries build on the CI. If the download fails, a full build of Skia is triggered.

Feature vulkan

Vulkan support can be enabled by setting the Cargo feature default = ["vulkan"] in skia-safe/Cargo.toml, which will cause a rebuild of Skia. To render the examples with Vulkan use cargo run --example skia-org -- [OUTPUT_DIR] --driver vulkan.

Note that Vulkan drivers need to be available. On Windows, they are most likely available already, on Linux this article on linuxconfig.org might get you started, and on macOS with Metal support, install the Vulkan SDK for Mac and configure MoltenVK by setting the DYLD_LIBRARY_PATH, VK_LAYER_PATH, and VK_ICD_FILENAMES environment variables as described in Documentation/getting_started_macos.html.

Feature shaper

The Cargo feature shaper enables text shaping with Harfbuzz and ICU.

On Windows, the file icudtl.dat must be available in your executable's directory. To provide the data file, either copy it from the build's output directory (shown when skia-bindings is compiled with cargo build -vv | grep "ninja: Entering directory"), or - if your executable directory is writable - invoke the function skia_safe::icu::init() before creating the skia_safe::Shaper object.

A simple example can be found in the skia-org command line application.

Examples

The examples are taken from Skia's website and ported to the Rust API.

If you were able to build the project, run

cargo run --example skia-org -- [OUTPUT_DIR]

to generate some Skia drawn PNG images in the directory OUTPUT_DIR. To render with OpenGL, use

cargo run --example skia-org -- [OUTPUT_DIR] --driver opengl

And cargo run --example skia-org -- --help shows the drivers that are supported.

Some examples:

Fill, Stroke, Text:

Fill, Stroke, Text

Sweep Gradient:

Sweep Gradient

Dash Path Effect:

Dash Path Effect

For more, you may take a look at the rust-skia.github.io repository.

This project needs contributions!

If you'd like to help with the bindings, take a look at the Wiki to get started and create an issue to avoid duplicate work. For smaller tasks, grep for "TODO" in the source code. And for heroic work, check out the label help wanted. And if you like to help making the Rust API nicer to use, look out for open issues with the label api ergonomics.

Maintainers

License

MIT