1use crate::{
2 Brush, Color, ColorFilter, CornerRadii, DrawPrimitive, ImageBitmap, LayerShape, Point, Rect,
3 RenderEffect, RuntimeShader, ShadowPrimitive,
4};
5use std::collections::hash_map::DefaultHasher;
6use std::hash::{Hash, Hasher};
7
8pub trait RenderHash {
9 fn render_hash(&self) -> u64;
10}
11
12impl RenderHash for Color {
13 fn render_hash(&self) -> u64 {
14 finish_hash(|state| hash_color(*self, state))
15 }
16}
17
18impl RenderHash for Point {
19 fn render_hash(&self) -> u64 {
20 finish_hash(|state| hash_point(*self, state))
21 }
22}
23
24impl RenderHash for Rect {
25 fn render_hash(&self) -> u64 {
26 finish_hash(|state| hash_rect(*self, state))
27 }
28}
29
30impl RenderHash for CornerRadii {
31 fn render_hash(&self) -> u64 {
32 finish_hash(|state| hash_corner_radii(*self, state))
33 }
34}
35
36impl RenderHash for LayerShape {
37 fn render_hash(&self) -> u64 {
38 finish_hash(|state| hash_layer_shape(*self, state))
39 }
40}
41
42impl RenderHash for Brush {
43 fn render_hash(&self) -> u64 {
44 finish_hash(|state| hash_brush(self, state))
45 }
46}
47
48impl RenderHash for ColorFilter {
49 fn render_hash(&self) -> u64 {
50 finish_hash(|state| hash_color_filter(*self, state))
51 }
52}
53
54impl RenderHash for ImageBitmap {
55 fn render_hash(&self) -> u64 {
56 finish_hash(|state| self.id().hash(state))
57 }
58}
59
60impl RenderHash for RuntimeShader {
61 fn render_hash(&self) -> u64 {
62 finish_hash(|state| hash_runtime_shader(self, state))
63 }
64}
65
66impl RenderHash for RenderEffect {
67 fn render_hash(&self) -> u64 {
68 finish_hash(|state| hash_render_effect(self, state))
69 }
70}
71
72impl RenderHash for DrawPrimitive {
73 fn render_hash(&self) -> u64 {
74 finish_hash(|state| hash_draw_primitive(self, state))
75 }
76}
77
78impl RenderHash for ShadowPrimitive {
79 fn render_hash(&self) -> u64 {
80 finish_hash(|state| hash_shadow_primitive(self, state))
81 }
82}
83
84fn finish_hash(write: impl FnOnce(&mut DefaultHasher)) -> u64 {
85 let mut hasher = DefaultHasher::new();
86 write(&mut hasher);
87 hasher.finish()
88}
89
90fn hash_f32_bits<H: Hasher>(value: f32, state: &mut H) {
91 value.to_bits().hash(state);
92}
93
94fn hash_color<H: Hasher>(color: Color, state: &mut H) {
95 hash_f32_bits(color.0, state);
96 hash_f32_bits(color.1, state);
97 hash_f32_bits(color.2, state);
98 hash_f32_bits(color.3, state);
99}
100
101fn hash_point<H: Hasher>(point: Point, state: &mut H) {
102 hash_f32_bits(point.x, state);
103 hash_f32_bits(point.y, state);
104}
105
106fn hash_rect<H: Hasher>(rect: Rect, state: &mut H) {
107 hash_f32_bits(rect.x, state);
108 hash_f32_bits(rect.y, state);
109 hash_f32_bits(rect.width, state);
110 hash_f32_bits(rect.height, state);
111}
112
113fn hash_corner_radii<H: Hasher>(radii: CornerRadii, state: &mut H) {
114 hash_f32_bits(radii.top_left, state);
115 hash_f32_bits(radii.top_right, state);
116 hash_f32_bits(radii.bottom_right, state);
117 hash_f32_bits(radii.bottom_left, state);
118}
119
120fn hash_layer_shape<H: Hasher>(shape: LayerShape, state: &mut H) {
121 match shape {
122 LayerShape::Rectangle => 0u8.hash(state),
123 LayerShape::Rounded(shape) => {
124 1u8.hash(state);
125 hash_corner_radii(shape.radii(), state);
126 }
127 }
128}
129
130fn hash_brush<H: Hasher>(brush: &Brush, state: &mut H) {
131 match brush {
132 Brush::Solid(color) => {
133 0u8.hash(state);
134 hash_color(*color, state);
135 }
136 Brush::LinearGradient {
137 colors,
138 stops,
139 start,
140 end,
141 tile_mode,
142 } => {
143 1u8.hash(state);
144 hash_color_slice(colors, state);
145 hash_optional_stop_list(stops.as_deref(), state);
146 hash_point(*start, state);
147 hash_point(*end, state);
148 tile_mode.hash(state);
149 }
150 Brush::RadialGradient {
151 colors,
152 stops,
153 center,
154 radius,
155 tile_mode,
156 } => {
157 2u8.hash(state);
158 hash_color_slice(colors, state);
159 hash_optional_stop_list(stops.as_deref(), state);
160 hash_point(*center, state);
161 hash_f32_bits(*radius, state);
162 tile_mode.hash(state);
163 }
164 Brush::SweepGradient {
165 colors,
166 stops,
167 center,
168 } => {
169 3u8.hash(state);
170 hash_color_slice(colors, state);
171 hash_optional_stop_list(stops.as_deref(), state);
172 hash_point(*center, state);
173 }
174 }
175}
176
177fn hash_color_slice<H: Hasher>(colors: &[Color], state: &mut H) {
178 colors.len().hash(state);
179 for color in colors {
180 hash_color(*color, state);
181 }
182}
183
184fn hash_optional_stop_list<H: Hasher>(stops: Option<&[f32]>, state: &mut H) {
185 match stops {
186 Some(stops) => {
187 1u8.hash(state);
188 stops.len().hash(state);
189 for stop in stops {
190 hash_f32_bits(*stop, state);
191 }
192 }
193 None => 0u8.hash(state),
194 }
195}
196
197fn hash_color_filter<H: Hasher>(filter: ColorFilter, state: &mut H) {
198 match filter {
199 ColorFilter::Tint(color) => {
200 0u8.hash(state);
201 hash_color(color, state);
202 }
203 ColorFilter::Modulate(color) => {
204 1u8.hash(state);
205 hash_color(color, state);
206 }
207 ColorFilter::Matrix(matrix) => {
208 2u8.hash(state);
209 for value in matrix {
210 hash_f32_bits(value, state);
211 }
212 }
213 }
214}
215
216fn hash_runtime_shader<H: Hasher>(shader: &RuntimeShader, state: &mut H) {
217 shader.source().hash(state);
218 shader.uniforms().len().hash(state);
219 for value in shader.uniforms() {
220 hash_f32_bits(*value, state);
221 }
222}
223
224fn hash_render_effect<H: Hasher>(effect: &RenderEffect, state: &mut H) {
225 match effect {
226 RenderEffect::Blur {
227 radius_x,
228 radius_y,
229 edge_treatment,
230 } => {
231 0u8.hash(state);
232 hash_f32_bits(*radius_x, state);
233 hash_f32_bits(*radius_y, state);
234 edge_treatment.hash(state);
235 }
236 RenderEffect::Offset { offset_x, offset_y } => {
237 1u8.hash(state);
238 hash_f32_bits(*offset_x, state);
239 hash_f32_bits(*offset_y, state);
240 }
241 RenderEffect::Shader { shader } => {
242 2u8.hash(state);
243 hash_runtime_shader(shader, state);
244 }
245 RenderEffect::Chain { first, second } => {
246 3u8.hash(state);
247 hash_render_effect(first, state);
248 hash_render_effect(second, state);
249 }
250 }
251}
252
253fn hash_draw_primitive<H: Hasher>(primitive: &DrawPrimitive, state: &mut H) {
254 match primitive {
255 DrawPrimitive::Content => {
256 0u8.hash(state);
257 }
258 DrawPrimitive::Blend {
259 primitive,
260 blend_mode,
261 } => {
262 1u8.hash(state);
263 blend_mode.hash(state);
264 hash_draw_primitive(primitive, state);
265 }
266 DrawPrimitive::Rect { rect, brush } => {
267 2u8.hash(state);
268 hash_rect(*rect, state);
269 hash_brush(brush, state);
270 }
271 DrawPrimitive::RoundRect { rect, brush, radii } => {
272 3u8.hash(state);
273 hash_rect(*rect, state);
274 hash_brush(brush, state);
275 hash_corner_radii(*radii, state);
276 }
277 DrawPrimitive::Image {
278 rect,
279 image,
280 alpha,
281 color_filter,
282 src_rect,
283 } => {
284 4u8.hash(state);
285 hash_rect(*rect, state);
286 image.id().hash(state);
287 hash_f32_bits(*alpha, state);
288 match color_filter {
289 Some(filter) => {
290 1u8.hash(state);
291 hash_color_filter(*filter, state);
292 }
293 None => 0u8.hash(state),
294 }
295 match src_rect {
296 Some(rect) => {
297 1u8.hash(state);
298 hash_rect(*rect, state);
299 }
300 None => 0u8.hash(state),
301 }
302 }
303 DrawPrimitive::Shadow(shadow) => {
304 5u8.hash(state);
305 hash_shadow_primitive(shadow, state);
306 }
307 }
308}
309
310fn hash_shadow_primitive<H: Hasher>(shadow: &ShadowPrimitive, state: &mut H) {
311 match shadow {
312 ShadowPrimitive::Drop {
313 shape,
314 blur_radius,
315 blend_mode,
316 } => {
317 0u8.hash(state);
318 hash_draw_primitive(shape, state);
319 hash_f32_bits(*blur_radius, state);
320 blend_mode.hash(state);
321 }
322 ShadowPrimitive::Inner {
323 fill,
324 cutout,
325 blur_radius,
326 blend_mode,
327 clip_rect,
328 } => {
329 1u8.hash(state);
330 hash_draw_primitive(fill, state);
331 hash_draw_primitive(cutout, state);
332 hash_f32_bits(*blur_radius, state);
333 blend_mode.hash(state);
334 hash_rect(*clip_rect, state);
335 }
336 }
337}
338
339#[cfg(test)]
340mod tests {
341 use super::*;
342 use crate::render_effect::TileMode;
343
344 #[test]
345 fn color_render_hash_changes_with_channels() {
346 assert_ne!(
347 Color(1.0, 0.0, 0.0, 1.0).render_hash(),
348 Color(0.0, 1.0, 0.0, 1.0).render_hash()
349 );
350 }
351
352 #[test]
353 fn point_rect_and_corner_radii_render_hash_changes_with_geometry() {
354 assert_ne!(
355 Point::new(1.0, 2.0).render_hash(),
356 Point::new(2.0, 1.0).render_hash()
357 );
358 assert_ne!(
359 Rect {
360 x: 0.0,
361 y: 0.0,
362 width: 10.0,
363 height: 20.0,
364 }
365 .render_hash(),
366 Rect {
367 x: 0.0,
368 y: 0.0,
369 width: 20.0,
370 height: 10.0,
371 }
372 .render_hash()
373 );
374 assert_ne!(
375 CornerRadii::uniform(4.0).render_hash(),
376 CornerRadii::uniform(6.0).render_hash()
377 );
378 }
379
380 #[test]
381 fn layer_shape_render_hash_tracks_shape_kind_and_radii() {
382 assert_ne!(
383 LayerShape::Rectangle.render_hash(),
384 LayerShape::Rounded(crate::RoundedCornerShape::uniform(8.0)).render_hash()
385 );
386 assert_ne!(
387 LayerShape::Rounded(crate::RoundedCornerShape::uniform(4.0)).render_hash(),
388 LayerShape::Rounded(crate::RoundedCornerShape::uniform(8.0)).render_hash()
389 );
390 }
391
392 #[test]
393 fn brush_render_hash_tracks_gradient_structure() {
394 let base = Brush::linear_gradient_with_tile_mode(
395 vec![Color::RED, Color::BLUE],
396 Point::new(0.0, 0.0),
397 Point::new(10.0, 10.0),
398 TileMode::Clamp,
399 );
400 let shifted = Brush::linear_gradient_with_tile_mode(
401 vec![Color::RED, Color::BLUE],
402 Point::new(1.0, 0.0),
403 Point::new(10.0, 10.0),
404 TileMode::Clamp,
405 );
406
407 assert_ne!(base.render_hash(), shifted.render_hash());
408 }
409
410 #[test]
411 fn color_filter_render_hash_tracks_variant_and_values() {
412 assert_ne!(
413 ColorFilter::Tint(Color::RED).render_hash(),
414 ColorFilter::Modulate(Color::RED).render_hash()
415 );
416 assert_ne!(
417 ColorFilter::Matrix([1.0; 20]).render_hash(),
418 ColorFilter::Matrix([0.0; 20]).render_hash()
419 );
420 }
421
422 #[test]
423 fn render_effect_render_hash_tracks_variant_parameters() {
424 assert_ne!(
425 RenderEffect::blur(4.0).render_hash(),
426 RenderEffect::blur(6.0).render_hash()
427 );
428 assert_ne!(
429 RenderEffect::offset(2.0, 1.0).render_hash(),
430 RenderEffect::offset(1.0, 2.0).render_hash()
431 );
432 }
433
434 #[test]
435 fn runtime_shader_render_hash_tracks_uniforms() {
436 let mut base = RuntimeShader::new("// hash");
437 base.set_float(0, 1.0);
438 let mut changed = base.clone();
439 changed.set_float(0, 2.0);
440 assert_ne!(base.render_hash(), changed.render_hash());
441 }
442
443 #[test]
444 fn draw_primitive_render_hash_tracks_nested_structure() {
445 let base = DrawPrimitive::Blend {
446 primitive: Box::new(DrawPrimitive::Rect {
447 rect: Rect {
448 x: 0.0,
449 y: 0.0,
450 width: 12.0,
451 height: 8.0,
452 },
453 brush: Brush::solid(Color::WHITE),
454 }),
455 blend_mode: crate::BlendMode::SrcOver,
456 };
457 let changed = DrawPrimitive::Blend {
458 primitive: Box::new(DrawPrimitive::Rect {
459 rect: Rect {
460 x: 0.0,
461 y: 0.0,
462 width: 12.0,
463 height: 8.0,
464 },
465 brush: Brush::solid(Color::BLACK),
466 }),
467 blend_mode: crate::BlendMode::SrcOver,
468 };
469 assert_ne!(base.render_hash(), changed.render_hash());
470 }
471}