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