@@ -4,51 +4,96 @@ This repository provides Zig bindings to the Skia C API. It builds Skia for mult
4
4
5
5
## Features
6
6
7
- - Builds Skia for multiple platforms (Linux, macOS, Windows, etc.)
8
- - Exposes the raw Skia C API headers
9
- - Compatible with Zig's ` @cImport ` for easy integration
7
+ - Pre-built Skia binaries
8
+ - Exposes the raw Skia C API headers (exposed throug a simple ` @cImport ` of the Skia headers)
9
+
10
+ ## Project status
11
+ * Warning* : This wrapper is in very early stage and not stable for production use. Also not all
12
+ features and plaforms are ready.
13
+
14
+ [ x] Skia build for Windows x86_64
15
+ [ ] Skia build for macOS x86_64
16
+ [ ] Skia build for macOS Apple Silicon
17
+ [ ] Skia build for Linux
10
18
11
19
## Getting Started
12
20
13
- ### Prerequisites
21
+ ### Usage
14
22
15
- Before building, ensure you have the following dependencies installed:
23
+ 1 . Import the ` skia-zig ` package into your project:
24
+ ``` bash
25
+ zig fetch --save https://github.com/basdp/skia-zig/releases/download/alpha-v1/skia-zig-alpha-v1.zip
26
+ ```
16
27
17
- - Zig (v0.10.0 or higher)
18
- - CMake
19
- - Ninja
20
- - Python 3
21
- - Clang (for compiling Skia)
28
+ 2 . Add the dependency to your ` build.zig ` file, somewhere below ` b.addExecutable(...) ` or whatever you are building:
22
29
23
- ### Building
30
+ ``` zig
31
+ const skia_dep = b.dependency("skia-zig", .{
32
+ .target = target,
33
+ .optimize = optimize,
34
+ });
35
+ exe.root_module.addImport("skia-zig", skia_dep.module("skia-zig"));
36
+ ```
37
+
38
+ 3 . You can now import ` skia-zig ` in your Zig code:
39
+ ``` zig
40
+ const skia = @import("skia-zig");
24
41
25
- To build Skia and set up the bindings for use in Zig, follow these steps:
42
+ pub fn main() !void {
43
+ const gr_glinterface = skia.gr_glinterface_create_native_interface();
44
+ defer skia.gr_glinterface_unref(gr_glinterface);
45
+ const gr_context = skia.gr_direct_context_make_gl(gr_glinterface) orelse return error.SkiaCreateContextFailed;
46
+ defer skia.gr_direct_context_free_gpu_resources(gr_context);
26
47
27
- 1 . Clone the repository:
48
+ const gl_info = skia.gr_gl_framebufferinfo_t{
49
+ .fFBOID = 0,
50
+ .fFormat = gl.RGBA8,
51
+ };
28
52
29
- ``` bash
30
- git clone https://github.com/yourusername/skia-zig-bindings.git
31
- cd skia-zig-bindings
32
- ```
53
+ const samples: = ... // get from GL or something
54
+ const stencil_bits = ... // get from GL or something
33
55
34
- 2. Build Skia for your platform:
56
+ const backendRenderTarget = skia.gr_backendrendertarget_new_gl(640, 480, samples, stencil_bits, &gl_info) orelse return error.SkiaCreateRenderTargetFailed;
35
57
36
- ` ` ` bash
37
- ./build_skia.sh
38
- ` ` `
58
+ const color_type = skia.RGBA_8888_SK_COLORTYPE;
59
+ const colorspace = null;
60
+ const props = null;
61
+ 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;
62
+ defer skia.sk_surface_unref(surface);
39
63
40
- This will download Skia, build it for your platform, and place the compiled libraries and headers in the ` build/ ` directory.
64
+ const canvas = skia.sk_surface_get_canvas(surface) orelse unreachable;
41
65
42
- 3. Link the Skia libraries and include the C headers in your Zig project.
66
+ while (/* app is running */) {
67
+ skia.sk_canvas_clear(canvas, 0xffffffff);
43
68
44
- # ## Usage
69
+ const fill = skia.sk_paint_new() orelse return error.SkiaCreatePaintFailed;
70
+ defer skia.sk_paint_delete(fill);
71
+ skia.sk_paint_set_color(fill, 0xff0000ff);
72
+ skia.sk_canvas_draw_paint(canvas, fill);
45
73
46
- After building, you can import the Skia C API headers in your Zig code as follows:
74
+ // Your Skia drawing here
47
75
48
- ` ` ` zig
49
- const skia = @cImport({
50
- @cInclude(" skia/c/sk_canvas.h" );
51
- @cInclude(" skia/c/sk_paint.h" );
52
- @cInclude(" skia/c/sk_surface.h" );
53
- // Add other Skia headers as needed
54
- });
76
+ skia.sk_canvas_flush(canvas);
77
+ }
78
+ }
79
+ ```
80
+
81
+
82
+ ### Setting your ABI to MSVC on Windows
83
+
84
+ Skia requires a msvc ABI on Windows, so make sure you target that abi. There are two possible
85
+ options to do so:
86
+
87
+ 1 . Set the target from the command line while building:
88
+
89
+ ``` bash
90
+ zig build -Dtarget=x86_64-windows-msvc
91
+ ```
92
+
93
+ 2. Or better yet; replace the ` const target = ...` line in your ` build.zig` file:
94
+
95
+ ` ` ` zig
96
+ const target = b.standardTargetOptions(.{ .default_target = .{
97
+ .abi = if (b.graph.host.result.os.tag == .windows) .msvc else null,
98
+ } });
99
+ ` ` `
0 commit comments