use std::{ffi::CStr, os::raw, ptr};
use skia_bindings::{GrVkDrawableInfo, GrVkImageInfo, GrVkSurfaceInfo};
use super::{Alloc, YcbcrConversionInfo};
use crate::gpu::{self, vk, Protected};
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct ImageInfo {
image: vk::Image,
alloc: Alloc,
pub tiling: vk::ImageTiling,
pub layout: vk::ImageLayout,
pub format: vk::Format,
pub image_usage_flags: vk::ImageUsageFlags,
pub sample_count: u32,
pub level_count: u32,
pub current_queue_family: u32,
pub protected: Protected,
pub ycbcr_conversion_info: YcbcrConversionInfo,
pub sharing_mode: vk::SharingMode,
}
unsafe_send_sync!(ImageInfo);
native_transmutable!(GrVkImageInfo, ImageInfo, image_info_layout);
impl Default for ImageInfo {
fn default() -> Self {
Self {
image: vk::NULL_HANDLE.into(),
alloc: Alloc::default(),
tiling: vk::ImageTiling::OPTIMAL,
layout: vk::ImageLayout::UNDEFINED,
format: vk::Format::UNDEFINED,
image_usage_flags: 0,
sample_count: 1,
level_count: 0,
current_queue_family: vk::QUEUE_FAMILY_IGNORED,
protected: Protected::No,
ycbcr_conversion_info: Default::default(),
sharing_mode: vk::SharingMode::EXCLUSIVE,
}
}
}
impl ImageInfo {
#[allow(clippy::too_many_arguments)]
pub unsafe fn new(
image: vk::Image,
alloc: Alloc,
tiling: vk::ImageTiling,
layout: vk::ImageLayout,
format: vk::Format,
level_count: u32,
current_queue_family: impl Into<Option<u32>>,
ycbcr_conversion_info: impl Into<Option<YcbcrConversionInfo>>,
protected: impl Into<Option<Protected>>, sharing_mode: impl Into<Option<vk::SharingMode>>, ) -> Self {
let current_queue_family = current_queue_family
.into()
.unwrap_or(vk::QUEUE_FAMILY_IGNORED);
let ycbcr_conversion_info = ycbcr_conversion_info.into().unwrap_or_default();
let protected = protected.into().unwrap_or(Protected::No);
let sharing_mode = sharing_mode.into().unwrap_or(vk::SharingMode::EXCLUSIVE);
Self {
image,
alloc,
tiling,
layout,
format,
level_count,
current_queue_family,
protected,
ycbcr_conversion_info,
sharing_mode,
..Self::default()
}
}
pub unsafe fn from_info(info: &ImageInfo, layout: vk::ImageLayout) -> Self {
Self::new(
info.image,
info.alloc,
info.tiling,
layout,
info.format,
info.level_count,
info.current_queue_family,
info.ycbcr_conversion_info,
info.protected,
info.sharing_mode,
)
}
pub unsafe fn from_info_with_queue_index(
info: &ImageInfo,
layout: vk::ImageLayout,
family_queue_index: u32,
) -> Self {
Self::new(
info.image,
info.alloc,
info.tiling,
layout,
info.format,
info.level_count,
family_queue_index,
info.ycbcr_conversion_info,
info.protected,
info.sharing_mode,
)
}
}
impl ImageInfo {
pub fn image(&self) -> &vk::Image {
&self.image
}
pub unsafe fn set_image(&mut self, image: vk::Image) {
self.image = image
}
pub fn alloc(&self) -> &Alloc {
&self.alloc
}
pub unsafe fn set_alloc(&mut self, alloc: Alloc) {
self.alloc = alloc;
}
}
#[derive(Copy, Clone, Debug)]
pub enum GetProcOf {
Instance(vk::Instance, *const raw::c_char),
Device(vk::Device, *const raw::c_char),
}
impl GetProcOf {
pub unsafe fn name(&self) -> &CStr {
match *self {
GetProcOf::Instance(_, name) => CStr::from_ptr(name),
GetProcOf::Device(_, name) => CStr::from_ptr(name),
}
}
}
pub type GetProcResult = *const raw::c_void;
pub trait GetProc: Fn(GetProcOf) -> GetProcResult {}
impl<T> GetProc for T where T: Fn(GetProcOf) -> GetProcResult {}
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct DrawableInfo {
pub secondary_command_buffer: vk::CommandBuffer,
pub color_attachment_index: u32,
pub compatible_render_pass: vk::RenderPass,
pub format: vk::Format,
pub draw_bounds: *mut vk::Rect2D,
}
native_transmutable!(GrVkDrawableInfo, DrawableInfo, drawable_info_layout);
unsafe_send_sync!(DrawableInfo);
impl Default for DrawableInfo {
fn default() -> Self {
DrawableInfo {
secondary_command_buffer: vk::NULL_HANDLE.into(),
color_attachment_index: 0,
compatible_render_pass: vk::NULL_HANDLE.into(),
format: vk::Format::UNDEFINED,
draw_bounds: ptr::null_mut(),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct SurfaceInfo {
pub sample_count: u32,
pub level_count: u32,
pub protected: gpu::Protected,
pub image_tiling: vk::ImageTiling,
pub format: vk::Format,
pub image_usage_flags: vk::ImageUsageFlags,
pub ycbcr_conversion_info: vk::YcbcrConversionInfo,
pub sharing_mode: vk::SharingMode,
}
native_transmutable!(GrVkSurfaceInfo, SurfaceInfo, surface_info_layout);
impl Default for SurfaceInfo {
fn default() -> Self {
Self {
sample_count: 1,
level_count: 0,
protected: Protected::No,
image_tiling: vk::ImageTiling::OPTIMAL,
format: vk::Format::UNDEFINED,
image_usage_flags: 0,
ycbcr_conversion_info: Default::default(),
sharing_mode: vk::SharingMode::EXCLUSIVE,
}
}
}