use crate::{GpuError, GpuResult, TextureDescriptor, TextureFormat, TextureUsage};
use skia_rs_core::{Color, Rect, Scalar};
#[derive(Debug, Clone)]
pub struct GpuSurfaceProps {
pub width: u32,
pub height: u32,
pub sample_count: u32,
pub format: TextureFormat,
pub srgb: bool,
}
impl Default for GpuSurfaceProps {
fn default() -> Self {
Self {
width: 0,
height: 0,
sample_count: 1,
format: TextureFormat::Rgba8Unorm,
srgb: false,
}
}
}
impl GpuSurfaceProps {
pub fn new(width: u32, height: u32) -> Self {
Self {
width,
height,
..Default::default()
}
}
pub fn with_format(mut self, format: TextureFormat) -> Self {
self.format = format;
self
}
pub fn with_sample_count(mut self, count: u32) -> Self {
self.sample_count = count;
self
}
pub fn with_srgb(mut self, srgb: bool) -> Self {
self.srgb = srgb;
self
}
}
pub trait GpuSurface: Send + Sync {
fn width(&self) -> u32;
fn height(&self) -> u32;
fn format(&self) -> TextureFormat;
fn sample_count(&self) -> u32;
fn clear(&mut self, color: Color);
fn present(&mut self);
fn read_pixels(&self, dst: &mut [u8], dst_row_bytes: usize) -> bool;
fn flush(&mut self);
}
#[derive(Debug, Clone)]
pub struct RenderPassDescriptor {
pub clear_color: Option<[f32; 4]>,
pub clear_depth: Option<f32>,
pub clear_stencil: Option<u32>,
}
impl Default for RenderPassDescriptor {
fn default() -> Self {
Self {
clear_color: Some([0.0, 0.0, 0.0, 1.0]),
clear_depth: Some(1.0),
clear_stencil: Some(0),
}
}
}
impl RenderPassDescriptor {
pub fn no_clear() -> Self {
Self {
clear_color: None,
clear_depth: None,
clear_stencil: None,
}
}
pub fn color_clear(r: f32, g: f32, b: f32, a: f32) -> Self {
Self {
clear_color: Some([r, g, b, a]),
clear_depth: None,
clear_stencil: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_surface_props() {
let props = GpuSurfaceProps::new(800, 600)
.with_format(TextureFormat::Bgra8Unorm)
.with_sample_count(4);
assert_eq!(props.width, 800);
assert_eq!(props.height, 600);
assert_eq!(props.sample_count, 4);
}
#[test]
fn test_render_pass_descriptor() {
let desc = RenderPassDescriptor::color_clear(1.0, 0.0, 0.0, 1.0);
assert!(desc.clear_color.is_some());
assert!(desc.clear_depth.is_none());
}
}