use std::sync::Arc;
use super::readback_window;
use crate::{
app::render_engine::{CapturedFrame, RenderEngine},
layout::{
quad::ClipRect,
render_list::{GlyphAtlas, GlyphCmd, RectCmd, RenderList},
},
node::{border::BorderRadius, color::Color},
};
const WIDTH: u32 = 96;
const HEIGHT: u32 = 56;
const CLEAR: Color = Color::new(24, 24, 27, 255);
const GLYPH: f32 = 12.0;
const ATLAS: u32 = 16;
fn atlas() -> GlyphAtlas {
let mut data = vec![0_u8; (ATLAS * ATLAS * 4) as usize];
for y in 2..10 {
for x in 2..10 {
let index = ((y * ATLAS + x) * 4) as usize;
data[index..index + 4].copy_from_slice(&[255; 4]);
}
}
GlyphAtlas {
data: Arc::from(data),
width: ATLAS,
height: ATLAS,
version: 1,
dirty_rects: Arc::from(Vec::new()),
dirty_from_version: 0,
}
}
fn clip(x: f32, y: f32, width: f32, height: f32) -> ClipRect {
ClipRect::rect(x, y, width, height)
}
fn glyph(order: usize, x: f32, y: f32, color: Color, clip: ClipRect) -> GlyphCmd {
GlyphCmd {
order,
x,
y,
width: GLYPH,
height: GLYPH,
color: color.to_linear_f32_array(),
atlas_min: [0.0, 0.0],
atlas_max: [GLYPH, GLYPH],
transform: [1.0, 0.0, 0.0, 1.0],
transform_origin: [0.0; 2],
sharpness: 1.0,
color_glyph: false,
shadow_sigma: 0.0,
clip,
}
}
fn runs() -> Vec<(Vec<(f32, f32)>, Color, ClipRect)> {
let row = |y: f32, xs: &[f32]| xs.iter().map(|x| (*x, y)).collect::<Vec<_>>();
let labels = clip(2.0, 2.0, 44.0, 16.0);
let rounded = ClipRect::rounded_rect(2.0, 38.0, 44.0, 16.0, Some(BorderRadius::all(6.0)));
vec![
(row(4.0, &[4.0, 16.0, 28.0]), Color::from_hex("#ef4444"), labels),
(
row(20.0, &[50.0, 62.0, 74.0]),
Color::from_hex("#eab308"),
clip(51.0, 21.0, 36.0, 10.0),
),
(row(21.0, &[4.0, 16.0]), Color::from_hex("#22c55e"), ClipRect::default()),
(
row(8.0, &[10.0, 22.0]),
Color::from_hex("#ffffff80"),
ClipRect::default(),
),
(row(2.0, &[14.0]), Color::from_hex("#3b82f680"), labels),
(
row(4.0, &[52.0, 64.0, 76.0]),
Color::from_hex("#a855f7"),
clip(50.0, 2.0, 30.0, 16.0),
),
(row(36.0, &[0.0, 14.0, 28.0]), Color::from_hex("#06b6d4"), rounded),
(row(42.0, &[34.0]), Color::from_hex("#f97316"), rounded),
]
}
fn list(rects: Vec<RectCmd>, glyphs: Vec<GlyphCmd>) -> RenderList {
RenderList {
clear_color: CLEAR,
rects,
glyphs,
#[cfg(feature = "raster")]
images: Vec::new(),
layers: Vec::new(),
backdrops: Vec::new(),
atlas: atlas(),
}
}
fn batched() -> RenderList {
let glyphs = runs()
.into_iter()
.enumerate()
.flat_map(|(order, (places, color, clip))| places.into_iter().map(move |(x, y)| glyph(order, x, y, color, clip)))
.collect();
list(Vec::new(), glyphs)
}
fn one_by_one() -> RenderList {
let mut rects = Vec::new();
let mut glyphs = Vec::new();
for (index, (places, color, clip)) in runs().into_iter().enumerate() {
let order = index * 2;
glyphs.extend(places.into_iter().map(|(x, y)| glyph(order, x, y, color, clip)));
if clip.active {
glyphs.push(glyph(order, clip.x + clip.width + 4.0, clip.y, color, clip));
}
rects.push(RectCmd {
order: order + 1,
x: 0.0,
y: 0.0,
width: WIDTH as f32,
height: HEIGHT as f32,
color: Color::new(0, 0, 0, 0),
radii: [0.0; 4],
stroke: [0.0; 4],
stroke_color: Color::new(0, 0, 0, 0),
stroke_style: crate::layout::render_list::StrokeStyle::default(),
transform: [1.0, 0.0, 0.0, 1.0],
transform_origin: [0.0; 2],
clip: ClipRect::default(),
gradient: None,
shadow: None,
});
}
list(rects, glyphs)
}
fn pixel(frame: &CapturedFrame, x: u32, y: u32) -> [u8; 3] {
let index = ((y * frame.width + x) * 4) as usize;
[frame.rgba[index], frame.rgba[index + 1], frame.rgba[index + 2]]
}
fn assert_batches_paint_like_single_runs(engine: &mut dyn RenderEngine, backend: &str) -> CapturedFrame {
let frames = readback_window::capture_each(engine, &[&batched(), &one_by_one()], WIDTH, HEIGHT);
let (batched, single) = (&frames[0], &frames[1]);
let differing = batched
.rgba
.chunks_exact(4)
.zip(single.rgba.chunks_exact(4))
.position(|(a, b)| a != b);
assert_eq!(
differing.map(|index| (index as u32 % WIDTH, index as u32 / WIDTH)),
None,
"{backend}: the batched frame differs from the frame drawn run by run at this pixel"
);
let clear = [CLEAR.r(), CLEAR.g(), CLEAR.b()];
let close = |actual: [u8; 3], expected: [u8; 3]| actual.iter().zip(expected).all(|(a, e)| a.abs_diff(e) <= 1);
assert!(close(pixel(batched, 8, 8), [0xef, 0x44, 0x44]), "{backend}: ink");
assert_eq!(pixel(batched, 4, 8), clear, "{backend}: a glyph's transparent edge");
assert!(
close(pixel(batched, 79, 8), [0xa8, 0x55, 0xf7]),
"{backend}: inside the cutting clip"
);
assert_eq!(pixel(batched, 81, 8), clear, "{backend}: past the cutting clip");
assert_eq!(pixel(batched, 2, 38), clear, "{backend}: the rounded clip's corner");
assert!(
close(pixel(batched, 6, 42), [0x06, 0xb6, 0xd4]),
"{backend}: inside the rounded clip"
);
let over = pixel(batched, 13, 12);
assert!(
over[0] > 0xef && over[1] > 0x90 && over[1] < 0xb0,
"{backend}: translucent text over the first text gave {over:?}"
);
frames.into_iter().next().expect("the batched frame")
}
#[cfg(feature = "wgpu")]
#[test]
#[ignore = "requires a Windows desktop and a GPU adapter; creates a hidden window"]
fn wgpu_glyph_batches_paint_like_single_runs() {
assert_batches_paint_like_single_runs(&mut crate::app::wgpu_render::WgpuRenderEngine::new(), "wgpu");
}
#[cfg(feature = "dx12")]
#[test]
#[ignore = "requires a Windows desktop and a DX12 adapter; creates a hidden window"]
fn dx12_glyph_batches_paint_like_single_runs() {
assert_batches_paint_like_single_runs(&mut crate::app::dx12_render::Dx12RenderEngine::new(), "dx12");
}
#[cfg(all(feature = "wgpu", feature = "dx12"))]
#[test]
#[ignore = "requires a Windows desktop, a GPU adapter and DX12; creates hidden windows"]
fn wgpu_and_dx12_glyph_batches_match_each_other() {
let wgpu = assert_batches_paint_like_single_runs(&mut crate::app::wgpu_render::WgpuRenderEngine::new(), "wgpu");
let dx12 = assert_batches_paint_like_single_runs(&mut crate::app::dx12_render::Dx12RenderEngine::new(), "dx12");
let worst = wgpu
.rgba
.iter()
.zip(&dx12.rgba)
.map(|(a, b)| a.abs_diff(*b))
.max()
.unwrap_or(0);
assert!(worst <= 2, "the backends differ by {worst}");
}
#[cfg(all(feature = "wgpu", feature = "perf_profile"))]
#[test]
#[ignore = "requires a Windows desktop and a GPU adapter; creates a hidden window"]
fn wgpu_glyph_runs_share_draws() {
let mut engine = crate::app::wgpu_render::WgpuRenderEngine::new();
assert_draw_calls(&mut engine);
}
#[cfg(all(feature = "dx12", feature = "perf_profile"))]
#[test]
#[ignore = "requires a Windows desktop and a DX12 adapter; creates a hidden window"]
fn dx12_glyph_runs_share_draws() {
let mut engine = crate::app::dx12_render::Dx12RenderEngine::new();
assert_draw_calls(&mut engine);
}
#[cfg(feature = "perf_profile")]
fn assert_draw_calls(engine: &mut dyn RenderEngine) {
readback_window::capture(engine, &batched(), WIDTH, HEIGHT);
let batched = engine.last_profile().expect("a render profile").draw_calls;
readback_window::capture(engine, &one_by_one(), WIDTH, HEIGHT);
let single = engine.last_profile().expect("a render profile").draw_calls;
assert_eq!(batched, 3);
assert_eq!(single, 16);
}