1use crate::arena::StrRef;
4use crate::element::SpriteSource;
5use crate::solver::Region;
6use crate::style::Color;
7
8#[derive(Copy, Clone, PartialEq, Eq)]
10pub struct DrawOp<'a> {
11 pub id: Option<u32>,
13 pub bounds: Region,
15 pub clip: Region,
17 pub clip_radius: u8,
19 pub payload: DrawPayload<'a>,
21}
22
23#[derive(Copy, Clone)]
25pub enum DrawPayload<'a> {
26 Quad {
28 bg: Color,
29 bg_grad: Color,
30 border_color: Color,
31 rounded: u8,
32 border_width: u8,
33 },
34 TextRun {
36 text: StrRef<'a>,
37 color: Color,
38 font_size: u8,
39 text_flags: u8,
40 },
41 Sprite(SpriteSource<'a>),
43}
44
45impl<'a> PartialEq for DrawPayload<'a> {
46 fn eq(&self, other: &Self) -> bool {
47 match (*self, *other) {
48 (
49 Self::Quad {
50 bg: b1,
51 bg_grad: bg1,
52 border_color: bc1,
53 rounded: r1,
54 border_width: bw1,
55 },
56 Self::Quad {
57 bg: b2,
58 bg_grad: bg2,
59 border_color: bc2,
60 rounded: r2,
61 border_width: bw2,
62 },
63 ) => b1 == b2 && bg1 == bg2 && bc1 == bc2 && r1 == r2 && bw1 == bw2,
64 (
65 Self::TextRun {
66 text: t1,
67 color: c1,
68 font_size: f1,
69 text_flags: tf1,
70 },
71 Self::TextRun {
72 text: t2,
73 color: c2,
74 font_size: f2,
75 text_flags: tf2,
76 },
77 ) => t1 == t2 && c1 == c2 && f1 == f2 && tf1 == tf2,
78 (
79 Self::Sprite(SpriteSource::Bitmap {
80 bytes: b1,
81 format: f1,
82 tint: t1,
83 }),
84 Self::Sprite(SpriteSource::Bitmap {
85 bytes: b2,
86 format: f2,
87 tint: t2,
88 }),
89 ) => {
90 core::ptr::eq(b1.as_ptr(), b2.as_ptr())
91 && b1.len() == b2.len()
92 && f1 == f2
93 && t1 == t2
94 }
95 (
96 Self::Sprite(SpriteSource::Shader { state_hash: h1, .. }),
97 Self::Sprite(SpriteSource::Shader { state_hash: h2, .. }),
98 ) => h1 == h2,
99 (
100 Self::Sprite(s1 @ SpriteSource::Svg { .. }),
101 Self::Sprite(s2 @ SpriteSource::Svg { .. }),
102 ) => s1 == s2,
103 (Self::Sprite(s1 @ SpriteSource::Arc { .. }), Self::Sprite(s2 @ SpriteSource::Arc { .. })) => {
104 s1 == s2
105 }
106 _ => false,
107 }
108 }
109}
110
111impl<'a> Eq for DrawPayload<'a> {}
112
113#[repr(C)]
115#[derive(Copy, Clone, PartialEq, Eq, Debug)]
116pub struct DrawOpSnapshot {
117 pub effective_bounds: Region,
119 pub payload_hash: u32,
121}
122
123impl<'a> DrawOp<'a> {
124 pub fn to_snapshot(&self) -> DrawOpSnapshot {
126 let mut h: u32 = 0x811c_9dc5;
127 let mix = |h: &mut u32, byte: u8| {
128 *h ^= byte as u32;
129 *h = h.wrapping_mul(0x0100_0193);
130 };
131 let mix_i16 = |h: &mut u32, val: i16| {
132 for b in val.to_le_bytes() {
133 mix(h, b);
134 }
135 };
136 let mix_u32 = |h: &mut u32, val: u32| {
137 for b in val.to_le_bytes() {
138 mix(h, b);
139 }
140 };
141 if let Some(id) = self.id {
142 mix(&mut h, 0xFF);
143 mix_u32(&mut h, id);
144 }
145 mix_i16(&mut h, self.bounds.x);
147 mix_i16(&mut h, self.bounds.y);
148 mix_i16(&mut h, self.bounds.w);
149 mix_i16(&mut h, self.bounds.h);
150 mix_i16(&mut h, self.clip.x);
151 mix_i16(&mut h, self.clip.y);
152 mix_i16(&mut h, self.clip.w);
153 mix_i16(&mut h, self.clip.h);
154 mix(&mut h, self.clip_radius);
155
156 match self.payload {
157 DrawPayload::Quad {
158 bg,
159 bg_grad,
160 border_color,
161 rounded,
162 border_width,
163 } => {
164 mix(&mut h, 1);
165 mix_u32(&mut h, bg.0);
166 mix_u32(&mut h, bg_grad.0);
167 mix_u32(&mut h, border_color.0);
168 mix(&mut h, rounded);
169 mix(&mut h, border_width);
170 }
171 DrawPayload::TextRun {
172 text,
173 color,
174 font_size,
175 text_flags,
176 } => {
177 mix(&mut h, 2);
178 mix_u32(&mut h, color.0);
179 mix(&mut h, font_size);
180 mix(&mut h, text_flags);
181 for &b in text.as_str().as_bytes() {
182 mix(&mut h, b);
183 }
184 }
185 DrawPayload::Sprite(SpriteSource::Bitmap {
186 bytes,
187 format,
188 tint,
189 }) => {
190 mix(&mut h, 3);
191 mix(&mut h, format as u8);
192 mix_u32(&mut h, tint.0);
193 mix_u32(&mut h, bytes.as_ptr() as usize as u32);
194 mix_u32(&mut h, bytes.len() as u32);
195 }
196 DrawPayload::Sprite(SpriteSource::Shader { state_hash, .. }) => {
197 mix(&mut h, 4);
198 mix_u32(&mut h, state_hash);
199 }
200 DrawPayload::Sprite(SpriteSource::Svg {
201 source,
202 is_path_only,
203 tint,
204 stroke_width_x10,
205 }) => {
206 mix(&mut h, 6);
207 mix(&mut h, if is_path_only { 1 } else { 0 });
208 mix(&mut h, stroke_width_x10);
209 mix_u32(&mut h, tint.0);
210 for &b in source.as_str().as_bytes() {
211 mix(&mut h, b);
212 }
213 }
214 DrawPayload::Sprite(SpriteSource::Arc {
215 thickness,
216 start_deg10,
217 sweep_deg10,
218 fill_sweep_deg10,
219 track_color,
220 fill_color,
221 }) => {
222 mix(&mut h, 5);
223 mix(&mut h, thickness);
224 mix_i16(&mut h, start_deg10 as i16);
225 mix_i16(&mut h, sweep_deg10 as i16);
226 mix_i16(&mut h, fill_sweep_deg10 as i16);
227 mix_u32(&mut h, track_color.0);
228 mix_u32(&mut h, fill_color.0);
229 }
230 }
231 DrawOpSnapshot {
232 effective_bounds: self.bounds.intersect(self.clip),
233 payload_hash: h,
234 }
235 }
236}
237