use crate::layout::render_list::RenderList;
#[derive(Clone)]
pub struct SharedWgpuContext {
pub instance: wgpu::Instance,
pub adapter: wgpu::Adapter,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
pub surface_format: wgpu::TextureFormat,
pub(crate) number: u64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WgpuViewportRect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
#[derive(Clone, Copy)]
pub struct WgpuFrameInfo<'a> {
#[cfg_attr(not(feature = "raster"), allow(dead_code))]
render_list: &'a RenderList,
pub surface_width: u32,
pub surface_height: u32,
pub scale_factor: f32,
}
impl<'a> WgpuFrameInfo<'a> {
pub(crate) fn new(render_list: &'a RenderList, surface_width: u32, surface_height: u32, scale_factor: f32) -> Self {
Self {
render_list,
surface_width,
surface_height,
scale_factor,
}
}
#[cfg(feature = "raster")]
pub fn viewport(&self, image_id: u64) -> Option<WgpuViewportRect> {
self.image(image_id).map(|image| WgpuViewportRect {
x: image.x,
y: image.y,
width: image.width,
height: image.height,
})
}
#[cfg(feature = "raster")]
pub fn visible_viewport(&self, image_id: u64) -> Option<WgpuViewportRect> {
let image = self.image(image_id)?;
let [a, b, c, d] = image.transform;
let [origin_x, origin_y] = image.transform_origin;
let corners = [
(0.0, 0.0),
(image.width, 0.0),
(0.0, image.height),
(image.width, image.height),
]
.map(|(x, y)| {
let (x, y) = (x - origin_x, y - origin_y);
(image.x + origin_x + a * x + c * y, image.y + origin_y + b * x + d * y)
});
let mut left = corners.iter().map(|corner| corner.0).fold(f32::INFINITY, f32::min);
let mut top = corners.iter().map(|corner| corner.1).fold(f32::INFINITY, f32::min);
let mut right = corners.iter().map(|corner| corner.0).fold(f32::NEG_INFINITY, f32::max);
let mut bottom = corners.iter().map(|corner| corner.1).fold(f32::NEG_INFINITY, f32::max);
if image.clip.active {
left = left.max(image.clip.x);
top = top.max(image.clip.y);
right = right.min(image.clip.x + image.clip.width);
bottom = bottom.min(image.clip.y + image.clip.height);
}
left = left.max(0.0);
top = top.max(0.0);
right = right.min(self.surface_width as f32);
bottom = bottom.min(self.surface_height as f32);
(right > left && bottom > top).then(|| WgpuViewportRect {
x: left,
y: top,
width: right - left,
height: bottom - top,
})
}
#[cfg(feature = "raster")]
fn image(&self, image_id: u64) -> Option<&crate::images::ImageCmd> {
self.render_list.images.iter().find(|image| image.image_id == image_id)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct WgpuAppError {
pub kind: wgpu::ErrorFilter,
pub message: String,
}
impl From<wgpu::Error> for WgpuAppError {
fn from(error: wgpu::Error) -> Self {
let kind = match &error {
wgpu::Error::OutOfMemory { .. } => wgpu::ErrorFilter::OutOfMemory,
wgpu::Error::Validation { .. } => wgpu::ErrorFilter::Validation,
wgpu::Error::Internal { .. } => wgpu::ErrorFilter::Internal,
};
Self {
kind,
message: error.to_string(),
}
}
}
impl std::fmt::Display for WgpuAppError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{:?} error: {}", self.kind, self.message)
}
}
impl std::error::Error for WgpuAppError {}
pub trait WgpuFrameExtension {
fn initialize(&mut self, _gpu: &SharedWgpuContext) {}
fn prepare(&mut self, _gpu: &SharedWgpuContext, _frame: WgpuFrameInfo<'_>) {}
fn wants_redraw(&self) -> bool {
false
}
fn error(&mut self, _error: &WgpuAppError) {}
fn device_lost(&mut self) {}
fn shutdown(&mut self) {}
}
pub(crate) struct WgpuFrameExtensionEntry {
pub extension: Box<dyn WgpuFrameExtension>,
pub initialized: bool,
pub told_lost: bool,
}
impl WgpuFrameExtensionEntry {
pub fn new(extension: Box<dyn WgpuFrameExtension>) -> Self {
Self {
extension,
initialized: false,
told_lost: false,
}
}
}