Skip to content

Commit

Permalink
readme changes
Browse files Browse the repository at this point in the history
  • Loading branch information
basdp committed Oct 9, 2024
1 parent 1ab11e6 commit 21a7760
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

steps:
- uses: actions/checkout@v4

- uses: msys2/setup-msys2@v2
id: msys2
with:
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
mkdir -p ../skia/lib/win-x64
cp --recursive include ../skia/include
cp --recursive out/lib/*.lib ../skia/lib/win-x64/
cp --recursive out/lib/*.lib ../skia/lib/win-x86_64/
cd ..
rm -rf skia_repo
Expand Down
109 changes: 77 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,96 @@ This repository provides Zig bindings to the Skia C API. It builds Skia for mult

## Features

- Builds Skia for multiple platforms (Linux, macOS, Windows, etc.)
- Exposes the raw Skia C API headers
- Compatible with Zig's `@cImport` for easy integration
- Pre-built Skia binaries
- Exposes the raw Skia C API headers (exposed throug a simple `@cImport` of the Skia headers)

## Project status
*Warning*: This wrapper is in very early stage and not stable for production use. Also not all
features and plaforms are ready.

[x] Skia build for Windows x86_64
[ ] Skia build for macOS x86_64
[ ] Skia build for macOS Apple Silicon
[ ] Skia build for Linux

## Getting Started

### Prerequisites
### Usage

Before building, ensure you have the following dependencies installed:
1. Import the `skia-zig` package into your project:
```bash
zig fetch --save https://github.com/basdp/skia-zig/releases/download/alpha-v1/skia-zig-alpha-v1.zip
```

- Zig (v0.10.0 or higher)
- CMake
- Ninja
- Python 3
- Clang (for compiling Skia)
2. Add the dependency to your `build.zig` file, somewhere below `b.addExecutable(...)` or whatever you are building:

### Building
```zig
const skia_dep = b.dependency("skia-zig", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("skia-zig", skia_dep.module("skia-zig"));
```

3. You can now import `skia-zig` in your Zig code:
```zig
const skia = @import("skia-zig");
To build Skia and set up the bindings for use in Zig, follow these steps:
pub fn main() !void {
const gr_glinterface = skia.gr_glinterface_create_native_interface();
defer skia.gr_glinterface_unref(gr_glinterface);
const gr_context = skia.gr_direct_context_make_gl(gr_glinterface) orelse return error.SkiaCreateContextFailed;
defer skia.gr_direct_context_free_gpu_resources(gr_context);
1. Clone the repository:
const gl_info = skia.gr_gl_framebufferinfo_t{
.fFBOID = 0,
.fFormat = gl.RGBA8,
};
```bash
git clone https://github.com/yourusername/skia-zig-bindings.git
cd skia-zig-bindings
```
const samples: = ... // get from GL or something
const stencil_bits = ... // get from GL or something
2. Build Skia for your platform:
const backendRenderTarget = skia.gr_backendrendertarget_new_gl(640, 480, samples, stencil_bits, &gl_info) orelse return error.SkiaCreateRenderTargetFailed;
```bash
./build_skia.sh
```
const color_type = skia.RGBA_8888_SK_COLORTYPE;
const colorspace = null;
const props = null;
const surface = skia.sk_surface_new_backend_render_target(@ptrCast(gr_context), backendRenderTarget, skia.BOTTOM_LEFT_GR_SURFACE_ORIGIN, color_type, colorspace, props) orelse return error.SkiaCreateSurfaceFailed;
defer skia.sk_surface_unref(surface);
This will download Skia, build it for your platform, and place the compiled libraries and headers in the `build/` directory.
const canvas = skia.sk_surface_get_canvas(surface) orelse unreachable;
3. Link the Skia libraries and include the C headers in your Zig project.
while (/* app is running */) {
skia.sk_canvas_clear(canvas, 0xffffffff);
### Usage
const fill = skia.sk_paint_new() orelse return error.SkiaCreatePaintFailed;
defer skia.sk_paint_delete(fill);
skia.sk_paint_set_color(fill, 0xff0000ff);
skia.sk_canvas_draw_paint(canvas, fill);
After building, you can import the Skia C API headers in your Zig code as follows:
// Your Skia drawing here
```zig
const skia = @cImport({
@cInclude("skia/c/sk_canvas.h");
@cInclude("skia/c/sk_paint.h");
@cInclude("skia/c/sk_surface.h");
// Add other Skia headers as needed
});
skia.sk_canvas_flush(canvas);
}
}
```


### Setting your ABI to MSVC on Windows

Skia requires a msvc ABI on Windows, so make sure you target that abi. There are two possible
options to do so:

1. Set the target from the command line while building:

```bash
zig build -Dtarget=x86_64-windows-msvc
```

2. Or better yet; replace the `const target = ...` line in your `build.zig` file:

```zig
const target = b.standardTargetOptions(.{ .default_target = .{
.abi = if (b.graph.host.result.os.tag == .windows) .msvc else null,
} });
```

0 comments on commit 21a7760

Please sign in to comment.