summaryrefslogtreecommitdiffstats
path: root/third_party/rust/wpf-gpu-raster/src/c_bindings.rs
blob: 10162872009179f78a01aad8c223320b2eb2b11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::{PathBuilder, OutputPath, OutputVertex, FillMode, rasterize_to_tri_list};
use crate::types::{BYTE, POINT};

#[no_mangle]
pub extern "C" fn wgr_new_builder() -> *mut PathBuilder {
    let pb = PathBuilder::new();
    Box::into_raw(Box::new(pb))
}

#[no_mangle]
pub extern "C" fn wgr_builder_move_to(pb: &mut PathBuilder, x: f32, y: f32) {
    pb.move_to(x, y);
}

#[no_mangle]
pub extern "C" fn wgr_builder_line_to(pb: &mut PathBuilder, x: f32, y: f32) {
    pb.line_to(x, y);
}

#[no_mangle]
pub extern "C" fn wgr_builder_curve_to(pb: &mut PathBuilder, c1x: f32, c1y: f32, c2x: f32, c2y: f32, x: f32, y: f32) {
    pb.curve_to(c1x, c1y, c2x, c2y, x, y);
}

#[no_mangle]
pub extern "C" fn wgr_builder_quad_to(pb: &mut PathBuilder, cx: f32, cy: f32, x: f32, y: f32) {
    pb.quad_to(cx, cy, x, y);
}

#[no_mangle]
pub extern "C" fn wgr_builder_close(pb: &mut PathBuilder) {
    pb.close();
}

#[no_mangle]
pub extern "C" fn wgr_builder_set_fill_mode(pb: &mut PathBuilder, fill_mode: FillMode) {
    pb.set_fill_mode(fill_mode)
}

#[repr(C)]
pub struct Path {
    fill_mode: FillMode,
    points: *const POINT,
    num_points: usize,
    types: *const BYTE,
    num_types: usize,
}

impl From<OutputPath> for Path {
    fn from(output_path: OutputPath) -> Self {
        let path = Self {
            fill_mode: output_path.fill_mode,
            points: output_path.points.as_ptr(),
            num_points: output_path.points.len(),
            types: output_path.types.as_ptr(),
            num_types: output_path.types.len(),
        };
        std::mem::forget(output_path);
        path
    }
}

impl Into<OutputPath> for Path {
    fn into(self) -> OutputPath {
        OutputPath {
            fill_mode: self.fill_mode,
            points: unsafe {
                if self.points == std::ptr::null() {
                    Default::default()
                } else {
                    Box::from_raw(std::slice::from_raw_parts_mut(self.points as *mut POINT, self.num_points))
                }
            },
            types: unsafe {
                if self.types == std::ptr::null() {
                    Default::default()
                } else {
                    Box::from_raw(std::slice::from_raw_parts_mut(self.types as *mut BYTE, self.num_types))
                }
            },
        }
    }
}

#[no_mangle]
pub extern "C" fn wgr_builder_get_path(pb: &mut PathBuilder) -> Path {
    Path::from(pb.get_path().unwrap_or_default())
}

#[repr(C)]
pub struct VertexBuffer {
    data: *const OutputVertex,
    len: usize
}

#[no_mangle]
pub extern "C" fn wgr_path_rasterize_to_tri_list(
    path: &Path,
    clip_x: i32,
    clip_y: i32,
    clip_width: i32,
    clip_height: i32,
    need_inside: bool,
    need_outside: bool,
    rasterization_truncates: bool,
    output_ptr: *mut OutputVertex,
    output_capacity: usize,
) -> VertexBuffer {
    let output_buffer = if output_ptr != std::ptr::null_mut() {
        unsafe { Some(std::slice::from_raw_parts_mut(output_ptr, output_capacity)) }
    } else {
        None
    };
    let mut result = rasterize_to_tri_list(
        path.fill_mode,
        unsafe { std::slice::from_raw_parts(path.types, path.num_types) },
        unsafe { std::slice::from_raw_parts(path.points, path.num_points) },
        clip_x, clip_y, clip_width, clip_height,
        need_inside, need_outside,
        rasterization_truncates,
        output_buffer
    );
    if let Some(output_buffer_size) = result.get_output_buffer_size() {
        VertexBuffer {
            data: std::ptr::null(),
            len: output_buffer_size,
        }
    } else {
        let slice = result.flush_output();
        let vb = VertexBuffer {
            data: slice.as_ptr(),
            len: slice.len(),
        };
        std::mem::forget(slice);
        vb
    }
}

#[no_mangle]
pub extern "C" fn wgr_path_release(path: Path) {
    let output_path: OutputPath = path.into();
    drop(output_path);
}

#[no_mangle]
pub extern "C" fn wgr_vertex_buffer_release(vb: VertexBuffer)
{
    if vb.data != std::ptr::null() {
        unsafe {
            drop(Box::from_raw(std::slice::from_raw_parts_mut(vb.data as *mut OutputVertex, vb.len)));
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn wgr_builder_release(pb: *mut PathBuilder) {
    drop(Box::from_raw(pb));
}