Skip to content

Commit

Permalink
Merge pull request #3 from pragmatrix/vulkan
Browse files Browse the repository at this point in the history
Vulkan Bindings
  • Loading branch information
pragmatrix committed Feb 16, 2019
2 parents 4324da5 + 74fa18f commit e0391ce
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 297 deletions.
6 changes: 3 additions & 3 deletions .gitignore
@@ -1,5 +1,5 @@
/target
**/*.rs.bk
src/bindings.rs
Cargo.lock
*.png
*.png
/src/bindings.rs
/src/bindings.rs.bk
6 changes: 6 additions & 0 deletions Cargo.toml
Expand Up @@ -6,9 +6,15 @@ edition = "2018"
build = "build.rs"
links = "skia"

[features]
default = []
vulkan = []

[dependencies]

[build-dependencies]
cc = "1"
# temporary, 47.2 will contain the changes that are required to support inline functions on windows.
bindgen = { git = "https://github.com/rust-lang/rust-bindgen", rev = "49b74244f7db8618b9a1b135ce2659751636c59c" }
regex = "1.1"

18 changes: 18 additions & 0 deletions Makefile
@@ -0,0 +1,18 @@
.PHONY: all
all:
echo "take a look at the Makefile"

.PHONY: show-bindings
show-bindings: build
# code will reopen the file while we are formatting it, so
# better use a temporary file.
cp src/bindings.rs /tmp/bindings_inflight.rs
-rustfmt /tmp/bindings_inflight.rs --force
cp /tmp/bindings_inflight.rs /tmp/bindings.rs
rm /tmp/bindings_inflight.rs
code /tmp/bindings.rs

.PHONY: build
build:
cargo build

10 changes: 5 additions & 5 deletions azure-pipelines-template.yml
Expand Up @@ -47,8 +47,8 @@ jobs:
- script: cargo build --release --all-targets -vvv
env: { INIT_SKIA: true }
displayName: Build rust-skia
- script: |
rustup target add wasm32-unknown-unknown
cargo check --target wasm32-unknown-unknown
displayName: Check WebAssembly target
condition: eq(variables['rustup_toolchain'], 'stable')
# - script: |
# rustup target add wasm32-unknown-unknown
# cargo check --target wasm32-unknown-unknown
# displayName: Check WebAssembly target
# condition: eq(variables['rustup_toolchain'], 'stable')
134 changes: 115 additions & 19 deletions build.rs
Expand Up @@ -2,9 +2,12 @@ extern crate bindgen;
extern crate cc;

use std::env;
use std::fs::read_dir;
use std::path::PathBuf;
use std::fs;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use regex::Regex;
use std::slice;

use cc::Build;

Expand Down Expand Up @@ -32,15 +35,44 @@ fn main() {
.status().unwrap().success(), "git sync deps fail");

let gn_args = {
let base_args =

let keep_inline_functions = true;

let mut args =
r#"--args=is_official_build=true skia_use_system_expat=false skia_use_system_icu=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false cc="clang" cxx="clang++""#
.to_owned();

if cfg!(feature="vulkan") {
args.push_str(" skia_use_vulkan=true skia_enable_spirv_validation=false");
}

if cfg!(windows) {
base_args + r#" clang_win="C:\Program Files\LLVM" extra_cflags=["/MD"]"#

let mut flags : Vec<&str> = vec![];
flags.push(if cfg!(build="debug") { "/MTd" } else { "/MD" });

if keep_inline_functions {
// sadly, this also disables inlining completely and is probably a real performance bummer.
flags.push("/Ob0")
};

let flags : String = {
fn quote(s: &str) -> String { String::from("\"") + s + "\"" }

let v : Vec<String> =
flags.into_iter().map(quote).collect();
v.join(",")
};

args.push_str(r#" clang_win="C:\Program Files\LLVM""#);
args.push_str(&format!(" extra_cflags=[{}]", flags));
} else {
base_args
if keep_inline_functions {
args.push_str(r#" extra_cflags=["-fno-inline-functions"]"#)
}
}

args
};

let gn_command = if cfg!(windows) {
Expand Down Expand Up @@ -102,9 +134,39 @@ fn main() {
println!("cargo:rustc-link-lib=usp10");
println!("cargo:rustc-link-lib=ole32");
println!("cargo:rustc-link-lib=user32");

// required since GrContext::MakeVulkan is linked.
if cfg!(feature="vulkan") {
println!("cargo:rustc-link-lib=opengl32");
}
}

if env::var("INIT_SKIA").is_ok() {
// regenerate bindings?
//
// note: the bindings are generated into the src directory to support
// IDE based symbol lookup in dependent projects.

/*
let skia_lib = PathBuf::from(&skia_out_dir).join("skia.lib");
let generated_bindings = PathBuf::from("src/bindings.rs");
let bindings_cpp_src = PathBuf::from("src/bindings.cpp");
let us = PathBuf::from("build.rs");
fn mtime(path: &Path) -> std::time::SystemTime {
fs::metadata(path).unwrap().modified().unwrap()
}
let regenerate_bindings =
!generated_bindings.exists()
|| mtime(&skia_lib) > mtime(&generated_bindings)
|| mtime(&bindings_cpp_src) > mtime(&generated_bindings)
|| mtime(&us) > mtime(&generated_bindings);
*/

let regenerate_bindings = true;

if regenerate_bindings {
bindgen_gen(&current_dir_name, &skia_out_dir)
}
}
Expand All @@ -114,36 +176,70 @@ fn bindgen_gen(current_dir_name: &str, skia_out_dir: &str) {
let mut builder = bindgen::Builder::default()
.generate_inline_functions(true)

.whitelist_function("C_.*")

.rustified_enum("GrSurfaceOrigin")
.rustified_enum("SkColorType")
.rustified_enum("SkPaint_Style")
.rustified_enum("SkPaint_Cap")
.rustified_enum("SkPaint_Join")
.rustified_enum("SkColorSpace_RenderTargetGamma")
.rustified_enum("SkColorSpace_Gamut")

.whitelist_function("SkiaCreateCanvas")
.whitelist_function("SkiaCreateRect")
.whitelist_function("SkiaClearCanvas")
.whitelist_function("SkiaGetSurfaceData")
.whitelist_var("SK_ColorTRANSPARENT")
.whitelist_var("SK_ColorBLACK")
.whitelist_var("SK_ColorDKGRAY")
.whitelist_var("SK_ColorGRAY")
.whitelist_var("SK_ColorLTGRAY")
.whitelist_var("SK_ColorWHITE")
.whitelist_var("SK_ColorRED")
.whitelist_var("SK_ColorGREEN")
.whitelist_var("SK_ColorBLUE")
.whitelist_var("SK_ColorYELLOW")
.whitelist_var("SK_ColorCYAN")
.whitelist_var("SK_ColorMAGENTA")

.whitelist_var("SK_Color.*")
.use_core()
.clang_arg("-std=c++14");

let mut cc_build = Build::new();

builder = builder.header("src/bindings.cpp");

for include_dir in read_dir("skia/include").expect("Unable to read skia/include") {
for include_dir in fs::read_dir("skia/include").expect("Unable to read skia/include") {
let dir = include_dir.unwrap();
let include_path = format!("{}/{}", &current_dir_name, &dir.path().to_str().unwrap());
builder = builder.clang_arg(format!("-I{}", &include_path));
cc_build.include(&include_path);
}

// WIP: extract all the preprocessor definitions ninja was
// using to build skia.

/*
let ninja_config = {
let mut file =
File::open("skia/out/Static/obj/skia.ninja")
.expect("ninja configuration file not found (did skia build?)");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("failed to read ninja configuration file");
contents
};
let defines : String = {
let re = Regex::new("(?m)^defines = (.*)$").unwrap();
let captures =
re.captures(ninja_config.as_str()).unwrap();
captures.get(1).unwrap().as_str().into()
};
*/

if cfg!(feature="vulkan") {
builder = builder
.rustified_enum("VkImageTiling")
.rustified_enum("VkImageLayout")
.rustified_enum("VkFormat");

cc_build.define("SK_VULKAN", "1");
builder = builder.clang_arg("-DSK_VULKAN");
cc_build.define("SKIA_IMPLEMENTATION", "1");
builder = builder.clang_arg("-DSKIA_IMPLEMENTATION=1");
}

cc_build
.cpp(true)
.flag("-std=c++14")
Expand Down
27 changes: 0 additions & 27 deletions examples/hello.rs

This file was deleted.

0 comments on commit e0391ce

Please sign in to comment.