use super::*;
impl HybridRenderer {
#[must_use]
pub fn with_backend(
gpu_backend: GraphicsBackend,
width: u32,
height: u32,
config: RenderConfig,
) -> Self {
Self::with_backend_and_font(
gpu_backend,
width,
height,
config,
NativeSystemFont::deterministic(),
)
}
#[must_use]
pub fn with_backend_and_font(
gpu_backend: GraphicsBackend,
width: u32,
height: u32,
config: RenderConfig,
font_provider: NativeSystemFont,
) -> Self {
let mut compositor: RecordingGpuDriver = RecordingGpuDriver::default();
compositor.set_backend_kind(gpu_backend);
Self {
software: SoftwareRenderer::with_font_provider(width, height, config, font_provider),
compositor,
last_cpu_effect_regions: Vec::new(),
last_gpu_native_commands: 0,
last_cpu_tile_count: 0,
last_cpu_upload_bytes: 0,
last_cpu_rasterized_pixels: 0,
}
}
#[must_use]
pub fn partition_commands(display_list: &DisplayList) -> HybridCommandPartition {
let scale: f32 = display_list.get_scale_factor().max(0.5);
let mut gpu_native_count: usize = 0;
let mut gpu_covered_pixels: usize = 0;
let mut cpu_effect_regions: Vec<Rect> = Vec::new();
for cmd in display_list.get_commands().iter() {
match cmd {
DrawCommand::BackdropBlur { rect, .. } => {
let phys: Rect = Rect::new(
rect.get_x() * scale,
rect.get_y() * scale,
(rect.get_width() * scale).max(1.0),
(rect.get_height() * scale).max(1.0),
);
cpu_effect_regions.push(phys);
}
DrawCommand::FillRoundedRect { rect, .. }
| DrawCommand::LinearGradientRect { rect, .. }
| DrawCommand::BoxShadow { rect, .. }
| DrawCommand::TextRun { rect, .. }
| DrawCommand::Image { rect, .. }
| DrawCommand::SharedImage { rect, .. }
| DrawCommand::SpriteRegion { rect, .. } => {
gpu_native_count = gpu_native_count.saturating_add(1);
let pw: usize = ((rect.get_width() * scale).ceil() as usize).max(1);
let ph: usize = ((rect.get_height() * scale).ceil() as usize).max(1);
gpu_covered_pixels = gpu_covered_pixels.saturating_add(pw.saturating_mul(ph));
}
DrawCommand::Circle { radius, .. } => {
gpu_native_count = gpu_native_count.saturating_add(1);
let d: usize = (((*radius) * 2.0 * scale).ceil() as usize).max(1);
gpu_covered_pixels = gpu_covered_pixels.saturating_add(d.saturating_mul(d));
}
DrawCommand::Line {
from, to, width, ..
} => {
gpu_native_count = gpu_native_count.saturating_add(1);
let lw: usize = (((to.get_x() - from.get_x()).abs().max(*width) * scale).ceil()
as usize)
.max(1);
let lh: usize = (((to.get_y() - from.get_y()).abs().max(*width) * scale).ceil()
as usize)
.max(1);
gpu_covered_pixels = gpu_covered_pixels.saturating_add(lw.saturating_mul(lh));
}
DrawCommand::Triangle { p0, p1, p2, .. } => {
gpu_native_count = gpu_native_count.saturating_add(1);
let w: f32 = p0.get_x().max(p1.get_x()).max(p2.get_x())
- p0.get_x().min(p1.get_x()).min(p2.get_x());
let h: f32 = p0.get_y().max(p1.get_y()).max(p2.get_y())
- p0.get_y().min(p1.get_y()).min(p2.get_y());
let pw: usize = ((w * scale).ceil() as usize).max(1);
let ph: usize = ((h * scale).ceil() as usize).max(1);
gpu_covered_pixels = gpu_covered_pixels.saturating_add(pw.saturating_mul(ph));
}
DrawCommand::Clear(_)
| DrawCommand::TextCaret { .. }
| DrawCommand::PushClip(_)
| DrawCommand::PushRoundedClip { .. }
| DrawCommand::PopClip => {
gpu_native_count = gpu_native_count.saturating_add(1);
}
}
}
HybridCommandPartition {
gpu_native_count,
cpu_effect_regions,
gpu_covered_pixels,
}
}
#[must_use]
pub fn evaluate_last_frame_net_gain(
&self,
gpu_full_ms: f64,
cpu_full_ms: f64,
cpu_tile_ms: f64,
gpu_composite_ms: f64,
) -> HybridNetGainEstimate {
let fb: &Framebuffer = self.get_software().get_framebuffer();
estimate_hybrid_net_gain(
fb.get_width(),
fb.get_height(),
self.get_last_cpu_effect_regions().as_slice(),
gpu_full_ms,
cpu_full_ms,
cpu_tile_ms,
gpu_composite_ms,
)
}
pub fn render(
&mut self,
display_list: &DisplayList,
damage: DamageRegion,
) -> Result<(usize, usize), PlatformError> {
let scale: f32 = display_list.get_scale_factor().max(0.5);
let vp: Size = display_list.get_viewport();
let phys_w: u32 = if vp.get_width() > 0.0 {
((vp.get_width() * scale).round() as u32).max(1)
} else {
self.get_software().get_framebuffer().get_width().max(1)
};
let phys_h: u32 = if vp.get_height() > 0.0 {
((vp.get_height() * scale).round() as u32).max(1)
} else {
self.get_software().get_framebuffer().get_height().max(1)
};
if self.get_software().get_framebuffer().get_width() != phys_w
|| self.get_software().get_framebuffer().get_height() != phys_h
{
let sw: &mut SoftwareRenderer = self.get_mut_software();
if let Some(fb) = sw.framebuffer.as_mut() {
fb.resize(phys_w, phys_h);
}
}
let full_rect: Rect = Rect::new(0.0, 0.0, phys_w as f32, phys_h as f32);
let partition: HybridCommandPartition = Self::partition_commands(display_list);
let mut batch: GpuBatch = GpuBatch::default();
batch.set_scissor(full_rect);
let mut clear_color: Color = COLOR_TRANSPARENT;
for cmd in display_list.get_commands().iter() {
match cmd {
DrawCommand::Clear(c) => {
clear_color = *c;
}
DrawCommand::FillRoundedRect {
rect,
color,
radius,
border,
} => {
let quad: GpuQuadInstance = GpuQuadInstance {
rect: [
rect.get_x() * scale,
rect.get_y() * scale,
rect.get_width() * scale,
rect.get_height() * scale,
],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: color.to_f32_array(),
secondary_color: border.get_color().to_f32_array(),
radii: [
radius.get_top_left() * scale,
radius.get_top_right() * scale,
radius.get_bottom_right() * scale,
radius.get_bottom_left() * scale,
],
stroke_or_blur: border.get_width() * scale,
angle_rad: 0.0,
kind: GpuPrimitiveKind::RoundedRect as u32,
reserved: 0,
};
let prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
batch.get_mut_instances().push(quad);
batch.get_mut_primitives().push(prim);
}
DrawCommand::LinearGradientRect {
rect,
gradient,
radius,
} => {
let quad: GpuQuadInstance = GpuQuadInstance {
rect: [
rect.get_x() * scale,
rect.get_y() * scale,
rect.get_width() * scale,
rect.get_height() * scale,
],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: gradient.get_start_color().to_f32_array(),
secondary_color: gradient.get_end_color().to_f32_array(),
radii: [
radius.get_top_left() * scale,
radius.get_top_right() * scale,
radius.get_bottom_right() * scale,
radius.get_bottom_left() * scale,
],
stroke_or_blur: 0.0,
angle_rad: gradient.get_angle_deg().to_radians(),
kind: GpuPrimitiveKind::Gradient as u32,
reserved: 0,
};
let prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
batch.get_mut_instances().push(quad);
batch.get_mut_primitives().push(prim);
}
DrawCommand::BoxShadow {
rect,
radius,
shadow,
} => {
let pad: f32 = (shadow.get_blur_radius() + shadow.get_spread_radius()) * scale;
let quad: GpuQuadInstance = GpuQuadInstance {
rect: [
(rect.get_x() + shadow.get_offset_x()) * scale - pad,
(rect.get_y() + shadow.get_offset_y()) * scale - pad,
rect.get_width() * scale + pad * 2.0,
rect.get_height() * scale + pad * 2.0,
],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: shadow.get_color().to_f32_array(),
secondary_color: [0.0; 4],
radii: [
radius.get_top_left() * scale,
radius.get_top_right() * scale,
radius.get_bottom_right() * scale,
radius.get_bottom_left() * scale,
],
stroke_or_blur: shadow.get_blur_radius() * scale,
angle_rad: pad,
kind: GpuPrimitiveKind::Shadow as u32,
reserved: 0,
};
let prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
batch.get_mut_instances().push(quad);
batch.get_mut_primitives().push(prim);
}
DrawCommand::TextRun { rect, style, .. } => {
let quad: GpuQuadInstance = GpuQuadInstance {
rect: [
rect.get_x() * scale,
rect.get_y() * scale,
rect.get_width() * scale,
rect.get_height() * scale,
],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: style.get_color().to_f32_array(),
secondary_color: [0.0; 4],
radii: [0.0; 4],
stroke_or_blur: 0.0,
angle_rad: 0.0,
kind: GpuPrimitiveKind::Glyph as u32,
reserved: 0,
};
let prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
batch.get_mut_instances().push(quad);
batch.get_mut_primitives().push(prim);
}
DrawCommand::Image { rect, radius, .. }
| DrawCommand::SharedImage { rect, radius, .. } => {
let quad: GpuQuadInstance = GpuQuadInstance {
rect: [
rect.get_x() * scale,
rect.get_y() * scale,
rect.get_width() * scale,
rect.get_height() * scale,
],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: [1.0, 1.0, 1.0, 1.0],
secondary_color: [0.0; 4],
radii: [
radius.get_top_left() * scale,
radius.get_top_right() * scale,
radius.get_bottom_right() * scale,
radius.get_bottom_left() * scale,
],
stroke_or_blur: 0.0,
angle_rad: 0.0,
kind: GpuPrimitiveKind::TexturedQuad as u32,
reserved: 0,
};
let prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
batch.get_mut_instances().push(quad);
batch.get_mut_primitives().push(prim);
}
DrawCommand::Circle {
center,
radius,
color,
} => {
let r: f32 = radius * scale;
let quad: GpuQuadInstance = GpuQuadInstance {
rect: [
center.get_x() * scale - r,
center.get_y() * scale - r,
r * 2.0,
r * 2.0,
],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: color.to_f32_array(),
secondary_color: [0.0; 4],
radii: [r; 4],
stroke_or_blur: 0.0,
angle_rad: 0.0,
kind: GpuPrimitiveKind::Circle as u32,
reserved: 0,
};
let prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&quad, 0, 0);
batch.get_mut_instances().push(quad);
batch.get_mut_primitives().push(prim);
}
DrawCommand::BackdropBlur { .. }
| DrawCommand::TextCaret { .. }
| DrawCommand::Line { .. }
| DrawCommand::Triangle { .. }
| DrawCommand::SpriteRegion { .. }
| DrawCommand::PushClip(_)
| DrawCommand::PushRoundedClip { .. }
| DrawCommand::PopClip => {}
}
}
let mut cpu_tiles: usize = 0;
let mut cpu_pixels: usize = 0;
let mut cpu_upload_bytes: usize = 0;
if !partition.get_cpu_effect_regions().is_empty() {
let mut sparse_damage: DamageRegion = DamageRegion::default();
for region in partition.get_cpu_effect_regions().iter() {
let clipped_x: f32 = region.get_x().clamp(0.0, phys_w as f32);
let clipped_y: f32 = region.get_y().clamp(0.0, phys_h as f32);
let clipped_w: f32 = region
.get_width()
.min((phys_w as f32 - clipped_x).max(0.0))
.max(0.0);
let clipped_h: f32 = region
.get_height()
.min((phys_h as f32 - clipped_y).max(0.0))
.max(0.0);
if clipped_w <= 0.0 || clipped_h <= 0.0 {
continue;
}
let tight_rect: Rect = Rect::new(clipped_x, clipped_y, clipped_w, clipped_h);
if !damage.get_full_damage()
&& let Some(dmg_rect) = damage.bounds_rect()
&& tight_rect.intersect(dmg_rect).is_none()
{
continue;
}
sparse_damage.add_rect(tight_rect);
let tile_w: usize = clipped_w.ceil() as usize;
let tile_h: usize = clipped_h.ceil() as usize;
let tile_bytes: usize = tile_w
.saturating_mul(tile_h)
.saturating_mul(HYBRID_RGBA8_BYTES_PER_PIXEL);
cpu_upload_bytes = cpu_upload_bytes.saturating_add(tile_bytes);
let tile_quad: GpuQuadInstance = GpuQuadInstance {
rect: [clipped_x, clipped_y, clipped_w, clipped_h],
clip: [0.0, 0.0, phys_w as f32, phys_h as f32],
color: [1.0, 1.0, 1.0, 1.0],
secondary_color: [0.0; 4],
radii: [0.0; 4],
stroke_or_blur: 0.0,
angle_rad: 0.0,
kind: GpuPrimitiveKind::TexturedQuad as u32,
reserved: 1,
};
let tile_prim: GpuPrimitiveInstance =
GpuPrimitiveInstance::from_quad_instance(&tile_quad, 0, 0);
batch.get_mut_instances().push(tile_quad);
batch.get_mut_primitives().push(tile_prim);
}
if sparse_damage.bounds_rect().is_some() {
let (tiles, pixels): (usize, usize) =
self.get_mut_software().render(display_list, sparse_damage);
cpu_tiles = tiles;
cpu_pixels = pixels;
}
}
self.set_last_cpu_effect_regions(partition.get_cpu_effect_regions().clone());
self.set_last_gpu_native_commands(partition.get_gpu_native_count());
self.set_last_cpu_tile_count(cpu_tiles);
self.set_last_cpu_upload_bytes(cpu_upload_bytes);
self.set_last_cpu_rasterized_pixels(cpu_pixels);
let submitted: usize =
self.get_mut_compositor()
.submit_batches(phys_w, phys_h, clear_color, &[batch])?;
let total_pixels: usize = if cpu_pixels > 0 {
partition
.get_gpu_covered_pixels()
.saturating_add(cpu_pixels)
} else {
partition.get_gpu_covered_pixels().max(1)
};
Ok((submitted.max(1), total_pixels))
}
}