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
use crate::prelude::*; use crate::{Color, Data, Point, Rect}; use skia_bindings as sb; use skia_bindings::{ SkColor, SkPoint, SkVertices, SkVertices_Bone, SkVertices_Builder, SkVertices_VertexMode, }; #[cfg(test)] use skia_bindings::{SkVertices_BoneIndices, SkVertices_BoneWeights}; #[cfg(test)] use std::mem; use std::ops::{Index, IndexMut}; use std::{ptr, slice}; pub type BoneIndices = [u32; 4]; #[test] fn bone_indices_layout() { assert_eq!( mem::size_of::<BoneIndices>(), mem::size_of::<SkVertices_BoneIndices>() ); } pub type BoneWeights = [u32; 4]; #[test] fn bone_weights_layout() { assert_eq!( mem::size_of::<BoneWeights>(), mem::size_of::<SkVertices_BoneWeights>() ); } #[derive(Copy, Clone, PartialEq, Debug)] #[repr(C)] pub struct Bone { values: [f32; 6], } impl NativeTransmutable<SkVertices_Bone> for Bone {} impl Index<usize> for Bone { type Output = f32; fn index(&self, index: usize) -> &Self::Output { &self.values[index] } } impl IndexMut<usize> for Bone { fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut self.values[index] } } impl Bone { pub fn map_point(&self, point: impl Into<Point>) -> Point { let point = point.into(); let values = &self.values; let x = values[0] * point.x + values[2] * point.y + values[4]; let y = values[1] * point.x + values[3] * point.y + values[5]; Point::new(x, y) } pub fn map_rect(&self, rect: impl AsRef<Rect>) -> Rect { Rect::from_native(unsafe { sb::C_SkVertices_Bone_mapRect(self.native(), rect.as_ref().native()) }) } } #[test] fn test_bone_layout() { Bone::test_layout(); } #[derive(Copy, Clone, PartialEq, Eq, Debug)] #[repr(i32)] pub enum VertexMode { Triangles = SkVertices_VertexMode::kTriangles_VertexMode as _, TriangleStrip = SkVertices_VertexMode::kTriangleStrip_VertexMode as _, TriangleFan = SkVertices_VertexMode::kTriangleFan_VertexMode as _, } impl NativeTransmutable<SkVertices_VertexMode> for VertexMode {} #[test] fn test_vertices_vertex_mode_layout() { VertexMode::test_layout() } pub type Vertices = RCHandle<SkVertices>; impl NativeRefCounted for SkVertices { fn _ref(&self) { unsafe { sb::C_SkVertices_ref(self) } } fn _unref(&self) { unsafe { sb::C_SkVertices_unref(self) } } fn unique(&self) -> bool { unsafe { sb::C_SkVertices_unique(self) } } } impl RCHandle<SkVertices> { pub fn new_copy( mode: VertexMode, positions: &[Point], texs: &[Point], colors: &[Color], bone_indices_and_weights: Option<(&[BoneIndices], &[BoneWeights])>, indices: Option<&[u16]>, is_volatile: impl Into<Option<bool>>, ) -> Vertices { let vertex_count = positions.len(); assert_eq!(texs.len(), vertex_count); assert_eq!(colors.len(), vertex_count); if let Some((bi, bw)) = bone_indices_and_weights { assert_eq!(bi.len(), vertex_count); assert_eq!(bw.len(), vertex_count); } let bone_indices = bone_indices_and_weights.map(|t| t.0); let bone_weights = bone_indices_and_weights.map(|t| t.1); let bone_indices_ptr = bone_indices.map(|bi| bi.as_ptr()).unwrap_or(ptr::null()); let bone_weights_ptr = bone_weights.map(|bw| bw.as_ptr()).unwrap_or(ptr::null()); let indices_ptr = indices.map(|i| i.as_ptr()).unwrap_or(ptr::null()); let indices_count = indices.map(|i| i.len()).unwrap_or(0); Vertices::from_ptr(unsafe { sb::C_SkVertices_MakeCopy( mode.into_native(), vertex_count as _, positions.native().as_ptr(), texs.native().as_ptr(), colors.native().as_ptr(), bone_indices_ptr as _, bone_weights_ptr as _, indices_count.try_into().unwrap(), indices_ptr, is_volatile.into().unwrap_or(true), ) }) .unwrap() } pub fn unique_id(&self) -> u32 { self.native().fUniqueID } pub fn mode(&self) -> VertexMode { VertexMode::from_native(self.native().fMode) } pub fn bounds(&self) -> &Rect { Rect::from_native_ref(&self.native().fBounds) } pub fn has_colors(&self) -> bool { self.colors().is_some() } pub fn has_tex_coords(&self) -> bool { self.tex_coords().is_some() } pub fn has_bones(&self) -> bool { self.bone_indices().is_some() } pub fn has_indices(&self) -> bool { self.indices().is_some() } pub fn vertex_count(&self) -> usize { self.native().fVertexCnt.try_into().unwrap() } pub fn positions(&self) -> &[Point] { let positions: *const SkPoint = self.native().fPositions; unsafe { slice::from_raw_parts(positions as _, self.vertex_count()) } } pub fn tex_coords(&self) -> Option<&[Point]> { let texs: *const SkPoint = self.native().fTexs.into_option()?; Some(unsafe { slice::from_raw_parts(texs as _, self.vertex_count()) }) } pub fn colors(&self) -> Option<&[Color]> { let colors: *const SkColor = self.native().fColors.into_option()?; Some(unsafe { slice::from_raw_parts(colors as _, self.vertex_count()) }) } pub fn bone_indices(&self) -> Option<&[BoneIndices]> { let indices = self.native().fBoneIndices.into_option()?; Some(unsafe { slice::from_raw_parts(indices as _, self.vertex_count()) }) } pub fn bone_weights(&self) -> Option<&[BoneWeights]> { let weights = self.native().fBoneWeights.into_option()?; Some(unsafe { slice::from_raw_parts(weights as _, self.vertex_count()) }) } pub fn index_count(&self) -> usize { self.native().fIndexCnt.try_into().unwrap() } pub fn indices(&self) -> Option<&[u16]> { let indices = self.native().fIndices.into_option()?; Some(unsafe { slice::from_raw_parts_mut(indices as _, self.index_count()) }) } pub fn is_volatile(&self) -> bool { self.native().fIsVolatile } pub fn apply_bones(&self, bones: &[Bone]) -> Vertices { Vertices::from_ptr(unsafe { sb::C_SkVertices_applyBones( self.native(), bones.native().as_ptr(), bones.len().try_into().unwrap(), ) }) .unwrap() } pub fn approximate_size(&self) -> usize { unsafe { self.native().approximateSize() } } pub fn decode(buffer: &[u8]) -> Option<Vertices> { Vertices::from_ptr(unsafe { sb::C_SkVertices_Decode(buffer.as_ptr() as _, buffer.len()) }) } pub fn encode(&self) -> Data { Data::from_ptr(unsafe { sb::C_SkVertices_encode(self.native()) }).unwrap() } } bitflags! { pub struct BuilderFlags: u32 { const HAS_TEX_COORDS = sb::SkVertices_BuilderFlags_kHasTexCoords_BuilderFlag as u32; const HAS_COLORS = sb::SkVertices_BuilderFlags_kHasColors_BuilderFlag as u32; const HAS_BONES = sb::SkVertices_BuilderFlags_kHasBones_BuilderFlag as u32; const IS_NON_VOLATILE = sb::SkVertices_BuilderFlags_kIsNonVolatile_BuilderFlag as u32; } } pub type Builder = Handle<SkVertices_Builder>; impl NativeDrop for SkVertices_Builder { fn drop(&mut self) { unsafe { sb::C_SkVertices_Builder_destruct(self) } } } impl Handle<SkVertices_Builder> { pub fn new( mode: VertexMode, vertex_count: usize, index_count: usize, flags: BuilderFlags, ) -> Builder { Self::from_native(unsafe { SkVertices_Builder::new( mode.into_native(), vertex_count.try_into().unwrap(), index_count.try_into().unwrap(), flags.bits(), ) }) } pub fn is_valid(&self) -> bool { !self.native().fVertices.fPtr.is_null() } pub fn vertex_count(&self) -> usize { unsafe { self.native().vertexCount() }.try_into().unwrap() } pub fn index_count(&self) -> usize { unsafe { self.native().indexCount() }.try_into().unwrap() } pub fn is_volatile(&self) -> bool { unsafe { self.native().isVolatile() } } pub fn positions(&mut self) -> &mut [Point] { unsafe { let positions: *mut SkPoint = self.native_mut().positions(); slice::from_raw_parts_mut(positions as _, self.vertex_count()) } } pub fn tex_coords(&mut self) -> Option<&mut [Point]> { unsafe { let coords: *mut SkPoint = self.native_mut().texCoords().into_option()?; Some(slice::from_raw_parts_mut(coords as _, self.vertex_count())) } } pub fn colors(&mut self) -> Option<&mut [Color]> { unsafe { let colors: *mut SkColor = self.native_mut().colors().into_option()?; Some(slice::from_raw_parts_mut(colors as _, self.vertex_count())) } } pub fn bone_indices(&mut self) -> Option<&mut [BoneIndices]> { unsafe { let indices = self.native_mut().boneIndices().into_option()?; Some(slice::from_raw_parts_mut(indices as _, self.vertex_count())) } } pub fn bone_weights(&mut self) -> Option<&mut [BoneWeights]> { unsafe { let weights = self.native_mut().boneWeights().into_option()?; Some(slice::from_raw_parts_mut(weights as _, self.vertex_count())) } } pub fn indices(&mut self) -> Option<&mut [u16]> { unsafe { let indices = self.native_mut().indices().into_option()?; Some(slice::from_raw_parts_mut(indices as _, self.index_count())) } } pub fn detach(mut self) -> Vertices { Vertices::from_ptr(unsafe { sb::C_SkVertices_Builder_detach(self.native_mut()) }).unwrap() } }