Skip to main content

skia_bindings/
impls.rs

1//! This file contains implementations for types that are re-exported in skia-safe.
2//!
3//! We could provide trait implementations in skia-safe, but then users of the library would have to
4//! import the implementation type _and_ the trait.
5//!
6//! See also: <https://github.com/rust-lang/rfcs/issues/1880>
7
8use crate::{
9    SkAlphaType, SkBlendMode, SkBlendModeCoeff, SkPathFillType, SkPathVerb, SkPath_Verb,
10    SkYUVColorSpace,
11};
12use std::ffi::CStr;
13
14impl SkBlendMode {
15    pub fn as_coeff(self) -> Option<(SkBlendModeCoeff, SkBlendModeCoeff)> {
16        let mut src = SkBlendModeCoeff::Zero;
17        let mut dst = SkBlendModeCoeff::Zero;
18        if unsafe { crate::SkBlendMode_AsCoeff(self, &mut src, &mut dst) } {
19            Some((src, dst))
20        } else {
21            None
22        }
23    }
24
25    pub fn name(self) -> &'static str {
26        unsafe {
27            let name_ptr = crate::SkBlendMode_Name(self);
28            CStr::from_ptr(name_ptr).to_str().unwrap()
29        }
30    }
31}
32
33//
34// m84 introduced two different variants of the Path verb types.
35// One with Done and one without.
36//
37
38impl SkPathVerb {
39    /// The maximum number of points an iterator will return for the verb.
40    pub const MAX_POINTS: usize = SkPath_Verb::MAX_POINTS;
41    /// The number of points an iterator will return for the verb.
42    pub fn points(self) -> usize {
43        SkPath_Verb::from(self).points()
44    }
45}
46
47impl SkPath_Verb {
48    /// The maximum number of points an iterator will return for the verb.
49    pub const MAX_POINTS: usize = 4;
50    /// The number of points an iterator will return for the verb.
51    pub fn points(self) -> usize {
52        match self {
53            SkPath_Verb::Move => 1,
54            SkPath_Verb::Line => 2,
55            SkPath_Verb::Quad => 3,
56            SkPath_Verb::Conic => 3,
57            SkPath_Verb::Cubic => 4,
58            SkPath_Verb::Close => 0,
59            SkPath_Verb::Done => 0,
60        }
61    }
62}
63
64impl From<SkPathVerb> for SkPath_Verb {
65    fn from(v: SkPathVerb) -> Self {
66        match v {
67            SkPathVerb::Move => SkPath_Verb::Move,
68            SkPathVerb::Line => SkPath_Verb::Line,
69            SkPathVerb::Quad => SkPath_Verb::Quad,
70            SkPathVerb::Conic => SkPath_Verb::Conic,
71            SkPathVerb::Cubic => SkPath_Verb::Cubic,
72            SkPathVerb::Close => SkPath_Verb::Close,
73        }
74    }
75}
76
77impl SkPathFillType {
78    pub fn is_even_odd(self) -> bool {
79        (self as i32 & 1) != 0
80    }
81
82    pub fn is_inverse(self) -> bool {
83        (self as i32 & 2) != 0
84    }
85
86    #[must_use]
87    pub fn to_non_inverse(self) -> Self {
88        match self {
89            Self::Winding => self,
90            Self::EvenOdd => self,
91            Self::InverseWinding => Self::Winding,
92            Self::InverseEvenOdd => Self::EvenOdd,
93        }
94    }
95
96    pub fn toggle_inverse(self) -> Self {
97        match self {
98            Self::Winding => Self::InverseWinding,
99            Self::EvenOdd => Self::InverseEvenOdd,
100            Self::InverseWinding => Self::Winding,
101            Self::InverseEvenOdd => Self::EvenOdd,
102        }
103    }
104}
105
106impl SkAlphaType {
107    pub fn is_opaque(self) -> bool {
108        self == SkAlphaType::Opaque
109    }
110}
111
112impl SkYUVColorSpace {
113    pub fn is_limited_range(self) -> bool {
114        unsafe { crate::SkYUVColorSpaceIsLimitedRange(self) }
115    }
116}
117
118#[cfg(feature = "gl")]
119impl From<crate::GrGLenum> for crate::GrGLFormat {
120    fn from(e: crate::GrGLenum) -> Self {
121        unsafe { crate::C_GrGLFormatFromGLEnum(e) }
122    }
123}
124
125#[cfg(feature = "gl")]
126impl From<crate::GrGLFormat> for crate::GrGLenum {
127    fn from(format: crate::GrGLFormat) -> Self {
128        unsafe { crate::C_GrGLFormatToEnum(format) }
129    }
130}
131
132#[cfg(feature = "vulkan")]
133mod vulkan {
134    impl PartialEq for crate::VkComponentMapping {
135        fn eq(&self, other: &Self) -> bool {
136            self.r == other.r && self.g == other.g && self.b == other.b && self.a == other.a
137        }
138    }
139
140    impl Eq for crate::VkComponentMapping {}
141}
142
143#[cfg(feature = "d3d")]
144mod d3d {
145    use std::marker::PhantomData;
146
147    impl<T> Default for crate::gr_cp<T> {
148        fn default() -> Self {
149            Self {
150                fObject: std::ptr::null_mut(),
151                _phantom_0: PhantomData,
152            }
153        }
154    }
155
156    impl Default for crate::GrD3DTextureResourceInfo {
157        fn default() -> Self {
158            let mut instance = std::mem::MaybeUninit::uninit();
159            unsafe {
160                crate::C_GrD3DTextureResourceInfo_Construct(instance.as_mut_ptr());
161                instance.assume_init()
162            }
163        }
164    }
165}