1 | use crate::{prelude::*, Color, Point, Rect}; |
2 | use sb::SkNVRefCnt; |
3 | use skia_bindings::{self as sb, SkPoint, SkVertices, SkVertices_Builder}; |
4 | use std::{fmt, ptr, slice}; |
5 | |
6 | #[deprecated (since = "0.29.0" , note = "removed without replacement" )] |
7 | pub type BoneIndices = [u32; 4]; |
8 | |
9 | #[deprecated (since = "0.29.0" , note = "removed without replacement" )] |
10 | pub type BoneWeights = [u32; 4]; |
11 | |
12 | #[derive (Copy, Clone, PartialEq, Debug)] |
13 | #[repr (C)] |
14 | #[deprecated (since = "0.29.0" , note = "removed without replacement" )] |
15 | pub struct Bone { |
16 | values: [f32; 6], |
17 | } |
18 | |
19 | pub use skia_bindings::SkVertices_VertexMode as VertexMode; |
20 | variant_name!(VertexMode::Triangles); |
21 | |
22 | pub type Vertices = RCHandle<SkVertices>; |
23 | unsafe_send_sync!(Vertices); |
24 | require_base_type!(SkVertices, SkNVRefCnt); |
25 | |
26 | impl NativeRefCounted for SkVertices { |
27 | fn _ref(&self) { |
28 | unsafe { sb::C_SkVertices_ref(self) } |
29 | } |
30 | |
31 | fn _unref(&self) { |
32 | unsafe { sb::C_SkVertices_unref(self) } |
33 | } |
34 | |
35 | fn unique(&self) -> bool { |
36 | unsafe { sb::C_SkVertices_unique(self) } |
37 | } |
38 | } |
39 | |
40 | impl fmt::Debug for Vertices { |
41 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
42 | f&mut DebugStruct<'_, '_>.debug_struct("Vertices" ) |
43 | .field("unique_id" , &self.unique_id()) |
44 | .field("bounds" , &self.bounds()) |
45 | .field(name:"approximate_size" , &self.approximate_size()) |
46 | .finish() |
47 | } |
48 | } |
49 | |
50 | impl Vertices { |
51 | pub fn new_copy( |
52 | mode: VertexMode, |
53 | positions: &[Point], |
54 | texs: &[Point], |
55 | colors: &[Color], |
56 | indices: Option<&[u16]>, |
57 | ) -> Vertices { |
58 | let vertex_count = positions.len(); |
59 | assert_eq!(texs.len(), vertex_count); |
60 | assert_eq!(colors.len(), vertex_count); |
61 | |
62 | let indices_ptr = indices.map(|i| i.as_ptr()).unwrap_or(ptr::null()); |
63 | let indices_count = indices.map(|i| i.len()).unwrap_or(0); |
64 | |
65 | Vertices::from_ptr(unsafe { |
66 | sb::C_SkVertices_MakeCopy( |
67 | mode, |
68 | vertex_count as _, |
69 | positions.native().as_ptr(), |
70 | texs.native().as_ptr(), |
71 | colors.native().as_ptr(), |
72 | indices_count.try_into().unwrap(), |
73 | indices_ptr, |
74 | ) |
75 | }) |
76 | .unwrap() |
77 | } |
78 | |
79 | pub const MAX_CUSTOM_ATTRIBUTES: usize = 8; |
80 | |
81 | pub fn unique_id(&self) -> u32 { |
82 | self.native().fUniqueID |
83 | } |
84 | |
85 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
86 | pub fn mode(&self) -> VertexMode { |
87 | self.native().fMode |
88 | } |
89 | |
90 | pub fn bounds(&self) -> &Rect { |
91 | Rect::from_native_ref(&self.native().fBounds) |
92 | } |
93 | |
94 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
95 | #[allow (deprecated)] |
96 | pub fn has_colors(&self) -> bool { |
97 | self.colors().is_some() |
98 | } |
99 | |
100 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
101 | #[allow (deprecated)] |
102 | pub fn has_tex_coords(&self) -> bool { |
103 | self.tex_coords().is_some() |
104 | } |
105 | |
106 | #[deprecated (since = "0.29.0" , note = "returns false" )] |
107 | pub fn has_bones(&self) -> bool { |
108 | false |
109 | } |
110 | |
111 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
112 | #[allow (deprecated)] |
113 | pub fn has_indices(&self) -> bool { |
114 | self.indices().is_some() |
115 | } |
116 | |
117 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
118 | pub fn vertex_count(&self) -> usize { |
119 | self.native().fVertexCount.try_into().unwrap() |
120 | } |
121 | |
122 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
123 | pub fn index_count(&self) -> usize { |
124 | self.native().fIndexCount.try_into().unwrap() |
125 | } |
126 | |
127 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
128 | #[allow (deprecated)] |
129 | pub fn positions(&self) -> &[Point] { |
130 | let positions: *const SkPoint = self.native().fPositions; |
131 | unsafe { safer::from_raw_parts(positions as _, self.vertex_count()) } |
132 | } |
133 | |
134 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
135 | #[allow (deprecated)] |
136 | pub fn tex_coords(&self) -> Option<&[Point]> { |
137 | let texs = self.native().fTexs.into_option()?; |
138 | Some(unsafe { slice::from_raw_parts(texs.as_ptr() as *const _, self.vertex_count()) }) |
139 | } |
140 | |
141 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
142 | #[allow (deprecated)] |
143 | pub fn colors(&self) -> Option<&[Color]> { |
144 | let colors = self.native().fColors.into_option()?; |
145 | Some(unsafe { slice::from_raw_parts(colors.as_ptr() as *const _, self.vertex_count()) }) |
146 | } |
147 | |
148 | #[deprecated (since = "0.29.0" , note = "returns None" )] |
149 | #[allow (deprecated)] |
150 | pub fn bone_indices(&self) -> Option<&[BoneIndices]> { |
151 | None |
152 | } |
153 | |
154 | #[deprecated (since = "0.29.0" , note = "returns None" )] |
155 | #[allow (deprecated)] |
156 | pub fn bone_weights(&self) -> Option<&[BoneWeights]> { |
157 | None |
158 | } |
159 | |
160 | #[deprecated (since = "0.29.0" , note = "will be removed without replacement" )] |
161 | #[allow (deprecated)] |
162 | pub fn indices(&self) -> Option<&[u16]> { |
163 | let indices = self.native().fIndices.into_option()?; |
164 | Some(unsafe { slice::from_raw_parts_mut(indices.as_ptr(), self.index_count()) }) |
165 | } |
166 | |
167 | #[deprecated (since = "0.29.0" , note = "returns false" )] |
168 | pub fn is_volatile(&self) -> bool { |
169 | false |
170 | } |
171 | |
172 | #[deprecated (since = "0.29.0" , note = "removed without replacement" )] |
173 | #[allow (deprecated)] |
174 | pub fn apply_bones(&self, _bones: &[Bone]) -> ! { |
175 | unimplemented!("removed without replacement" ) |
176 | } |
177 | |
178 | pub fn approximate_size(&self) -> usize { |
179 | unsafe { self.native().approximateSize() } |
180 | } |
181 | |
182 | #[deprecated (since = "0.31.0" , note = "removed without replacement" )] |
183 | pub fn decode(_buffer: &[u8]) -> ! { |
184 | panic!("removed without replacement" ); |
185 | } |
186 | |
187 | #[deprecated (since = "0.31.0" , note = "removed without replacement" )] |
188 | pub fn encode(&self) -> ! { |
189 | panic!("removed without replacement" ); |
190 | } |
191 | } |
192 | |
193 | bitflags! { |
194 | #[derive (Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
195 | pub struct BuilderFlags: u32 |
196 | { |
197 | #[allow(clippy::unnecessary_cast)] |
198 | const HAS_TEX_COORDS = sb::SkVertices_BuilderFlags_kHasTexCoords_BuilderFlag as u32; |
199 | #[allow(clippy::unnecessary_cast)] |
200 | const HAS_COLORS = sb::SkVertices_BuilderFlags_kHasColors_BuilderFlag as u32; |
201 | } |
202 | } |
203 | |
204 | pub type Builder = Handle<SkVertices_Builder>; |
205 | unsafe_send_sync!(Builder); |
206 | |
207 | impl NativeDrop for SkVertices_Builder { |
208 | fn drop(&mut self) { |
209 | unsafe { sb::C_SkVertices_Builder_destruct(self) } |
210 | } |
211 | } |
212 | |
213 | impl fmt::Debug for Builder { |
214 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
215 | f.debug_struct(name:"Builder" ).finish() |
216 | } |
217 | } |
218 | |
219 | impl Builder { |
220 | pub fn new( |
221 | mode: VertexMode, |
222 | vertex_count: usize, |
223 | index_count: usize, |
224 | flags: BuilderFlags, |
225 | ) -> Builder { |
226 | let r = Self::from_native_c(unsafe { |
227 | SkVertices_Builder::new( |
228 | mode, |
229 | vertex_count.try_into().unwrap(), |
230 | index_count.try_into().unwrap(), |
231 | flags.bits(), |
232 | ) |
233 | }); |
234 | |
235 | assert!(!r.native().fVertices.fPtr.is_null()); |
236 | r |
237 | } |
238 | |
239 | pub fn positions(&mut self) -> &mut [Point] { |
240 | unsafe { |
241 | let vertices = &*self.native().fVertices.fPtr; |
242 | safer::from_raw_parts_mut( |
243 | Point::from_native_ptr_mut(vertices.fPositions), |
244 | vertices.fVertexCount.try_into().unwrap(), |
245 | ) |
246 | } |
247 | } |
248 | |
249 | pub fn indices(&mut self) -> Option<&mut [u16]> { |
250 | unsafe { |
251 | let vertices = &*self.native().fVertices.fPtr; |
252 | let indices = vertices.fIndices.into_option()?; |
253 | Some(slice::from_raw_parts_mut( |
254 | indices.as_ptr(), |
255 | vertices.fIndexCount.try_into().unwrap(), |
256 | )) |
257 | } |
258 | } |
259 | |
260 | pub fn tex_coords(&mut self) -> Option<&mut [Point]> { |
261 | unsafe { |
262 | let vertices = &*self.native().fVertices.fPtr; |
263 | let mut coords = vertices.fTexs.into_option()?; |
264 | Some(slice::from_raw_parts_mut( |
265 | Point::from_native_ref_mut(coords.as_mut()), |
266 | vertices.fVertexCount.try_into().unwrap(), |
267 | )) |
268 | } |
269 | } |
270 | |
271 | pub fn colors(&mut self) -> Option<&mut [Color]> { |
272 | unsafe { |
273 | let vertices = &*self.native().fVertices.fPtr; |
274 | let mut colors = vertices.fColors.into_option()?; |
275 | Some(slice::from_raw_parts_mut( |
276 | Color::from_native_ref_mut(colors.as_mut()), |
277 | vertices.fVertexCount.try_into().unwrap(), |
278 | )) |
279 | } |
280 | } |
281 | |
282 | #[deprecated (since = "0.29.0" , note = "returns false" )] |
283 | pub fn is_volatile(&self) -> bool { |
284 | false |
285 | } |
286 | |
287 | #[deprecated (since = "0.29.0" , note = "returns None" )] |
288 | #[allow (deprecated)] |
289 | pub fn bone_indices(&mut self) -> Option<&mut [BoneIndices]> { |
290 | None |
291 | } |
292 | |
293 | #[deprecated (since = "0.29.0" , note = "returns None" )] |
294 | #[allow (deprecated)] |
295 | pub fn bone_weights(&mut self) -> Option<&mut [BoneWeights]> { |
296 | None |
297 | } |
298 | |
299 | pub fn detach(mut self) -> Vertices { |
300 | Vertices::from_ptr(unsafe { sb::C_SkVertices_Builder_detach(self.native_mut()) }).unwrap() |
301 | } |
302 | } |
303 | |