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