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().hash(state);
223}
224
225fn hash_render_effect<H: Hasher>(effect: &RenderEffect, state: &mut H) {
226 match effect {
227 RenderEffect::Blur {
228 radius_x,
229 radius_y,
230 edge_treatment,
231 } => {
232 0u8.hash(state);
233 hash_f32_bits(*radius_x, state);
234 hash_f32_bits(*radius_y, state);
235 edge_treatment.hash(state);
236 }
237 RenderEffect::Offset { offset_x, offset_y } => {
238 1u8.hash(state);
239 hash_f32_bits(*offset_x, state);
240 hash_f32_bits(*offset_y, state);
241 }
242 RenderEffect::Shader { shader } => {
243 2u8.hash(state);
244 hash_runtime_shader(shader, state);
245 }
246 RenderEffect::Chain { first, second } => {
247 3u8.hash(state);
248 hash_render_effect(first, state);
249 hash_render_effect(second, state);
250 }
251 }
252}
253
254fn hash_draw_primitive<H: Hasher>(primitive: &DrawPrimitive, state: &mut H) {
255 match primitive {
256 DrawPrimitive::Content => {
257 0u8.hash(state);
258 }
259 DrawPrimitive::Blend {
260 primitive,
261 blend_mode,
262 } => {
263 1u8.hash(state);
264 blend_mode.hash(state);
265 hash_draw_primitive(primitive, state);
266 }
267 DrawPrimitive::Rect { rect, brush } => {
268 2u8.hash(state);
269 hash_rect(*rect, state);
270 hash_brush(brush, state);
271 }
272 DrawPrimitive::RoundRect { rect, brush, radii } => {
273 3u8.hash(state);
274 hash_rect(*rect, state);
275 hash_brush(brush, state);
276 hash_corner_radii(*radii, state);
277 }
278 DrawPrimitive::Image {
279 rect,
280 image,
281 alpha,
282 color_filter,
283 sampling,
284 src_rect,
285 } => {
286 4u8.hash(state);
287 hash_rect(*rect, state);
288 image.id().hash(state);
289 hash_f32_bits(*alpha, state);
290 sampling.hash(state);
291 match color_filter {
292 Some(filter) => {
293 1u8.hash(state);
294 hash_color_filter(*filter, state);
295 }
296 None => 0u8.hash(state),
297 }
298 match src_rect {
299 Some(rect) => {
300 1u8.hash(state);
301 hash_rect(*rect, state);
302 }
303 None => 0u8.hash(state),
304 }
305 }
306 DrawPrimitive::Shadow(shadow) => {
307 5u8.hash(state);
308 hash_shadow_primitive(shadow, state);
309 }
310 }
311}
312
313fn hash_shadow_primitive<H: Hasher>(shadow: &ShadowPrimitive, state: &mut H) {
314 match shadow {
315 ShadowPrimitive::Drop {
316 shape,
317 blur_radius,
318 blend_mode,
319 } => {
320 0u8.hash(state);
321 hash_draw_primitive(shape, state);
322 hash_f32_bits(*blur_radius, state);
323 blend_mode.hash(state);
324 }
325 ShadowPrimitive::Inner {
326 fill,
327 cutout,
328 blur_radius,
329 blend_mode,
330 clip_rect,
331 } => {
332 1u8.hash(state);
333 hash_draw_primitive(fill, state);
334 hash_draw_primitive(cutout, state);
335 hash_f32_bits(*blur_radius, state);
336 blend_mode.hash(state);
337 hash_rect(*clip_rect, state);
338 }
339 }
340}
341
342#[cfg(test)]
343mod tests {
344 use super::*;
345 use crate::render_effect::TileMode;
346
347 #[test]
348 fn color_render_hash_changes_with_channels() {
349 assert_ne!(
350 Color(1.0, 0.0, 0.0, 1.0).render_hash(),
351 Color(0.0, 1.0, 0.0, 1.0).render_hash()
352 );
353 }
354
355 #[test]
356 fn point_rect_and_corner_radii_render_hash_changes_with_geometry() {
357 assert_ne!(
358 Point::new(1.0, 2.0).render_hash(),
359 Point::new(2.0, 1.0).render_hash()
360 );
361 assert_ne!(
362 Rect {
363 x: 0.0,
364 y: 0.0,
365 width: 10.0,
366 height: 20.0,
367 }
368 .render_hash(),
369 Rect {
370 x: 0.0,
371 y: 0.0,
372 width: 20.0,
373 height: 10.0,
374 }
375 .render_hash()
376 );
377 assert_ne!(
378 CornerRadii::uniform(4.0).render_hash(),
379 CornerRadii::uniform(6.0).render_hash()
380 );
381 }
382
383 #[test]
384 fn layer_shape_render_hash_tracks_shape_kind_and_radii() {
385 assert_ne!(
386 LayerShape::Rectangle.render_hash(),
387 LayerShape::Rounded(crate::RoundedCornerShape::uniform(8.0)).render_hash()
388 );
389 assert_ne!(
390 LayerShape::Rounded(crate::RoundedCornerShape::uniform(4.0)).render_hash(),
391 LayerShape::Rounded(crate::RoundedCornerShape::uniform(8.0)).render_hash()
392 );
393 }
394
395 #[test]
396 fn brush_render_hash_tracks_gradient_structure() {
397 let base = Brush::linear_gradient_with_tile_mode(
398 vec![Color::RED, Color::BLUE],
399 Point::new(0.0, 0.0),
400 Point::new(10.0, 10.0),
401 TileMode::Clamp,
402 );
403 let shifted = Brush::linear_gradient_with_tile_mode(
404 vec![Color::RED, Color::BLUE],
405 Point::new(1.0, 0.0),
406 Point::new(10.0, 10.0),
407 TileMode::Clamp,
408 );
409
410 assert_ne!(base.render_hash(), shifted.render_hash());
411 }
412
413 #[test]
414 fn color_filter_render_hash_tracks_variant_and_values() {
415 assert_ne!(
416 ColorFilter::Tint(Color::RED).render_hash(),
417 ColorFilter::Modulate(Color::RED).render_hash()
418 );
419 assert_ne!(
420 ColorFilter::Matrix([1.0; 20]).render_hash(),
421 ColorFilter::Matrix([0.0; 20]).render_hash()
422 );
423 }
424
425 #[test]
426 fn render_effect_render_hash_tracks_variant_parameters() {
427 assert_ne!(
428 RenderEffect::blur(4.0).render_hash(),
429 RenderEffect::blur(6.0).render_hash()
430 );
431 assert_ne!(
432 RenderEffect::offset(2.0, 1.0).render_hash(),
433 RenderEffect::offset(1.0, 2.0).render_hash()
434 );
435 }
436
437 #[test]
438 fn runtime_shader_render_hash_ignores_uniforms() {
439 let mut base = RuntimeShader::new("// hash");
440 base.set_float(0, 1.0);
441 let mut changed = base.clone();
442 changed.set_float(0, 2.0);
443 assert_eq!(
444 base.render_hash(),
445 changed.render_hash(),
446 "render_hash must depend only on source, not uniforms — \
447 animated uniforms (time, position) would otherwise produce a \
448 new effect_hash every frame, filling the layer cache with stale textures"
449 );
450 }
451
452 #[test]
453 fn runtime_shader_render_hash_tracks_source() {
454 let a = RuntimeShader::new("// shader A");
455 let b = RuntimeShader::new("// shader B");
456 assert_ne!(a.render_hash(), b.render_hash());
457 }
458
459 #[test]
460 fn draw_primitive_render_hash_tracks_nested_structure() {
461 let base = DrawPrimitive::Blend {
462 primitive: Box::new(DrawPrimitive::Rect {
463 rect: Rect {
464 x: 0.0,
465 y: 0.0,
466 width: 12.0,
467 height: 8.0,
468 },
469 brush: Brush::solid(Color::WHITE),
470 }),
471 blend_mode: crate::BlendMode::SrcOver,
472 };
473 let changed = DrawPrimitive::Blend {
474 primitive: Box::new(DrawPrimitive::Rect {
475 rect: Rect {
476 x: 0.0,
477 y: 0.0,
478 width: 12.0,
479 height: 8.0,
480 },
481 brush: Brush::solid(Color::BLACK),
482 }),
483 blend_mode: crate::BlendMode::SrcOver,
484 };
485 assert_ne!(base.render_hash(), changed.render_hash());
486 }
487}