use crate::arena::StrRef;
use crate::element::SpriteSource;
use crate::solver::Region;
use crate::style::Color;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct DrawOp<'a> {
pub id: Option<u32>,
pub bounds: Region,
pub clip: Region,
pub clip_radius: u8,
pub payload: DrawPayload<'a>,
}
#[derive(Copy, Clone)]
pub enum DrawPayload<'a> {
Quad {
bg: Color,
bg_grad: Color,
border_color: Color,
rounded: u8,
border_width: u8,
},
TextRun {
text: StrRef<'a>,
color: Color,
font_size: u8,
text_flags: u8,
},
Sprite(SpriteSource<'a>),
}
impl<'a> PartialEq for DrawPayload<'a> {
fn eq(&self, other: &Self) -> bool {
match (*self, *other) {
(
Self::Quad {
bg: b1,
bg_grad: bg1,
border_color: bc1,
rounded: r1,
border_width: bw1,
},
Self::Quad {
bg: b2,
bg_grad: bg2,
border_color: bc2,
rounded: r2,
border_width: bw2,
},
) => b1 == b2 && bg1 == bg2 && bc1 == bc2 && r1 == r2 && bw1 == bw2,
(
Self::TextRun {
text: t1,
color: c1,
font_size: f1,
text_flags: tf1,
},
Self::TextRun {
text: t2,
color: c2,
font_size: f2,
text_flags: tf2,
},
) => t1 == t2 && c1 == c2 && f1 == f2 && tf1 == tf2,
(
Self::Sprite(SpriteSource::Bitmap {
bytes: b1,
format: f1,
tint: t1,
}),
Self::Sprite(SpriteSource::Bitmap {
bytes: b2,
format: f2,
tint: t2,
}),
) => {
core::ptr::eq(b1.as_ptr(), b2.as_ptr())
&& b1.len() == b2.len()
&& f1 == f2
&& t1 == t2
}
(
Self::Sprite(SpriteSource::Shader { state_hash: h1, .. }),
Self::Sprite(SpriteSource::Shader { state_hash: h2, .. }),
) => h1 == h2,
(
Self::Sprite(s1 @ SpriteSource::Svg { .. }),
Self::Sprite(s2 @ SpriteSource::Svg { .. }),
) => s1 == s2,
(Self::Sprite(s1 @ SpriteSource::Arc { .. }), Self::Sprite(s2 @ SpriteSource::Arc { .. })) => {
s1 == s2
}
_ => false,
}
}
}
impl<'a> Eq for DrawPayload<'a> {}
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct DrawOpSnapshot {
pub effective_bounds: Region,
pub payload_hash: u32,
}
impl<'a> DrawOp<'a> {
pub fn to_snapshot(&self) -> DrawOpSnapshot {
let mut h: u32 = 0x811c_9dc5;
let mix = |h: &mut u32, byte: u8| {
*h ^= byte as u32;
*h = h.wrapping_mul(0x0100_0193);
};
let mix_i16 = |h: &mut u32, val: i16| {
for b in val.to_le_bytes() {
mix(h, b);
}
};
let mix_u32 = |h: &mut u32, val: u32| {
for b in val.to_le_bytes() {
mix(h, b);
}
};
if let Some(id) = self.id {
mix(&mut h, 0xFF);
mix_u32(&mut h, id);
}
mix_i16(&mut h, self.bounds.x);
mix_i16(&mut h, self.bounds.y);
mix_i16(&mut h, self.bounds.w);
mix_i16(&mut h, self.bounds.h);
mix_i16(&mut h, self.clip.x);
mix_i16(&mut h, self.clip.y);
mix_i16(&mut h, self.clip.w);
mix_i16(&mut h, self.clip.h);
mix(&mut h, self.clip_radius);
match self.payload {
DrawPayload::Quad {
bg,
bg_grad,
border_color,
rounded,
border_width,
} => {
mix(&mut h, 1);
mix_u32(&mut h, bg.0);
mix_u32(&mut h, bg_grad.0);
mix_u32(&mut h, border_color.0);
mix(&mut h, rounded);
mix(&mut h, border_width);
}
DrawPayload::TextRun {
text,
color,
font_size,
text_flags,
} => {
mix(&mut h, 2);
mix_u32(&mut h, color.0);
mix(&mut h, font_size);
mix(&mut h, text_flags);
for &b in text.as_str().as_bytes() {
mix(&mut h, b);
}
}
DrawPayload::Sprite(SpriteSource::Bitmap {
bytes,
format,
tint,
}) => {
mix(&mut h, 3);
mix(&mut h, format as u8);
mix_u32(&mut h, tint.0);
mix_u32(&mut h, bytes.as_ptr() as usize as u32);
mix_u32(&mut h, bytes.len() as u32);
}
DrawPayload::Sprite(SpriteSource::Shader { state_hash, .. }) => {
mix(&mut h, 4);
mix_u32(&mut h, state_hash);
}
DrawPayload::Sprite(SpriteSource::Svg {
source,
is_path_only,
tint,
stroke_width_x10,
}) => {
mix(&mut h, 6);
mix(&mut h, if is_path_only { 1 } else { 0 });
mix(&mut h, stroke_width_x10);
mix_u32(&mut h, tint.0);
for &b in source.as_str().as_bytes() {
mix(&mut h, b);
}
}
DrawPayload::Sprite(SpriteSource::Arc {
thickness,
start_deg10,
sweep_deg10,
fill_sweep_deg10,
track_color,
fill_color,
}) => {
mix(&mut h, 5);
mix(&mut h, thickness);
mix_i16(&mut h, start_deg10 as i16);
mix_i16(&mut h, sweep_deg10 as i16);
mix_i16(&mut h, fill_sweep_deg10 as i16);
mix_u32(&mut h, track_color.0);
mix_u32(&mut h, fill_color.0);
}
}
DrawOpSnapshot {
effective_bounds: self.bounds.intersect(self.clip),
payload_hash: h,
}
}
}