thorvg 0.1.2

Safe Rust bindings to the ThorVG vector graphics library
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use crate::error::{Error, Result};
use crate::paint::Paint;
use thorvg_sys as ffi;

/// Color space for the rendering buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ColorSpace {
    /// Alpha, Blue, Green, Red (premultiplied alpha).
    ABGR8888,
    /// Alpha, Red, Green, Blue (premultiplied alpha).
    ARGB8888,
    /// Alpha, Blue, Green, Red (straight alpha).
    ABGR8888S,
    /// Alpha, Red, Green, Blue (straight alpha).
    ARGB8888S,
}

impl ColorSpace {
    pub(crate) fn to_raw(self) -> ffi::Tvg_Colorspace {
        match self {
            ColorSpace::ABGR8888 => ffi::Tvg_Colorspace::TVG_COLORSPACE_ABGR8888,
            ColorSpace::ARGB8888 => ffi::Tvg_Colorspace::TVG_COLORSPACE_ARGB8888,
            ColorSpace::ABGR8888S => ffi::Tvg_Colorspace::TVG_COLORSPACE_ABGR8888S,
            ColorSpace::ARGB8888S => ffi::Tvg_Colorspace::TVG_COLORSPACE_ARGB8888S,
        }
    }
}

/// Engine rendering options.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum EngineOption {
    /// No options.
    None,
    /// Default rendering mode.
    #[default]
    Default,
    /// Enable smart (partial) rendering.
    SmartRender,
}

impl EngineOption {
    pub(crate) fn to_raw(self) -> ffi::Tvg_Engine_Option {
        match self {
            EngineOption::None => ffi::Tvg_Engine_Option::TVG_ENGINE_OPTION_NONE,
            EngineOption::Default => ffi::Tvg_Engine_Option::TVG_ENGINE_OPTION_DEFAULT,
            EngineOption::SmartRender => ffi::Tvg_Engine_Option::TVG_ENGINE_OPTION_SMART_RENDER,
        }
    }
}

/// A software-rendered canvas.
///
/// This canvas uses the CPU engine for rendering.
///
/// The lifetime `'eng` ties this canvas to a [`Thorvg`](crate::Thorvg) engine
/// instance. Create canvases via [`Thorvg::sw_canvas()`](crate::Thorvg::sw_canvas).
///
/// # Thread Safety
///
/// `SwCanvas` is [`Send`] but not [`Sync`]: you may move it to another
/// thread, but you must not share references across threads.  All
/// mutation goes through `&mut self`, so the borrow checker enforces
/// exclusive access at compile time.
pub struct SwCanvas<'eng> {
    raw: ffi::Tvg_Canvas,
    _engine: core::marker::PhantomData<&'eng ()>,
}

// SAFETY: `SwCanvas` exclusively owns a heap-allocated ThorVG canvas
// (`Tvg_Canvas`).  The C++ implementation guards shared global state
// (renderer ref-count, memory pool, loader registry) with internal
// mutexes (`_rendererMtx`, `ScopedLock` / `StrictKey`), and per-canvas
// state is only accessed through `&mut self`.  Transferring sole
// ownership to another thread is therefore safe.
//
// `SwCanvas` is intentionally `!Sync`: the raw pointer field prevents
// the auto-`Sync` impl, which is correct — concurrent `&`-access to
// the same C handle would be a data race.
unsafe impl Send for SwCanvas<'_> {}

impl SwCanvas<'_> {
    /// Creates a new software canvas with the given engine options.
    pub(crate) fn new(option: EngineOption) -> Result<Self> {
        let raw = unsafe { ffi::tvg_swcanvas_create(option.to_raw()) };
        if raw.is_null() {
            return Err(Error::Unknown);
        }
        Ok(Self {
            raw,
            _engine: core::marker::PhantomData,
        })
    }

    /// Sets the rendering target buffer.
    ///
    /// The buffer must be at least `stride * height` elements.
    ///
    /// # Safety
    /// The caller must ensure that `buffer` remains valid and is not moved,
    /// reallocated, or dropped for the entire lifetime of the canvas (or until
    /// `set_target` is called again with a different buffer). The canvas stores
    /// the pointer internally and writes to it during [`draw`](Self::draw) and
    /// [`sync`](Self::sync).
    pub unsafe fn set_target(
        &mut self,
        buffer: &mut [u32],
        stride: u32,
        width: u32,
        height: u32,
        colorspace: ColorSpace,
    ) -> Result<()> {
        assert!(
            buffer.len() >= (stride * height) as usize,
            "buffer too small: need {} elements, got {}",
            stride * height,
            buffer.len()
        );
        let result = unsafe {
            ffi::tvg_swcanvas_set_target(
                self.raw,
                buffer.as_mut_ptr(),
                stride,
                width,
                height,
                colorspace.to_raw(),
            )
        };
        Error::from_raw(result)
    }

    /// Adds a paint object to the canvas for rendering.
    ///
    /// Ownership of the paint is transferred to the canvas.
    pub fn push<P: Paint>(&mut self, paint: P) -> Result<()> {
        let raw_paint = paint.into_raw();
        let result = unsafe { ffi::tvg_canvas_add(self.raw, raw_paint) };
        Error::from_raw(result)
    }

    /// Inserts a paint object before another existing paint in the canvas.
    ///
    /// Ownership of `target` is transferred to the canvas.
    pub fn insert<P: Paint, Q: Paint>(&mut self, target: P, at: &Q) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_insert(self.raw, target.into_raw(), at.raw()) };
        Error::from_raw(result)
    }

    /// Removes a paint object from the canvas.
    pub fn remove<P: Paint>(&mut self, paint: &P) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_remove(self.raw, paint.raw()) };
        Error::from_raw(result)
    }

    /// Removes all paint objects from the canvas.
    pub fn clear(&mut self) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_remove(self.raw, core::ptr::null_mut()) };
        Error::from_raw(result)
    }

    /// Updates all modified paint objects in preparation for rendering.
    pub fn update(&mut self) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_update(self.raw) };
        Error::from_raw(result)
    }

    /// Renders all paint objects on the canvas.
    ///
    /// If `clear` is true, the target buffer is cleared before drawing.
    pub fn draw(&mut self, clear: bool) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_draw(self.raw, clear) };
        Error::from_raw(result)
    }

    /// Waits for the rendering to finish.
    pub fn sync(&mut self) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_sync(self.raw) };
        Error::from_raw(result)
    }

    /// Sets the drawing viewport (clipping region).
    pub fn set_viewport(&mut self, x: i32, y: i32, w: i32, h: i32) -> Result<()> {
        let result = unsafe { ffi::tvg_canvas_set_viewport(self.raw, x, y, w, h) };
        Error::from_raw(result)
    }

    /// Update, draw (clearing the buffer), and sync in one call.
    ///
    /// Equivalent to calling [`update`](Self::update), [`draw(true)`](Self::draw),
    /// and [`sync`](Self::sync) in sequence.
    pub fn render(&mut self) -> Result<()> {
        self.update()?;
        self.draw(true)?;
        self.sync()
    }
}

impl Drop for SwCanvas<'_> {
    fn drop(&mut self) {
        unsafe {
            ffi::tvg_canvas_destroy(self.raw);
        }
    }
}

// ── Shared canvas operations ───────────────────────────────────────

macro_rules! impl_canvas_ops {
    ($ty:ident) => {
        impl $ty<'_> {
            /// Adds a paint object to the canvas for rendering.
            ///
            /// Ownership of the paint is transferred to the canvas.
            pub fn push<P: Paint>(&mut self, paint: P) -> Result<()> {
                let raw_paint = paint.into_raw();
                Error::from_raw(unsafe { ffi::tvg_canvas_add(self.raw, raw_paint) })
            }

            /// Inserts a paint object before another existing paint in the canvas.
            pub fn insert<P: Paint, Q: Paint>(&mut self, target: P, at: &Q) -> Result<()> {
                Error::from_raw(unsafe {
                    ffi::tvg_canvas_insert(self.raw, target.into_raw(), at.raw())
                })
            }

            /// Removes a paint object from the canvas.
            pub fn remove<P: Paint>(&mut self, paint: &P) -> Result<()> {
                Error::from_raw(unsafe { ffi::tvg_canvas_remove(self.raw, paint.raw()) })
            }

            /// Removes all paint objects from the canvas.
            pub fn clear(&mut self) -> Result<()> {
                Error::from_raw(unsafe { ffi::tvg_canvas_remove(self.raw, core::ptr::null_mut()) })
            }

            /// Updates all modified paint objects in preparation for rendering.
            pub fn update(&mut self) -> Result<()> {
                Error::from_raw(unsafe { ffi::tvg_canvas_update(self.raw) })
            }

            /// Renders all paint objects on the canvas.
            pub fn draw(&mut self, clear: bool) -> Result<()> {
                Error::from_raw(unsafe { ffi::tvg_canvas_draw(self.raw, clear) })
            }

            /// Waits for the rendering to finish.
            pub fn sync(&mut self) -> Result<()> {
                Error::from_raw(unsafe { ffi::tvg_canvas_sync(self.raw) })
            }

            /// Sets the drawing viewport (clipping region).
            pub fn set_viewport(&mut self, x: i32, y: i32, w: i32, h: i32) -> Result<()> {
                Error::from_raw(unsafe { ffi::tvg_canvas_set_viewport(self.raw, x, y, w, h) })
            }

            /// Update, draw (clearing the buffer), and sync in one call.
            ///
            /// Equivalent to calling [`update`](Self::update), [`draw(true)`](Self::draw),
            /// and [`sync`](Self::sync) in sequence.
            pub fn render(&mut self) -> Result<()> {
                self.update()?;
                self.draw(true)?;
                self.sync()
            }
        }

        impl Drop for $ty<'_> {
            fn drop(&mut self) {
                unsafe {
                    ffi::tvg_canvas_destroy(self.raw);
                }
            }
        }
    };
}

// ── GlCanvas ───────────────────────────────────────────────────────

/// An OpenGL/ES-rendered canvas.
///
/// The lifetime `'eng` ties this canvas to a [`Thorvg`](crate::Thorvg) engine
/// instance. Create canvases via [`Thorvg::gl_canvas()`](crate::Thorvg::gl_canvas).
///
/// # Thread Safety
///
/// `GlCanvas` is [`Send`] but not [`Sync`]: you may move it to another
/// thread, but you must not share references across threads.
pub struct GlCanvas<'eng> {
    raw: ffi::Tvg_Canvas,
    _engine: core::marker::PhantomData<&'eng ()>,
}

// SAFETY: same rationale as `SwCanvas` — exclusive ownership of a
// heap-allocated C handle; global state is mutex-protected.
unsafe impl Send for GlCanvas<'_> {}

impl GlCanvas<'_> {
    /// Creates a new OpenGL canvas with the given engine options.
    pub(crate) fn new(option: EngineOption) -> Result<Self> {
        let raw = unsafe { ffi::tvg_glcanvas_create(option.to_raw()) };
        if raw.is_null() {
            return Err(Error::Unknown);
        }
        Ok(Self {
            raw,
            _engine: core::marker::PhantomData,
        })
    }

    /// Sets the OpenGL drawing target.
    ///
    /// - `display` — platform-specific display handle (`EGLDisplay`), or `null` for non-EGL.
    /// - `surface` — platform-specific surface handle (`EGLSurface` / `HDC`), or `null`.
    /// - `context` — the OpenGL context for rendering.
    /// - `id` — GL target ID (FBO ID), `0` for the main surface.
    /// - `w`, `h` — dimensions in pixels.
    /// - `colorspace` — pixel format (currently only `ABGR8888S` as `GL_RGBA8`).
    ///
    /// # Safety
    /// The caller must ensure the provided pointers are valid GL/EGL handles.
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn set_target(
        &mut self,
        display: *mut core::ffi::c_void,
        surface: *mut core::ffi::c_void,
        context: *mut core::ffi::c_void,
        id: i32,
        w: u32,
        h: u32,
        colorspace: ColorSpace,
    ) -> Result<()> {
        Error::from_raw(unsafe {
            ffi::tvg_glcanvas_set_target(
                self.raw,
                display,
                surface,
                context,
                id,
                w,
                h,
                colorspace.to_raw(),
            )
        })
    }
}

impl_canvas_ops!(GlCanvas);

// ── WgCanvas ───────────────────────────────────────────────────────

/// WebGPU target type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum WgTargetType {
    /// Use a `WGPUSurface` as the presentable target.
    Surface = 0,
    /// Use a `WGPUTexture` as the presentable target.
    Texture = 1,
}

/// A WebGPU-rendered canvas.
///
/// The lifetime `'eng` ties this canvas to a [`Thorvg`](crate::Thorvg) engine
/// instance. Create canvases via [`Thorvg::wg_canvas()`](crate::Thorvg::wg_canvas).
///
/// # Thread Safety
///
/// `WgCanvas` is [`Send`] but not [`Sync`]: you may move it to another
/// thread, but you must not share references across threads.
pub struct WgCanvas<'eng> {
    raw: ffi::Tvg_Canvas,
    _engine: core::marker::PhantomData<&'eng ()>,
}

// SAFETY: same rationale as `SwCanvas` — exclusive ownership of a
// heap-allocated C handle; global state is mutex-protected.
unsafe impl Send for WgCanvas<'_> {}

impl WgCanvas<'_> {
    /// Creates a new WebGPU canvas with the given engine options.
    pub(crate) fn new(option: EngineOption) -> Result<Self> {
        let raw = unsafe { ffi::tvg_wgcanvas_create(option.to_raw()) };
        if raw.is_null() {
            return Err(Error::Unknown);
        }
        Ok(Self {
            raw,
            _engine: core::marker::PhantomData,
        })
    }

    /// Sets the WebGPU drawing target.
    ///
    /// - `device` — `WGPUDevice` handle, or `null` to let `ThorVG` assign one.
    /// - `instance` — `WGPUInstance` context.
    /// - `target` — `WGPUSurface` or `WGPUTexture` handle.
    /// - `w`, `h` — dimensions.
    /// - `colorspace` — pixel format (currently only `ABGR8888S` as `RGBA8Unorm`).
    /// - `target_type` — whether `target` is a surface or texture.
    ///
    /// # Safety
    /// The caller must ensure the provided pointers are valid WebGPU handles.
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn set_target(
        &mut self,
        device: *mut core::ffi::c_void,
        instance: *mut core::ffi::c_void,
        target: *mut core::ffi::c_void,
        w: u32,
        h: u32,
        colorspace: ColorSpace,
        target_type: WgTargetType,
    ) -> Result<()> {
        Error::from_raw(unsafe {
            ffi::tvg_wgcanvas_set_target(
                self.raw,
                device,
                instance,
                target,
                w,
                h,
                colorspace.to_raw(),
                target_type as i32,
            )
        })
    }
}

impl_canvas_ops!(WgCanvas);

impl core::fmt::Debug for SwCanvas<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SwCanvas").finish_non_exhaustive()
    }
}

impl core::fmt::Debug for GlCanvas<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("GlCanvas").finish_non_exhaustive()
    }
}

impl core::fmt::Debug for WgCanvas<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("WgCanvas").finish_non_exhaustive()
    }
}