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 = SkBlendModeCoeff::Zero;
17 let mut dst: SkBlendModeCoeff = 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: *const {unknown} = 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 use SkPathFillType::*;
89 match self {
90 Winding => self,
91 EvenOdd => self,
92 InverseWinding => Winding,
93 InverseEvenOdd => EvenOdd,
94 }
95 }
96}
97
98impl SkAlphaType {
99 pub fn is_opaque(self) -> bool {
100 self == SkAlphaType::Opaque
101 }
102}
103
104impl SkYUVColorSpace {
105 pub fn is_limited_range(self) -> bool {
106 unsafe { crate::SkYUVColorSpaceIsLimitedRange(self) }
107 }
108}
109
110#[cfg(feature = "gl")]
111impl From<crate::GrGLenum> for crate::GrGLFormat {
112 fn from(e: crate::GrGLenum) -> Self {
113 unsafe { crate::C_GrGLFormatFromGLEnum(glFormat:e) }
114 }
115}
116
117#[cfg(feature = "gl")]
118impl From<crate::GrGLFormat> for crate::GrGLenum {
119 fn from(format: crate::GrGLFormat) -> Self {
120 unsafe { crate::C_GrGLFormatToEnum(format) }
121 }
122}
123
124#[cfg(feature = "vulkan")]
125mod vulkan {
126 impl PartialEq for crate::VkComponentMapping {
127 fn eq(&self, other: &Self) -> bool {
128 self.r == other.r && self.g == other.g && self.b == other.b && self.a == other.a
129 }
130 }
131
132 impl Eq for crate::VkComponentMapping {}
133}
134
135#[cfg(feature = "d3d")]
136mod d3d {
137 use std::marker::PhantomData;
138
139 impl<T> Default for crate::gr_cp<T> {
140 fn default() -> Self {
141 Self {
142 fObject: std::ptr::null_mut(),
143 _phantom_0: PhantomData,
144 }
145 }
146 }
147
148 impl Default for crate::GrD3DTextureResourceInfo {
149 fn default() -> Self {
150 let mut instance: MaybeUninit<{unknown}> = std::mem::MaybeUninit::uninit();
151 unsafe {
152 crate::C_GrD3DTextureResourceInfo_Construct(instance.as_mut_ptr());
153 instance.assume_init()
154 }
155 }
156 }
157}
158