skia_safe/gpu/vk/
vulkan_types.rs

1use skia_bindings::{
2    self as sb, skgpu_VulkanAlloc, skgpu_VulkanBackendMemory, skgpu_VulkanYcbcrConversionInfo,
3};
4
5use crate::{gpu::vk, prelude::*};
6
7#[deprecated(since = "0.76.0", note = "Use BackendMemory")]
8pub type GraphicsBackendMemory = skgpu_VulkanBackendMemory;
9pub type BackendMemory = skgpu_VulkanBackendMemory;
10
11#[repr(C)]
12#[derive(Copy, Clone, Debug)]
13pub struct Alloc {
14    pub memory: vk::DeviceMemory,
15    pub offset: vk::DeviceSize,
16    pub size: vk::DeviceSize,
17    pub flags: AllocFlag,
18    pub backend_memory: BackendMemory,
19    uses_system_heap: bool,
20}
21unsafe_send_sync!(Alloc);
22
23native_transmutable!(skgpu_VulkanAlloc, Alloc);
24
25impl Default for Alloc {
26    fn default() -> Self {
27        Self {
28            memory: vk::NULL_HANDLE.into(),
29            offset: 0,
30            size: 0,
31            flags: AllocFlag::empty(),
32            backend_memory: 0,
33            uses_system_heap: false,
34        }
35    }
36}
37
38impl PartialEq for Alloc {
39    fn eq(&self, other: &Self) -> bool {
40        unsafe { sb::C_VulkanAlloc_Equals(self.native(), other.native()) }
41    }
42}
43
44bitflags! {
45    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
46    pub struct AllocFlag : u32 {
47        const NONCOHERENT = sb::skgpu_VulkanAlloc_Flag::kNoncoherent_Flag as _;
48        const MAPPABLE = sb::skgpu_VulkanAlloc_Flag::kMappable_Flag as _;
49        const LAZILY_ALLOCATED = sb::skgpu_VulkanAlloc_Flag::kLazilyAllocated_Flag as _;
50    }
51}
52
53impl Alloc {
54    /// # Safety
55    /// The memory's lifetime is expected to outlive the lifetime of the returned object.
56    pub unsafe fn from_device_memory(
57        memory: vk::DeviceMemory,
58        offset: vk::DeviceSize,
59        size: vk::DeviceSize,
60        flags: AllocFlag,
61    ) -> Alloc {
62        Alloc {
63            memory,
64            offset,
65            size,
66            flags,
67            backend_memory: 0,
68            uses_system_heap: false,
69        }
70    }
71}
72
73// Robustness: All fields turned private in m144, so it's probably best to convert this to a Handle.
74#[derive(Copy, Clone, Eq, Debug)]
75#[repr(C)]
76pub struct YcbcrConversionInfo {
77    format: vk::Format,
78    external_format: u64,
79    ycbcr_model: vk::SamplerYcbcrModelConversion,
80    ycbcr_range: vk::SamplerYcbcrRange,
81    x_chroma_offset: vk::ChromaLocation,
82    y_chroma_offset: vk::ChromaLocation,
83    chroma_filter: vk::Filter,
84    force_explicit_reconstruction: vk::Bool32,
85    components: vk::ComponentMapping,
86    sampler_filter_must_match_chroma_filter: bool,
87    supports_linear_filter: bool,
88}
89
90native_transmutable!(skgpu_VulkanYcbcrConversionInfo, YcbcrConversionInfo);
91
92impl PartialEq for YcbcrConversionInfo {
93    fn eq(&self, other: &Self) -> bool {
94        unsafe { sb::C_VulkanYcbcrConversionInfo_Equals(self.native(), other.native()) }
95    }
96}
97
98impl Default for YcbcrConversionInfo {
99    fn default() -> Self {
100        Self {
101            format: vk::Format::UNDEFINED,
102            external_format: 0,
103            ycbcr_model: vk::SamplerYcbcrModelConversion::RGB_IDENTITY,
104            ycbcr_range: vk::SamplerYcbcrRange::ITU_FULL,
105            x_chroma_offset: vk::ChromaLocation::COSITED_EVEN,
106            y_chroma_offset: vk::ChromaLocation::COSITED_EVEN,
107            chroma_filter: vk::Filter::NEAREST,
108            force_explicit_reconstruction: 0,
109            components: vk::ComponentMapping {
110                r: vk::ComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
111                g: vk::ComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
112                b: vk::ComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
113                a: vk::ComponentSwizzle::VK_COMPONENT_SWIZZLE_IDENTITY,
114            },
115            sampler_filter_must_match_chroma_filter: true,
116            supports_linear_filter: false,
117        }
118    }
119}
120
121impl YcbcrConversionInfo {
122    #[allow(clippy::too_many_arguments)]
123    pub fn new_with_external_format(
124        external_format: u64,
125        ycbcr_model: vk::SamplerYcbcrModelConversion,
126        ycbcr_range: vk::SamplerYcbcrRange,
127        x_chroma_offset: vk::ChromaLocation,
128        y_chroma_offset: vk::ChromaLocation,
129        chroma_filter: vk::Filter,
130        force_explicit_reconstruction: vk::Bool32,
131        components: vk::ComponentMapping,
132        external_format_features: vk::FormatFeatureFlags,
133    ) -> Self {
134        Self::construct(|ci| unsafe {
135            sb::C_VulkanYcbcrConversionInfo_Construct_ExternalFormat(
136                ci,
137                external_format,
138                ycbcr_model,
139                ycbcr_range,
140                x_chroma_offset,
141                y_chroma_offset,
142                chroma_filter,
143                force_explicit_reconstruction,
144                components,
145                external_format_features,
146            )
147        })
148    }
149
150    #[allow(clippy::too_many_arguments)]
151    pub fn new_with_format(
152        format: vk::Format,
153        ycbcr_model: vk::SamplerYcbcrModelConversion,
154        ycbcr_range: vk::SamplerYcbcrRange,
155        x_chroma_offset: vk::ChromaLocation,
156        y_chroma_offset: vk::ChromaLocation,
157        chroma_filter: vk::Filter,
158        force_explicit_reconstruction: vk::Bool32,
159        components: vk::ComponentMapping,
160        format_features: vk::FormatFeatureFlags,
161    ) -> Self {
162        Self::construct(|ci| unsafe {
163            sb::C_VulkanYcbcrConversionInfo_Construct_Format(
164                ci,
165                format,
166                ycbcr_model,
167                ycbcr_range,
168                x_chroma_offset,
169                y_chroma_offset,
170                chroma_filter,
171                force_explicit_reconstruction,
172                components,
173                format_features,
174            )
175        })
176    }
177    pub fn is_valid(&self) -> bool {
178        self.ycbcr_model != vk::SamplerYcbcrModelConversion::RGB_IDENTITY
179            || self.has_external_format()
180    }
181
182    pub fn format(&self) -> vk::Format {
183        self.format
184    }
185
186    pub fn has_external_format(&self) -> bool {
187        self.external_format != 0
188    }
189
190    pub fn external_format(&self) -> u64 {
191        self.external_format
192    }
193
194    pub fn model(&self) -> vk::SamplerYcbcrModelConversion {
195        self.ycbcr_model
196    }
197
198    pub fn range(&self) -> vk::SamplerYcbcrRange {
199        self.ycbcr_range
200    }
201
202    pub fn x_chroma_offset(&self) -> vk::ChromaLocation {
203        self.x_chroma_offset
204    }
205
206    pub fn y_chroma_offset(&self) -> vk::ChromaLocation {
207        self.y_chroma_offset
208    }
209
210    pub fn chroma_filter(&self) -> vk::Filter {
211        self.chroma_filter
212    }
213
214    pub fn force_explicit_reconstruction(&self) -> vk::Bool32 {
215        self.force_explicit_reconstruction
216    }
217
218    pub fn components(&self) -> vk::ComponentMapping {
219        self.components
220    }
221
222    pub fn sampler_filter_must_match_chroma_filter(&self) -> bool {
223        self.sampler_filter_must_match_chroma_filter
224    }
225
226    pub fn supports_linear_filter(&self) -> bool {
227        self.supports_linear_filter
228    }
229}