#[cfg(feature = "backend-tiny-skia")]
pub mod cpu;
#[cfg(all(feature = "ssr", feature = "backend-tiny-skia"))]
pub mod ssr;
#[cfg(feature = "backend-wgpu")]
pub mod wgpu;
pub(crate) mod paint_tree;
mod painter;
pub mod path;
pub mod text;
pub use painter::{
BackdropRegion, ClipInfo, DrawCommand, LocalDrawItem, Painter, OUTLINE_Z_OFFSET,
};
pub use text::{
ci_at_visual_x, expanded_to_raw, glyph_char_x, glyph_pos_at_ci, measure_text_width,
move_visual_row, raw_to_expanded, shape_text, visual_row_at_exp_ci, visual_row_count,
visual_row_from_y, TextGlyph, TextMeasurer,
};
#[cfg(feature = "backend-tiny-skia")]
pub use cpu::TinySkiaRenderer;
#[cfg(feature = "backend-wgpu")]
pub use wgpu::WgpuRenderer;
use crate::style::{Color, Size};
#[derive(Clone)]
pub(crate) struct LocalTextArea {
pub buffer: std::rc::Rc<std::cell::RefCell<cosmic_text::Buffer>>,
pub generation: u64,
pub scale: f32,
pub color: crate::style::Color,
pub scroll_x: f32,
pub scroll_y: f32,
pub local_left: f32,
pub local_top: f32,
pub clip_local_x: f32,
pub clip_local_y: f32,
pub clip_w: f32,
pub clip_h: f32,
}
impl LocalTextArea {
#[allow(dead_code)]
pub fn to_world(
&self,
x: f32,
y: f32,
scroll_ox: f32,
scroll_oy: f32,
z: i32,
eid: crate::core::ElementId,
) -> crate::render::wgpu::glyphon_bridge::TextAreaDesc {
self.to_world_clipped(x, y, scroll_ox, scroll_oy, z, eid, None)
}
pub fn to_world_clipped(
&self,
x: f32,
y: f32,
scroll_ox: f32,
scroll_oy: f32,
z: i32,
eid: crate::core::ElementId,
parent_clip: Option<crate::style::Rect>,
) -> crate::render::wgpu::glyphon_bridge::TextAreaDesc {
let mut clip = crate::style::Rect::new(
x + self.clip_local_x - scroll_ox,
y + self.clip_local_y - scroll_oy,
self.clip_w,
self.clip_h,
);
if let Some(pc) = parent_clip {
let ix = clip.x.max(pc.x);
let iy = clip.y.max(pc.y);
let iw = ((clip.x + clip.width).min(pc.x + pc.width) - ix).max(0.0);
let ih = ((clip.y + clip.height).min(pc.y + pc.height) - iy).max(0.0);
clip = crate::style::Rect::new(ix, iy, iw, ih);
}
crate::render::wgpu::glyphon_bridge::TextAreaDesc {
buffer: self.buffer.clone(),
element_id: eid,
generation: self.generation,
left: x + self.local_left - scroll_ox,
top: y + self.local_top - scroll_oy,
scale: self.scale,
color: self.color,
scroll_x: self.scroll_x,
scroll_y: self.scroll_y,
clip_rect: Some(clip),
z_index: z,
}
}
}
#[derive(Clone)]
pub(crate) struct CachedScene {
pub local_items: Vec<painter::LocalDrawItem>,
pub commands: Vec<DrawCommand>,
pub local_text_areas: Vec<LocalTextArea>,
pub surface_gen: u64,
pub text_gen: u64,
pub decor_gen: u64,
pub decor_start: usize,
pub decor_text_start: usize,
pub scroll_x: f32,
pub scroll_y: f32,
}
#[derive(Clone)]
pub(crate) struct CachedSubtree {
pub commands: Vec<DrawCommand>,
pub text_areas: Vec<crate::render::wgpu::glyphon_bridge::TextAreaDesc>,
pub backdrop_regions: Vec<BackdropRegion>,
pub root_x: f32,
pub root_y: f32,
pub scroll_ox: f32,
pub scroll_oy: f32,
pub content_gen: u64,
#[allow(dead_code)]
pub layout_gen: u64,
pub scroll_gen: u64,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum RendererChoice {
Auto,
Gpu,
Cpu,
}
pub trait RenderBackend: 'static {
fn set_clear_color(&mut self, c: Color);
}
pub enum BackendRenderer {
#[cfg(feature = "backend-wgpu")]
Gpu(WgpuRenderer),
#[cfg(feature = "backend-tiny-skia")]
Cpu(TinySkiaRenderer),
Custom(Box<dyn RenderBackend>),
}
macro_rules! renderer_dispatch {
($self:expr, |$r:ident| $body:expr) => {
match $self {
#[cfg(feature = "backend-wgpu")]
BackendRenderer::Gpu($r) => $body,
#[cfg(feature = "backend-tiny-skia")]
BackendRenderer::Cpu($r) => $body,
BackendRenderer::Custom($r) => $body,
}
};
}
impl BackendRenderer {
pub fn set_clear_color(&mut self, c: Color) {
renderer_dispatch!(self, |r| r.set_clear_color(c));
}
#[cfg(feature = "backend-wgpu")]
pub fn create_surface(
&mut self,
window: &std::sync::Arc<dyn winit::window::Window>,
) -> Result<(), crate::core::error::UiError> {
match self {
BackendRenderer::Gpu(r) => r.create_surface(window).map_err(|e| {
crate::core::error::UiError::GpuInit(match e {
crate::render::wgpu::RenderError::NoAdapter => {
crate::core::error::GpuErrorKind::NoAdapter
}
crate::render::wgpu::RenderError::Surface => {
crate::core::error::GpuErrorKind::Surface
}
crate::render::wgpu::RenderError::Device => {
crate::core::error::GpuErrorKind::Device
}
})
}),
BackendRenderer::Cpu(_) => Ok(()),
BackendRenderer::Custom(_) => Ok(()),
}
}
pub fn destroy_surface(&mut self) {
match self {
#[cfg(feature = "backend-wgpu")]
BackendRenderer::Gpu(r) => r.destroy_surface(),
_ => {}
}
}
pub fn surface_ready(&self) -> bool {
match self {
#[cfg(feature = "backend-wgpu")]
BackendRenderer::Gpu(r) => r.has_surface(),
#[cfg(feature = "backend-tiny-skia")]
BackendRenderer::Cpu(_) => true,
BackendRenderer::Custom(_) => true,
}
}
#[cfg(feature = "backend-wgpu")]
pub fn begin_frame(&mut self) -> Result<wgpu::Frame, wgpu::RenderError> {
match self {
BackendRenderer::Gpu(r) => r.begin_frame(),
BackendRenderer::Cpu(_) => Err(wgpu::RenderError::Surface),
BackendRenderer::Custom(_) => Err(wgpu::RenderError::Surface),
}
}
#[cfg(feature = "backend-wgpu")]
pub fn draw_commands(
&mut self,
frame: &mut wgpu::Frame,
commands: &[DrawCommand],
text_areas: &[wgpu::TextAreaDesc],
backdrop_regions: &[painter::BackdropRegion],
hint: Size,
) {
match self {
BackendRenderer::Gpu(r) => {
r.draw_commands(frame, commands, text_areas, backdrop_regions, hint)
}
BackendRenderer::Cpu(_) => {}
BackendRenderer::Custom(_) => {}
}
}
#[cfg(feature = "backend-wgpu")]
pub fn end_frame(&mut self, frame: wgpu::Frame) {
match self {
BackendRenderer::Gpu(r) => r.end_frame(frame),
BackendRenderer::Cpu(_) => {}
BackendRenderer::Custom(_) => {}
}
}
#[cfg(feature = "backend-wgpu")]
pub fn resize_gpu(&mut self, w: u32, h: u32) {
match self {
BackendRenderer::Gpu(r) => r.resize(w, h),
BackendRenderer::Cpu(_) => {}
BackendRenderer::Custom(_) => {}
}
}
#[cfg(feature = "backend-wgpu")]
pub fn set_scale_factor_gpu(&mut self, sf: f64) {
match self {
BackendRenderer::Gpu(r) => r.set_scale_factor(sf),
BackendRenderer::Cpu(_) => {}
BackendRenderer::Custom(_) => {}
}
}
#[cfg(feature = "backend-tiny-skia")]
pub fn resize_cpu(&mut self, logical_w: f32, logical_h: f32, sf: f32) {
match self {
BackendRenderer::Cpu(r) => r.resize(logical_w, logical_h, sf),
BackendRenderer::Gpu(_) => {}
BackendRenderer::Custom(_) => {}
}
}
}