1use crate::arena::StrRef;
4use crate::element::SpriteSource;
5use crate::paint::{ExternalTextureSpec, LayerSpec, StyledQuad};
6use crate::solver::Region;
7use crate::style::Color;
8
9#[derive(Copy, Clone, PartialEq, Eq)]
11pub struct DrawOp<'a> {
12 pub id: Option<u32>,
14 pub bounds: Region,
16 pub clip: Region,
18 pub clip_radius: u8,
20 pub payload: DrawPayload<'a>,
22}
23
24#[derive(Copy, Clone)]
26pub enum DrawPayload<'a> {
27 Quad {
29 bg: Color,
30 bg_grad: Color,
31 border_color: Color,
32 rounded: u8,
33 border_width: u8,
34 },
35 TextRun {
37 text: StrRef<'a>,
38 color: Color,
39 font_size: u8,
40 text_flags: u8,
41 },
42 Sprite(SpriteSource<'a>),
44 StyledQuad(&'a StyledQuad<'a>),
46 PushLayer(&'a LayerSpec<'a>),
48 PopLayer,
50 ExternalTexture(&'a ExternalTextureSpec),
52}
53
54impl<'a> PartialEq for DrawPayload<'a> {
55 fn eq(&self, other: &Self) -> bool {
56 match (*self, *other) {
57 (
58 Self::Quad {
59 bg: b1,
60 bg_grad: bg1,
61 border_color: bc1,
62 rounded: r1,
63 border_width: bw1,
64 },
65 Self::Quad {
66 bg: b2,
67 bg_grad: bg2,
68 border_color: bc2,
69 rounded: r2,
70 border_width: bw2,
71 },
72 ) => b1 == b2 && bg1 == bg2 && bc1 == bc2 && r1 == r2 && bw1 == bw2,
73 (
74 Self::TextRun {
75 text: t1,
76 color: c1,
77 font_size: f1,
78 text_flags: tf1,
79 },
80 Self::TextRun {
81 text: t2,
82 color: c2,
83 font_size: f2,
84 text_flags: tf2,
85 },
86 ) => t1 == t2 && c1 == c2 && f1 == f2 && tf1 == tf2,
87 (
88 Self::Sprite(SpriteSource::Bitmap {
89 bytes: b1,
90 format: f1,
91 tint: t1,
92 }),
93 Self::Sprite(SpriteSource::Bitmap {
94 bytes: b2,
95 format: f2,
96 tint: t2,
97 }),
98 ) => {
99 core::ptr::eq(b1.as_ptr(), b2.as_ptr())
100 && b1.len() == b2.len()
101 && f1 == f2
102 && t1 == t2
103 }
104 (
105 Self::Sprite(SpriteSource::Shader { state_hash: h1, .. }),
106 Self::Sprite(SpriteSource::Shader { state_hash: h2, .. }),
107 ) => h1 == h2,
108 (
109 Self::Sprite(s1 @ SpriteSource::Svg { .. }),
110 Self::Sprite(s2 @ SpriteSource::Svg { .. }),
111 ) => s1 == s2,
112 (Self::Sprite(s1 @ SpriteSource::Arc { .. }), Self::Sprite(s2 @ SpriteSource::Arc { .. })) => {
113 s1 == s2
114 }
115 (Self::StyledQuad(a), Self::StyledQuad(b)) => a == b,
116 (Self::PushLayer(a), Self::PushLayer(b)) => a == b,
117 (Self::PopLayer, Self::PopLayer) => true,
118 (Self::ExternalTexture(a), Self::ExternalTexture(b)) => a == b,
119 _ => false,
120 }
121 }
122}
123
124impl<'a> Eq for DrawPayload<'a> {}
125
126#[repr(C)]
128#[derive(Copy, Clone, PartialEq, Eq, Debug)]
129pub struct DrawOpSnapshot {
130 pub effective_bounds: Region,
132 pub payload_hash: u32,
134}
135
136impl<'a> DrawOp<'a> {
137 pub fn to_snapshot(&self) -> DrawOpSnapshot {
139 let mut h: u32 = 0x811c_9dc5;
140 let mix = |h: &mut u32, byte: u8| {
141 *h ^= byte as u32;
142 *h = h.wrapping_mul(0x0100_0193);
143 };
144 let mix_i16 = |h: &mut u32, val: i16| {
145 for b in val.to_le_bytes() {
146 mix(h, b);
147 }
148 };
149 let mix_u32 = |h: &mut u32, val: u32| {
150 for b in val.to_le_bytes() {
151 mix(h, b);
152 }
153 };
154 if let Some(id) = self.id {
155 mix(&mut h, 0xFF);
156 mix_u32(&mut h, id);
157 }
158 mix_i16(&mut h, self.bounds.x);
160 mix_i16(&mut h, self.bounds.y);
161 mix_i16(&mut h, self.bounds.w);
162 mix_i16(&mut h, self.bounds.h);
163 mix_i16(&mut h, self.clip.x);
164 mix_i16(&mut h, self.clip.y);
165 mix_i16(&mut h, self.clip.w);
166 mix_i16(&mut h, self.clip.h);
167 mix(&mut h, self.clip_radius);
168
169 match self.payload {
170 DrawPayload::Quad {
171 bg,
172 bg_grad,
173 border_color,
174 rounded,
175 border_width,
176 } => {
177 mix(&mut h, 1);
178 mix_u32(&mut h, bg.0);
179 mix_u32(&mut h, bg_grad.0);
180 mix_u32(&mut h, border_color.0);
181 mix(&mut h, rounded);
182 mix(&mut h, border_width);
183 }
184 DrawPayload::TextRun {
185 text,
186 color,
187 font_size,
188 text_flags,
189 } => {
190 mix(&mut h, 2);
191 mix_u32(&mut h, color.0);
192 mix(&mut h, font_size);
193 mix(&mut h, text_flags);
194 for &b in text.as_str().as_bytes() {
195 mix(&mut h, b);
196 }
197 }
198 DrawPayload::Sprite(SpriteSource::Bitmap {
199 bytes,
200 format,
201 tint,
202 }) => {
203 mix(&mut h, 3);
204 mix(&mut h, format as u8);
205 mix_u32(&mut h, tint.0);
206 mix_u32(&mut h, bytes.as_ptr() as usize as u32);
207 mix_u32(&mut h, bytes.len() as u32);
208 }
209 DrawPayload::Sprite(SpriteSource::Shader { state_hash, .. }) => {
210 mix(&mut h, 4);
211 mix_u32(&mut h, state_hash);
212 }
213 DrawPayload::Sprite(SpriteSource::Svg {
214 source,
215 is_path_only,
216 tint,
217 stroke_width_x10,
218 }) => {
219 mix(&mut h, 6);
220 mix(&mut h, if is_path_only { 1 } else { 0 });
221 mix(&mut h, stroke_width_x10);
222 mix_u32(&mut h, tint.0);
223 for &b in source.as_str().as_bytes() {
224 mix(&mut h, b);
225 }
226 }
227 DrawPayload::Sprite(SpriteSource::Arc {
228 thickness,
229 start_deg10,
230 sweep_deg10,
231 fill_sweep_deg10,
232 track_color,
233 fill_color,
234 }) => {
235 mix(&mut h, 5);
236 mix(&mut h, thickness);
237 mix_i16(&mut h, start_deg10 as i16);
238 mix_i16(&mut h, sweep_deg10 as i16);
239 mix_i16(&mut h, fill_sweep_deg10 as i16);
240 mix_u32(&mut h, track_color.0);
241 mix_u32(&mut h, fill_color.0);
242 }
243 DrawPayload::StyledQuad(sq) => {
244 mix(&mut h, 7);
245 sq.mix_hash(&mut h);
246 }
247 DrawPayload::PushLayer(layer) => {
248 mix(&mut h, 8);
249 layer.mix_hash(&mut h);
250 }
251 DrawPayload::PopLayer => {
252 mix(&mut h, 9);
253 }
254 DrawPayload::ExternalTexture(ext) => {
255 mix(&mut h, 10);
256 ext.mix_hash(&mut h);
257 }
258 }
259 let eff_bounds = match self.payload {
260 DrawPayload::PushLayer(layer) => layer.expanded_bounds().intersect(self.clip),
261 DrawPayload::StyledQuad(sq) => {
262 if let Some(t) = sq.transform {
263 let cx = self.bounds.x as f32 + self.bounds.w as f32 * 0.5;
264 let cy = self.bounds.y as f32 + self.bounds.h as f32 * 0.5;
265 t.centered_at(cx, cy)
266 .transform_region(self.bounds)
267 .intersect(self.clip)
268 } else {
269 self.bounds.intersect(self.clip)
270 }
271 }
272 _ => self.bounds.intersect(self.clip),
273 };
274 DrawOpSnapshot {
275 effective_bounds: eff_bounds,
276 payload_hash: h,
277 }
278 }
279}
280