skia_safe/utils/
camera.rs

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
#![allow(deprecated)]
use crate::{prelude::*, scalar, Canvas, Matrix, M44, V3};
use skia_bindings::{self as sb, Sk3DView, SkCamera3D, SkPatch3D};
use std::fmt;

#[deprecated(
    since = "0.30.0",
    note = "Skia now has support for a 4x matrix (core::M44) in core::Canvas."
)]
#[derive(Clone, PartialEq, Debug)]
#[repr(C)]
pub struct Patch3D {
    pub u: V3,
    pub v: V3,
    pub origin: V3,
}

native_transmutable!(SkPatch3D, Patch3D, patch_3d_layout);

impl Default for Patch3D {
    fn default() -> Self {
        Patch3D::from_native_c(unsafe { SkPatch3D::new() })
    }
}

impl Patch3D {
    pub fn reset(&mut self) -> &mut Self {
        unsafe { self.native_mut().reset() }
        self
    }

    #[must_use]
    pub fn transform(&self, m: &M44) -> Self {
        let mut dst = Patch3D::default();
        unsafe { self.native().transform(m.native(), dst.native_mut()) }
        dst
    }

    pub fn dot_with(&self, v: impl Into<V3>) -> scalar {
        let v = v.into();
        unsafe { self.native().dotWith(v.x, v.y, v.z) }
    }
}

#[deprecated(
    since = "0.30.0",
    note = "Skia now has support for a 4x matrix (core::M44) in core::Canvas."
)]
#[derive(Clone, PartialEq, Debug)]
#[repr(C)]
pub struct Camera3D {
    pub location: V3,
    pub axis: V3,
    pub zenith: V3,
    pub observer: V3,
    orientation: Matrix,
    need_to_update: bool,
}

native_transmutable!(SkCamera3D, Camera3D, camera_3d_layout);

impl Default for Camera3D {
    fn default() -> Self {
        Camera3D::from_native_c(unsafe { SkCamera3D::new() })
    }
}

impl Camera3D {
    pub fn reset(&mut self) -> &mut Self {
        unsafe { self.native_mut().reset() }
        self
    }

    pub fn update(&mut self) -> &mut Self {
        unsafe { self.native_mut().update() }
        self
    }

    pub fn patch_to_matrix(&self, quilt: &Patch3D) -> Matrix {
        let mut matrix = Matrix::default();
        unsafe {
            self.native()
                .patchToMatrix(quilt.native(), matrix.native_mut())
        }
        matrix
    }
}

// Note: original name is Sk3DView not SkView3D
// Also note that the implementation uses interior pointers,
// so we let Skia do the allocation.

#[deprecated(
    since = "0.30.0",
    note = "Skia now has support for a 4x matrix (core::M44) in core::Canvas."
)]
pub type View3D = RefHandle<Sk3DView>;
unsafe_send_sync!(View3D);

impl Default for View3D {
    fn default() -> Self {
        Self::new()
    }
}

impl NativeDrop for Sk3DView {
    fn drop(&mut self) {
        unsafe { sb::C_Sk3DView_delete(self) }
    }
}

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

impl RefHandle<Sk3DView> {
    pub fn new() -> Self {
        View3D::from_ptr(unsafe { sb::C_Sk3DView_new() }).unwrap()
    }

    pub fn save(&mut self) -> &mut Self {
        unsafe { self.native_mut().save() }
        self
    }

    pub fn restore(&mut self) -> &mut Self {
        unsafe { self.native_mut().restore() }
        self
    }

    pub fn translate(&mut self, d: impl Into<V3>) -> &mut Self {
        let d = d.into();
        unsafe { self.native_mut().translate(d.x, d.y, d.z) }
        self
    }

    pub fn rotate_x(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().rotateX(deg) }
        self
    }

    pub fn rotate_y(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().rotateY(deg) }
        self
    }

    pub fn rotate_z(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().rotateZ(deg) }
        self
    }

    pub fn matrix(&self) -> Matrix {
        let mut m = Matrix::default();
        unsafe { self.native().getMatrix(m.native_mut()) }
        m
    }

    pub fn apply_to_canvas(&self, canvas: &Canvas) -> &Self {
        unsafe { self.native().applyToCanvas(canvas.native_mut()) }
        self
    }

    pub fn dot_with_normal(&self, d: impl Into<V3>) -> scalar {
        let d = d.into();
        unsafe { self.native().dotWithNormal(d.x, d.y, d.z) }
    }
}

#[test]
fn test_canvas_passing_syntax() {
    use crate::utils::new_null_canvas;
    use crate::Surface;

    let null_canvas = new_null_canvas();
    let view = View3D::default();
    // as mutable reference
    view.apply_to_canvas(&null_canvas);

    // and one with a mutable reference to a shared Canvas:
    let mut surface = Surface::new_raster_n32_premul((100, 100)).unwrap();
    view.apply_to_canvas(surface.canvas());
}